【发布时间】:2018-03-07 23:50:25
【问题描述】:
我正在维护一个Qt5 程序,它使用QProcess 在Linux 上执行ncftpget。
我正在尝试执行的命令是:
ncftpget -E -Z -u username -p 'a#$b1379' 1.1.1.1 /x/y/temp/ update/file
这只是将文件从服务器传输到本地目录。如果我在 bash 命令行上执行上述命令,它可以正常工作。
但是,当我的Qt5 应用程序使用QProcess 执行相同的命令时,ftp 服务器回复说用户名/密码不正确。当密码不包含任何“特殊”字符时,它可以正常工作。
正如S.M. 所述,QProcess 没有在 shell 中执行命令。所以我假设fork() 和exec() 调用的某个版本。 如何让QProcess 在这里做正确的事情?
我假设问题与密码中的特殊字符有关。当我构建我的QProcess 参数列表时,我特别确保密码被' 包围,以便特殊字符被转义。
代码sn-p:
processFTP.setParent(this->parent());
processFTP.setProcessChannelMode(QProcess::MergedChannels);
// ncftpArgs is a QStringList
// snippet doesn't show complete argument list
ncftpArgs->push_back("-Z");
ncftpArgs->push_back("-u");//username
ncftpArgs->push_back(QString((*phashSettings)[FTPUSERNAME]));
ncftpArgs->push_back("-p");//pw
// password can have special characters when shell executes command.
QString password((*phashSettings)[FTPPASSWORD]);
password.prepend("'");
password.append("'");
ncftpArgs->push_back(password);
ncftpArgs->push_back(QString((*phashSettings)[FTPSERVERIP]));
ncftpArgs->push_back(QString((*phashSettings)[FTPTEMPDIR]));
//beging the ncftpget session
QString sExe = "ncftpget"; //process name
// processFTP is a QProcess object
processFTP.start(sExe, (*ncftpArgs));
bSuccess = processFTP.waitForStarted();
// .... more code to monitor process etc...
代码记录了将传递给processFTP 的命令和参数,一切看起来都正确。
如何正确设置参数并启动QProcess 对象,以便将包含特殊字符的密码参数正确传递给可执行文件ncftpget?
和/或我该如何调试问题?
【问题讨论】:
-
为什么投反对票?