I'm looking for an easy way to establish an ssh connection in Qt similar to how I can operate in Java.
For instance, to log in to an ssh connection via java, I can do:
import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.transport.IgnoreHostKeyVerification;
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
......
SshClient ssh = new SshClient();
try {
// Timeout of ten seconds
ssh.setSocketTimeout(10*1000);
ssh.connect(ip, port, new IgnoreHostKeyVerification());
PasswordAuthenticationClient auth = new PasswordAuthenticationClient();
auth.setUsername(username);
auth.setPassword(password);
if (ssh.authenticate(auth) != AuthenticationProtocolState.COMPLETE) {
errorLabel.setForeground(Color.RED);
errorLabel.setText("Username or password is incorrect");
}
else
successful = true;
}
catch (Exception e) {
errorLabel.setForeground(Color.RED);
errorLabel.setText("Cannot log into website");
e.printStackTrace();
}
The solutions I've seen for Qt are:
- Roll my own using libssh
- Roll my own using libssh2
- Use QProcess to call an outside SSH process to establish a connection
- Buy the library here: http://netsieben.com/products/ssh/
Since the Java version is free, I'm a bit loathe to spend money on this, but if I have to, I have to. I'd rather not roll my own, because I can only see that being a cause of mistakes (unless those libraries are easy to use?).
Any thing I haven't found?
EDIT: I have now spent days wrestling with both libssh and libqxt. Both are utter failures on windows, from what I can determine. Libqxt won't compile on windows with ssh, even after following the steps described here and here. Libssh's C++ wrapper class includes everything in the header, which is causing linking failures if not carefully shepherded. Once those linker issues are solved, Libssh's compiled library crashes CDB, so debugging with libssh becomes impossible, regardless of whether or not the c++ wrapper is used. So now I'm back to libssh2 or paying for it via some dubious program that has apparently not been updated in four years. Cryptlib, maybe? It doesn't help that most of the qt forum help seems to be 'spawn another process', which therefore requires my users to have ssh installed, which is definitely not par for the course on Windows.
Here is an asynchronous ssh & scp "socket" I wrote for Qt that is cross platform. This requires libSSH and is not perfect (hides rsa key passing stuff, delivers command responses in single shot instead of via readyRead signals) , but it will most likely work for your needs. In order to get this working for windows this file must be the FIRST file included in your project (so that qt doesn't try and import windows.h before this can import the proper windows socket code).
Header:
Source:
I searched for some sort of solution to this for a couple days and then forgot about the problem. Then today I stumbled across this little gem in the Qt-Creator source Utils::ssh, includes support for SFTP, plain-old SSH, and all sorts of goodies.
Disentangling stuff from Qt-Creator can be a pain, but having gone through this process it amounts to grabbing Botan (one of the other libs in QT-Creator) + Utils
See LibQxt. Look for QxtSsh classes. hg repo using libssh2 internally.
I'm using libssh2 http://www.libssh2.org/ with qt. I'm using it in the easiest way - native sockets. I had no time to convert it to use qt sockets. However it works great. The project description is: http://www.chaosstuff.com/2013/09/gnome-mplayer-remote-with-qt-and-libssh2.html and the source code is in: https://bitbucket.org/nchokoev/qtsshremote