Skip to content

Instantly share code, notes, and snippets.

@quantumcore
Last active December 11, 2018 18:08
Show Gist options
  • Save quantumcore/9618c34b2a580e1e3c371e8fa090fb08 to your computer and use it in GitHub Desktop.
Save quantumcore/9618c34b2a580e1e3c371e8fa090fb08 to your computer and use it in GitHub Desktop.
[C++] Detect Hostname & Username. (Check if they are same) LINUX
#include <iostream>
#include <unistd.h>
#include <limits.h>
#include <string.h>
int main()
{
char hostname[HOST_NAME_MAX];
char loginName[LOGIN_NAME_MAX];
gethostname(hostname, HOST_NAME_MAX);
getlogin_r(loginName, LOGIN_NAME_MAX);
if(hostname == NULL)
{
std::cout << "Unable to get Hostname!" << std::endl;
} else {
std::cout << "[HOSTNAME]: " << hostname << std::endl;
}
std::cout << "Getting Logged User." << std::endl;
if(loginName == NULL)
{
std::cout << "Unable to get Logged User Name" << std::endl;
} else {
std::cout << "[USERNAME]: " << loginName << std::endl;
}
if(strcmp(loginName, hostname) == 0){
std::cout << "Username/Hostname same." << std::endl;
} else {
std::cout << "[OK]" << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment