write a C++ code to get the hostname - hostname

I want to write a C++ Code to get the full information on all the host name, and ip address and sub net mask of computer that has been added to the domain control.

gethostname will give you the name of the current host
gethostbyname looks up the host with a particular name and will give you the address
man 3 gethostname
man 3 gethostbyname
Or you can extract the information you need from the system like this :
#include <cstdlib>
#include <iostream>
#include <fstream>
int main(){
system( "ifconfig -a | grep inet | "
"sed 's/\\([ ]*[^ ]*\\)\\([ ]*[^ ]*\\).*$/\\1 \\2/' "
" > networkinfos.txt" ) ;
}

Related

How to get current computer's IP address in Xcode

When testing iPhone apps that connect to my local machine's server, I need to enter my computer's local IP address as opposed to localhost in order to test with devices other than the simulator. This gets annoying with dynamic IP addresses and when multiple developers are testing. Is there a code snippet that can get the IP address of the computer that is compiling code, and NOT the IP the address of the device that the application is running on (preferably in C or Objective-C, and not Swift)?
1) Add a "Run Script" in the "Build Phases" tab of your Xcode project that contains this:
export SERVER_IP=`ipconfig getifaddr en0`
Note: change "en0" to whichever interface matches your machine. en0 is the wifi on my machine and my hard-wire is en3. Do an "ifconfig -a" in Terminal to get the list of all of your adapters and see which is which for your machine
2) Go to your project file. Click the Project itself in the left menu then Build Settings in the right side. Go to "Apple LLVM 6.0 - Custom Compiler Flags". Under "Other C Flags" -> "Debug" define a new value called -DSERVER_IP=${SERVER_IP}
This will map your build script's results into a #DEFINE in your project
3) In your code use SERVER_IP just like you would any other #DEFINE and it will always have the value of the computer that built the code.
I got this working by having a run script set the computer's IP address in the app's plist, then reading the plist value in code.
1) In your Info.plist file, add a key/value pair that will contain your computer's IP address. For this example, we'll add a key of "CompanyNameDevServerIP", of type "String". Note that this key/value pair should be prefixed uniquely, so that it doesn't conflict with Apple's keys (see here for more info).
2) In the "Build Phases" tab, add a run script that contains the following:
if [ "$CONFIGURATION" == "Debug" ]; then
echo -n ${TARGET_BUILD_DIR}/${INFOPLIST_PATH} | xargs -0 /usr/libexec/PlistBuddy -c "Set :CompanyNameDevServerIP `ipconfig getifaddr en0`"
else
echo -n ${TARGET_BUILD_DIR}/${INFOPLIST_PATH} | xargs -0 /usr/libexec/PlistBuddy -c "Delete :CompanyNameDevServerIP"
fi
This sets the computer's IP address in the plist that gets bundled with the build, but only in debug builds. (It's removed from the plist file in release builds.)
Hat tip to this blog post for providing this technique.
You may need to use a different interface other than en0 (see here for more info).
3) In code, retrieve this plist value to get your computer's IP address:
NSString *serverIP = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CompanyNameDevServerIP"];
If you need this at compile time, you can just add a "Run Script" in the "Build Phases" tab of your Xcode project. Putting this into the source code will naturally return the IP address of where the code is running, not where it was built.
This script will return the primary IP address. You can modify the script to edit a Plist or whatever you need from there. PlistBuddy works well for modifying plist files at build time.
ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'

Obtaining and changing MAC address using objective-C in OS X

How can I obtain and then change MAC address using objective-C? The solution was suggested only for iOS: How can I programmatically get the MAC address of an iphone
This is setup a startup script to modify the MAC address each time you restart the computer.
#!/bin/sh
. /etc/rc.common
StartService () {
networksetup -setairportpower en0 on
networksetup -setairportpower en1 on
/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -z
/sbin/ifconfig en0 ether 00:`openssl rand -hex 5 | sed 's/\(..\)/\1:/g; s/.$//'`
/sbin/ifconfig en1 ether 00:`openssl rand -hex 5 | sed 's/\(..\)/\1:/g; s/.$//'`
networksetup -detectnewhardware
}
StopService () { return 0 }
RestartService () { return 0 }
RunService "$1"
Not directly using Objective-C but you can run a shell script from Obj-C using NSTask. More on that here.
I compiled this into a little installer a while back for a buddy. If you want the package, it's available here.

QSQLITE : not authorized unable to fetch row

I'm completely stuck with loading extensions in Qt's SQLite driver. I've done all steps from this 'how to'. Just changed SQLITE_ENABLE_LOAD_EXTENSION to DSQLITE_ENABLE_LOAD_EXTENSION. After that I'm trying to run following code in my app:
QSqlQuery tSqlQuery;
QString sql_command_text;
sql_command_text = QString("SELECT load_extension(\"libspatialite-4.dll\")");
if(!tSqlQuery.exec(sql_command_text))
qDebug() << tSqlQuery.lastError().text();
And qDebug gives me this: "not authorized Unable to fetch row".
I'm using Qt 4.6.3
What am I doing wrong? I've read tons of articles but couldn't find any solution.
You can be sure in following things:
I've done all steps from how-to
I've rebuild DLLs and replaced them
When I face this problem I've solved it this way:
Add in Src\qtbase\src\3rdparty\sqlite\sqlite3.c next lines:
#ifndef SQLITE_ENABLE_LOAD_EXTENSION
# define SQLITE_ENABLE_LOAD_EXTENSION 1
#endif
Added it somewhere near 150 line.
Removed from file Src\qtbase\src\3rdparty\sqlite.pri from DEFINES:
SQLITE_OMIT_EXTENSION
Leave it like this:
DEFINES += SQLITE_OMIT_COMPLETE SQLITE_ENABLE_FTS3 SQLITE_ENABLE_FTS3_PARENTHESIS SQLITE_ENABLE_RTREE
then cd command line (yeah, I'm at Win) to Src\qtbase\src\plugins\sqldrivers\sqlite
and execute next commands:
mingw32-make -j
mingw32-make install
flag -j makes make use all available cores for compiling, and install copy resulted dll to plugins\sqldrivers\sqlite dir (the place where all others dll's keep) of qt installation.
On linux (Ubuntu 14.10) I had to add this line in sqlite.pro before compiling:
LIBS=-ldl

Block remote CVS users from physically logging in to repo server

Our users are using ssh shell to run all CVS actitivies. Is it possible to block them from physically logging to the CVS server? For example, they use the following command to checkout files:
cvs -z3 -d:ext:username#cvsserver:/cvsroot/projects checkout -d project1 .
Since they are part of the group having the permissions to read/write CVS repository file system on the server, they could SSH login to the repo server and delete physical files. Can I block them? I have tried to sshd_config with DeneyUsers, or set /etc/passwd with NOLOGON shell, but they all block CVS commands too.
Thanks
Yes, it is possible. I'm using a custom shell that executes cvs only.
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char** argv)
{
if (getenv("CVSROOT") == NULL)
setenv("CVSROOT", "/home/cvs", 0);
execl("/usr/bin/cvs", "cvs", "server", NULL);
printf("No Permission.\n");
}
You could install rssh and set this as the user's login shell.
http://www.pizzashack.org/rssh/
rssh has a list of commands (including cvs) that you can restrict users to. If they try and ssh directly into the machine they will be denied and receive a message like this:
This account is restricted by rssh.
Allowed commands: cvs
If you believe this is in error, please contact your system administrator.

Using "runas" command for a program that writes a file (Windows Server 2008)

I wrote this simple console program (writeTxt.exe):
#include "stdafx.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char *fileName = "test.txt";
ofstream outStream(fileName, ios::out);
outStream << "This is a very simple test." << endl;
return 0;
}
Then I run it on the console of Windows Server 2008 using runas command (I logged-in as a different user from User1):
runas /user:User1 writeTxt.exe
But the program doesn't produce the file test.txt. If I log-in to the server as User1 and run the program, it works correctly. Do I have to set something for this to run correctly?
I believe that runas always launches programs with their working directory set to C:\Windows\System32 (or moral equivalent) rather than whatever your current working directory is when you invoke runas.
If User1 has permissions to write to that directory, that's where the file will be. If they don't have such a permission, then the program will fail.