来自 Qt 的 ssh 命令 [英] Ssh command from Qt

查看:157
本文介绍了来自 Qt 的 ssh 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Qt 应用程序中的 ssh 有问题.我需要运行一个命令来删除远程服务器上的文件.我尝试使用 QProcess 类来实现它.所以我将ssh"作为命令和必要参数的名称传递.通常它看起来像这样:

I have a problem with ssh in my Qt application. I need to run a command which removes a file on a remote server. I tried to use a QProcess class to achieve it. So I pass "ssh" as a name of command and necessary args. In common it looks like this:

QStringList params;    
params.append(" user@" + ::host +
              " \"rm /tmp/" + ::fileName + "\"");
d->impDelProcess->start("ssh", params);

但毕竟它一直在询问密码,尽管我生成了 ssh 密钥并将公钥复制到远程服务器.此外,当我像这样在终端中运行上面的命令时:

But after all it keeps asking a password, though I generated ssh keys and copied a public key to the remote server. Moreover, when I run the command above in the terminal like this:

ssh user@host "rm /path/fileName"

它完美无缺.文件被删除,不需要密码.所以,问题出在 QProcess 的某个地方.有没有办法摆脱询问密码?谢谢!

it works perfect. The file is deleted and no password is asked. So, the problem is somwhere in QProcess. Is any way to get rid of asking a password? Thank you!

推荐答案

当您使用字符串列表时,这些是单独的参数.试试这个:

Those are separate arguments, when you use string list. Try this:

params.append("user@" + ::host");
params.append("rm /tmp/" + ::fileName);

这将使 Qt 为 ssh 传递两个参数,登录字符串和要在远程主机上执行的命令.

That will make Qt pass two arguments for ssh, the login string and the command to execute at remote host.

重要提示! 以上假设 ::filename 不包含空格或其他任何令人讨厌的东西!.你可以用这个绕过空间:

Important note! Above assumes ::filename does not contain spaces or anything else nasty!. You can get around spaces with this:

params.append("rm '/tmp/" + ::fileName + "'");

但它不会帮助对抗通配符或路径中的 ..... 想象一下,例如,如果文件名是 ../home/user/*... 所以最好是可信输入,或者您需要对其进行消毒(但这超出了本答案的范围).

But it won't help against wild cards or .. in path... Imagine if file name was ../home/user/* for example... So that is better be trusted input, or you need to sanitize it (but that is beyond scope of this answer).

你在问题​​代码中所做的是构造一个参数,相当于这个shell命令行:

What you do in the question code is to construct a single argument, equivalent to this shell command line:

ssh 'user@host "rm /path/filename"'

这篇关于来自 Qt 的 ssh 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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