如何从远程计算机中获取当前登录的用户名

How to get the currently logged in username from a remote computer

本文关键字:登录 用户 获取 计算机      更新时间:2023-10-16

是否有一种方法可以从远程计算机中获取当前登录的用户名?(我正在寻找沿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和Computername Collection,但我是此类编程的新手(TBH:我刚刚开始C 来自C,我从未做过任何与网络相关的编程内容(,所以我是在此问题上不是很深。

是否有一种方法可以通过套接字在远程计算机上获取当前登录的用户名?

您可以使用 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();
}
相关文章: