我正在尝试在 Linux 上读取 sftp 命令的输出。我使用 QProcess 从 Qt 应用程序调用 sftp。但是我没有从 read() 方法中获得任何数据,这应该是进程的标准输出。我在开始进程之前将一个插槽连接到readyRead
信号,所以我不应该错过进程的任何输出。
Windows 上的同一个应用程序,使用 PuTTY 的 psftp,工作正常,我得到了我需要的所有数据。我想在 Linux 上也一样。
在我启动进程 ( sftp user@host
) 后,我在终端上从 sftp 获得密码提示,但在标准输出中没有。根据ldd,sftp使用libncurses,我认为这是问题所在。有没有办法在不使用 ncurses 的情况下将 sftp 的所有输出重定向到标准输出?
示例代码:
#include <QObject>
#include <QProcess>
class MyClass : public QObject {
Q_OBJECT
public:
MyClass(QObject *parent = 0) : QObject(parent) {
connect(&prc, SIGNAL(readyRead()), this, SLOT(read()));
prc.start("sftp user@host"); // valid user and host in the actual project
}
public slots:
void read() {
qDebug("bytes to read: %d", (int)prc.bytesAvailable());
QString data = prc.readAll();
qDebug("readAll: %s", qPrintable(data));
data = prc.readAllStandardOutput();
qDebug("readAllStandardOutput: %s", qPrintable(data));
data = prc.readAllStandardError();
qDebug("readAllStandardError: %s", qPrintable(data));
}
protected:
QProcess prc;
};
问题只存在于sftp
, 对于其他命令,如ls
or pwd
(linux bash ls/pwd,而不是 ftp ls/pwd)它有效。