如何在Qt中轻松建立SSH连接?
我正在寻找一种在 Qt 中建立 ssh 连接的简单方法,类似于我在 Java 中的操作方式。
例如,要通过 java 登录 ssh 连接,我可以这样做:
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();
}
我见过的 Qt 解决方案是:
- 使用 libssh 自行部署
- 使用 libssh2 自行部署
- 使用 QProcess 调用外部 SSH 进程来建立
- 连接库在这里: http://netsieben.com/products/ssh/
因为 Java 版本是免费的,我有点舍不得在这上面花钱,但如果必须的话,我也必须这么做。我不想自己推出,因为我只能看到这是错误的原因(除非这些库很容易使用?)。
有什么我没发现的吗?
编辑:我现在花了几天时间与 libssh 和 libqxt 进行斗争。据我所知,两者在 Windows 上都是完全失败的。即使按照此处描述的步骤操作,Libqxt 也不会在使用 ssh 的 Windows 上进行编译 和此处。 Libssh 的 C++ 包装类包含标头中的所有内容,如果不仔细管理,这会导致链接失败。一旦这些链接器问题得到解决,Libssh 的编译库就会使 CDB 崩溃,因此无论是否使用 c++ 包装器,都无法使用 libssh 进行调试。所以现在我又回到了 libssh2 或者通过一些可疑的程序来支付它,而这些程序显然已经四年没有更新了。 Cryptlib,也许吧?大多数 qt 论坛帮助似乎是“生成另一个进程”,因此要求我的用户安装 ssh,这对 Windows 上的课程来说绝对不符合标准。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个异步 ssh & scp“socket”是我为Qt编写的跨平台的。这需要 libSSH 并且并不完美(隐藏 rsa 密钥传递内容,以单次方式而不是通过 read 信号传递命令响应),但它很可能会满足您的需求。为了使其适用于 Windows,此文件必须是项目中包含的第一个文件(以便 qt 在导入正确的 Windows 套接字代码之前不会尝试导入 windows.h)。
标题:
来源:
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:
我花了几天时间寻找某种解决方案,然后忘记了这个问题。然后今天我在 Qt-Creator 源代码中偶然发现了这个小宝石 Utils::ssh,包括对 SFTP、普通 SSH 和各种好东西的支持。
从 Qt-Creator 中解开一些东西可能会很痛苦,但是经历了这个过程就相当于获取了 Botan(QT-Creator 中的其他库之一)+ Utils
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
请参阅 LibQxt。查找 QxtSsh 类。 hg repo 在内部使用 libssh2。
See LibQxt. Look for QxtSsh classes. hg repo using libssh2 internally.
我正在使用 libssh2 http://www.libssh2.org/ 和 qt 。我以最简单的方式使用它 - 本机套接字。我没有时间将其转换为使用 qt 套接字。不过效果很好。
项目描述是:
http://www.chaosstuff.com /2013/09/gnome-mplayer-remote-with-qt-and-libssh2.html
源代码位于:
https://bitbucket.org/nchokoev/qtsshremote
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