1

如何将 QProcess 用于命令行交互式参数,我正在尝试传输文件 usimg scp 提示输入密码

QString program = "c:/temp/pscp.exe";
QStringList arguments;
arguments << "C:/Users/polaris8/Desktop/Test1GB.zip" <<   "Mrigendra@192.168.26.142:/home/";
QPointer<QProcess> myProcess;
myProcess = new QProcess;
connect(myProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(readOutput()));
myProcess->start(program , arguments);

在此之后,命令行询问 Password 如何使用 QProcess 来满足它,我可以通过在我的参数中只为 scp 提供一些选项来克服它,或者我的插槽 readOutput 中的代码应该是什么,它将密码抛出到 Command Line 。任何的意见都将会有帮助。谢谢

4

2 回答 2

1

好像scp没有这样的选项,但是pscp(sftp客户端有)。因此,我将编写类似这样的内容,以根据以下手册页使用该选项扩展您的初始参数:

QString program = "c:/temp/pscp.exe";
QStringList arguments;
arguments << "-pw" << "password" << "C:/Users/polaris8/Desktop/Test1GB.zip" << "Mrigendra@192.168.26.142:/home/";
             ^^^^^^^^^^^^^^^^^^^
QPointer<QProcess> myProcess;
myProcess = new QProcess;
connect(myProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(readOutput()));
myProcess->start(program , arguments);

另外,我鼓励您将QStandardPaths用于像您这样的路径。有关详细信息,请参阅文档:

QStandardPaths::DesktopLocation 0   Returns the user's desktop directory.

因此,您最终可以替换此字符串:

"C:/Users/polaris8/Desktop/Test1GB.zip"

具有以下内容:

QStandardPaths::locate(QStandardPaths::DesktopLocation, "Test1GB.zip")

话虽如此,您可能希望将来考虑使用密钥而不是密码。它会更安全一点,也方便您的应用程序。

于 2014-01-14T07:53:18.783 回答
0

我认为您可以将用户名/密码作为选项传递:

-l user
-pw passwd

所以你的论点应该是这样的:

QStringList arguments;
arguments << "-l" << "Mrigendra" << "-pw" << "Password" <<
             "C:/Users/polaris8/Desktop/Test1GB.zip" <<
             "192.168.26.142:/home/";
于 2014-01-14T08:18:48.463 回答