Is there a way to get the currently logged in username from a remote computer? (I'm looking for something along the line of getUserName())
I've got a solution that kinda works but (to be brutally honest) feels like killing a fly with a cruise missile shot from a skateboard (complicated, definitely taking to long and probably overkill).
My solution (so far):
QString NetworkHandle::getUserName(QString entry){
QString s1, s2, command;
std::string temp, line;
const char *cmd;
char buf[BUFSIZ];
FILE *ptr, *file;
int c;
s1 = "wmic.exe /node:";
s2 = " computersystem get username 2> nul";
command = s1 + entry + s2;
temp = command.toLocal8Bit().constData();
cmd = temp.c_str();
file = fopen("buffer.txt", "w");
if(!file){
this->setErrLvl(3);
return "ERROR";
}
if((ptr = popen(cmd, "r")) != NULL){
while (fgets(buf, BUFSIZ, ptr) != NULL){
fprintf(file, "%s", buf);
}
pclose(ptr);
}
fclose(file);
std::ifstream input("buffer.txt");
c = 0;
if(!input){
this->setErrLvl(4);
return "ERROR";
}
while(!input.eof()){
std::getline(input, line);
if(c == 1 && line.size() > 1){
input.close();
entry = QString::fromUtf8(line.data(), line.size());
return entry;
}
c++;
}
input.close();
std::remove("buffer.txt");
return "Could not return Username.";
}
Like i said: just a tad on the extremly unpractial side.
The methode gets a QString with the IP-Address, combines it with wmic.exe /node:
and computersystem get username 2> nul
and writes the output from wmic.exe into a text-file, reads the required line (the second), shortens the string to the necessary information and returns said information (the User-name)
Now my problem is the following: This is all fine and dandy if i just want to get one username or so during runtime... which i don't. I need to fill an entire table (containing up to 200 or more entries, depending on network activity) which takes somewhere between 10 to 15 minutes.
Now my program handles getting the IP and computername collection via sockets but i'm new to this type of programming (tbh: i just started C++ coming from C and i never did any network-related programming stuff) so i'm not that deep in the matter.
Is there a way to get the currently logged in username on a remote computer via socket?
You can use QProcess
to handle wmic.exe tool like this:
void NetworkHandle::getUserName(QString entry)
{
QProcess *wmic_process = new QProcess();
wmic_process->setProgram("wmic.exe");
wmic_process->setArguments(QStringList() << QString("/node:%1").arg(entry) << "computersystem" << "get" << "username");
wmic_process->setProperty("ip_address", entry);
connect(wmic_process, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(parseUserName(int,QProcess::ExitStatus)) );
wmic_process->start();
}
void NetworkHandle::parseUserName(int exitCode, QProcess::ExitStatus exitStatus)
{
QProcess *process = dynamic_cast<QProcess*>(sender());
if (exitStatus == QProcess::NormalExit)
{
qDebug() << "information for node" << process->property("ip_address").toString();
qDebug() << process->readAllStandardOutput();
}
process->deleteLater();
}