How to get the current username from a remote computer

Is there a way to get the current username from a remote computer? (I'm looking for something along the line getUserName ())

I have a solution that works, but (to be grossly honest) feels like it is killing a fly with a cruise missile taken off a skateboard (difficult, certainly 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.";
}

      

As I said: just a little bit on the extremely unsightly side.

The method gets a QString with an IP address, concatenates it with wmic.exe /node:

and computersystem get username 2> nul

and writes the output from the wmic.exe file to a text file, reads the required string (the second), truncates the string to the required information, and returns the specified information (username)

Now my problem is this: everything is fine and dandy if I just want to get one username or so at runtime ... which I don't have. I need to populate an entire table (containing up to 200 or more records depending on network activity) which takes 10 to 15 minutes.

Now my program handles getting IP collection and computername over sockets, but I am new to this type of programming (tbh: I just started C ++, coming from C and I have never done networking related programming stuff) so I am not that deep in this matter.

Is there a way to get the current username on the remote machine via a socket?

+3


source to share


1 answer


You can use QProcess

wmic.exe to process the 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();
}

      

+1


source







All Articles