SSH connection in a Qt application

282 Views Asked by At

I have a Qt application in C++ that runs on a Windows host and has to execute some commands via SSH in a Linux machine.

I tried everything that came to my mind but without a single success:

  1. QProcess(), using both ssh <user>@<address> "command" and cmd /c "ssh <user>@<address> \"command\"" did nothing, powershell same story.
  2. Same story using the system().
  3. I tried both previous solutions with runas /user:Administrator but, again, nothing changed.
  4. I embedded the SSH command in a bat file and tried with previous solutions. Same story.

In general, when using directly ssh I don't get any output and it doesn't run; when I prepend cmd /c (or powershell) I get:

ssh is not recognized as an internal or external command, operable program or batch file

I know that I could use libssh or libssh2 but it's been asked me to try to avoid them, so I'll use them only if there's no other options.

Some snippet that I tried using:

  1. QProcess:
    QProcess* proc = new QProcess();
    proc->start("ssh", {"<user>@<address>",  "\"<command>\""}); 
    proc->waitForFinished();
    QString str = proc->readAll();
    std::cout << "out: "+ QString::number(proc->exitCode()) + " -- " + str;
    
  2. same as before but with full ssh.exe path
  3. same but with
    proc->start("cmd", {"/c", "ssh", "<user>@<address>",  "\"<command>\""});
    
  4. system():
    int ret = system("cmd /c \"ssh -t <user>@<address> \\\"<command>\\\"\"");
    std::cout()<<"ret: " + QString::number(ret));
    
    here I tried with all kind of \ escape possible.

EDIT: Ok, I might have found something.. Basically when I launch the command from my application, the OpenSSH folder containing ssh.exe is "missing". If I dir in c:\Windows\System32, I see everything except for the OpenSSH folder. If I run the same thing from CMD, the folder is present and contains ssh.exe Do you have any ideas on how to access the folder with my application?

0

There are 0 best solutions below