有没有办法从远程计算机获取当前登录的用户名? (我正在寻找getUserName())
行的东西我有一个有点有效的解决方案,但是(粗暴地说是诚实的)感觉就像用滑板射击的巡航导弹杀死一只苍蝇(复杂,绝对长时间而且可能是矫枉过正)。
我的解决方案(到目前为止):
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.";
}
就像我说的那样:只是极端无法实现的一面。
methode获取带有IP地址的QString,将其与wmic.exe /node:
和computersystem get username 2> nul
结合,并将wmic.exe的输出写入文本文件,读取所需的行(第二个) ,将字符串缩短为必要的信息并返回所述信息(用户名)
现在我的问题如下:如果我只是想在运行时获得一个用户名,这一切都很好,我不这样做。我需要填写整个表格(包含多达200个或更多条目,具体取决于网络活动),这需要10到15分钟。
现在我的程序处理通过套接字获取IP和计算机名称集合,但我是这种类型的编程的新手(tbh:我刚开始C ++来自C,我从来没有做任何与网络相关的编程工作)所以我是事情并没有那么深刻。
有没有办法通过套接字在远程计算机上获取当前登录的用户名?
答案 0 :(得分:1)
您可以使用QProcess
来处理wmic.exe工具,如下所示:
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();
}