如何检测ssh连接(通过QProcess)何时完成? [英] How to detect when ssh connection (over a QProcess) has finished?

查看:421
本文介绍了如何检测ssh连接(通过QProcess)何时完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 QProcess 从应用程序运行ssh隧道:

  QProcess * process = new QProcess(); 
process-> start(ssh,QStringList()< - L<<27017:localhost:27017<

到目前为止,它工作得很好,唯一的问题是,我没有办法,端口已实际创建。



当我在shell上运行命令时,大约需要10秒钟连接到远程主机,转发的端口准备就绪后可以使用。如何从我的应用程序中检测它?



编辑:



如vahancho所建议的,后连接在终端上有一些输出,可用于检测连接是否成功。然而,有一行在启动后立即运行伪终端将不会被分配,因为stdin不是一个终端,这可能会给出一个假警报。正确的输出在稍后发出的第二信号中可用(这是被打开的端口的真实指示符)。为了摆脱第一条消息,我现在运行ssh使用 ssh -t -t 强制stdin分配。


解决方案


所以,剩下的唯一问题是,任何人可以帮助我没有任何问题在这种方法?


稳定和健壮的解决方案,不幸的是。这是一个破碎的概念处理git输出,而不是使用实际的库。主要的问题是,这些软件没有任何保证输出兼容性,正当地。



想象一下,如果他们有一个不清楚的文本,一个typo, ,未注意。它们固有地需要分别修复输出,并且依赖于输出的所有应用程序将突然中断。



这也是背后使用专用库的原因,功能用于重用,而不是直接与面向用户的输出一起工作。在git的情况下,这意味着libgit2库,例如。



Qt没有一个ssh机制在默认情况下,你可以有这样的库在python,例如。 paramiko。



我建议使用 libssh libssh2 ,因为你也在评论中注意到了自己。我可以理解的不便,这不是一个真正的Qt'ish的方式,现在,但在这一点Qt不能提供任何更强大的没有第三方。



说,很高兴看到一个类似的附加库在Qt项目的未来,但这可能不会发生任何很快。如果你编写你的软件与正确的设计,你将能够切换到这样的库,一旦有人站起来保持这样一个额外的库Qt或其他地方的重大问题。


I am running an ssh tunnel from an application using a QProcess:

QProcess* process = new QProcess();
process->start("ssh", QStringList()<<"-L"<<"27017:localhost:27017"<<"example.com");

So far it works great, the only problem being that there is no way for me to see when the port has actually been created.

When I run the command on a shell, it takes about 10 seconds to connect to the remote host after which the forwarded port is ready for usage. How do I detect it from my application?

EDIT:

As suggested by vahancho, I used the fact that post-connection there is some output on the terminal that can be used to detect that the connection has succeeded. However, there is a line which is run instantly after launch Pseudo-terminal will not be allocated because stdin is not a terminal, which probably would give a false alarm. The correct output is available in the second signal, emitted a bit later (which is a true indicator of the port having being opened). To get rid of the first message, I am now running ssh using ssh -t -t to force an stdin allocation.

So, the only question left is, can anyone help me without any concerns in this approach?

解决方案

So, the only question left is, can anyone help me without any concerns in this approach?

This is not a stable and robust solution, unfortunately. It is similarly a broken concept to handling git outputs rather than using an actual library. The main problem is that these softwares do not have any guarantee for output compatibility, rightfully.

Just imagine that what happens if they have an unclear text, a typo, et all, unnoticed. They inherently need to fix the output respectively, and all the applications relying on the output would abruptly break.

This is also the reason behind working on dedicated libraries giving access to the functionality for reuse rather than working with the user facing output directly. In case of git, this means the libgit2 library, for instance.

Qt does not have an ssh mechanism in place by default like you can have such libraries in python, e.g. paramiko.

I would suggest to establish a way in your code by using libssh or libssh2 as you also noted yourself in the comment. I can understand the inconvenience that is not a truly Qt'ish way as of now, but at this point Qt cannot provide anything more robust without third-party.

That being said, it would be nice to see a similar add-on library in the Qt Project for the future, but this may not be happen any soon. If you write your software with proper design in mind, you will be able to switch to such a library withour major issues once someone stands up to maintain such an additional library to Qt or elsewhere.

这篇关于如何检测ssh连接(通过QProcess)何时完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆