简体   繁体   中英

Qt - execute commands through plink with QProcess

I'm trying to modify a file on a server linux with the following code but it doesn't work.

        QProcess *pProcess = new QProcess(this);
        QProcess *pProcess2 = new QProcess(this);
        pProcess2->setStandardOutputProcess(pProcess);

        QString cmd = QString("plink.exe -ssh %1 -i root.ppk -l root tee /etc/test.txt").arg(strSensorAddress);

        pProcess2->start("echo hello");
        pProcess->start(cmd);

        pProcess->setProcessChannelMode(QProcess::ForwardedChannels);

I was able to do it using a file like this :

    QStringList arguments;
    arguments << "-ssh" << strSensorAddress << "-i" << "root.ppk" << "-l" << "root" << "-m" << sFileName;

    pProcess->setProcessChannelMode(QProcess::MergedChannels);
    pProcess->start("plink.exe", arguments);

With the file containing :

echo hello |  tee /etc/factory_test.txt

But since the content needs to be changed I would to not rely on a file.

I'am able to execute commands when there's only one command needed, for exemple just creating a file like this :

    QString cmd = QString("plink.exe -ssh %1 -i root.ppk -l root  touch test.txt").arg(strSensorAddress);

    pProcess->start(cmd);

But when I need to do 2 commands like echo hello > test.txt , I can't do it

You can only run a process not a command with QProcess. However you can do :

process.start("bash", QStringList() << "-c" << "cat file | grep string");

for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM