Rsync, sshpass and QProcess
-
Hi,
I would like to use Rsync in QT. When I run this line in command line, it works:
rsync -vr --rsh="/usr/bin/sshpass -p 'password' ssh -o StrictHostKeyChecking=no -p 22 -l user" 192.168.10.10:/abc/ /def/
So in Qt I do the same:
process->start("rsync -vr --rsh=\"/usr/bin/sshpass -p 'password' ssh -o StrictHostKeyChecking=no -p 22 -l user\" 192.168.10.10:/abc/ /def/", args); // args is empty QStringList()
And this doesn't work.
I find simillar topic:
https://forum.qt.io/topic/127028/unable-to-run-qprocess-with-sshpass-in-windowsSo I try:
process->start("rsync", QStringList()<<"-vr"<<"--rsh=\"/usr/bin/sshpass -p 'password' ssh -o StrictHostKeyChecking=no -p 22 -l user\""<<"192.168.10.10:/abc/"<<"/def/");
( I get output: Rsync: Failed to exec /usr/bin/sshpass -p 'password' ssh -o StrictHostKeyChecking=no -p 22 -l user No such file or directory
And I try something like:
process->start("rsync", QStringList()<<"-vr"<<"--rsh=\"/usr/bin/sshpass"<<"-p"<<"'password'"<<"ssh"<<"-o"<<"StrictHostKeyChecking=no"<<"-p"<<"22"<<"-l"<<"user\""<<"192.168.10.10:/abc/"<<"/def/");
( I get output: unexpected remote args: 192.168.10.10:/abc/ )
-
Hi,
I would like to use Rsync in QT. When I run this line in command line, it works:
rsync -vr --rsh="/usr/bin/sshpass -p 'password' ssh -o StrictHostKeyChecking=no -p 22 -l user" 192.168.10.10:/abc/ /def/
So in Qt I do the same:
process->start("rsync -vr --rsh=\"/usr/bin/sshpass -p 'password' ssh -o StrictHostKeyChecking=no -p 22 -l user\" 192.168.10.10:/abc/ /def/", args); // args is empty QStringList()
And this doesn't work.
I find simillar topic:
https://forum.qt.io/topic/127028/unable-to-run-qprocess-with-sshpass-in-windowsSo I try:
process->start("rsync", QStringList()<<"-vr"<<"--rsh=\"/usr/bin/sshpass -p 'password' ssh -o StrictHostKeyChecking=no -p 22 -l user\""<<"192.168.10.10:/abc/"<<"/def/");
( I get output: Rsync: Failed to exec /usr/bin/sshpass -p 'password' ssh -o StrictHostKeyChecking=no -p 22 -l user No such file or directory
And I try something like:
process->start("rsync", QStringList()<<"-vr"<<"--rsh=\"/usr/bin/sshpass"<<"-p"<<"'password'"<<"ssh"<<"-o"<<"StrictHostKeyChecking=no"<<"-p"<<"22"<<"-l"<<"user\""<<"192.168.10.10:/abc/"<<"/def/");
( I get output: unexpected remote args: 192.168.10.10:/abc/ )
@TomNow99 said in Rsync, sshpass and QProcess:
process->start("rsync -vr --rsh=\"/usr/bin/sshpass -p 'password' ssh -o StrictHostKeyChecking=no -p 22 -l user\" 192.168.10.10:/abc/ /def/", args);
This won't work: nowadays
QProcess::start()
does not accept a single, command-line string to execute, you must pass just the executable as the first parameter and aQStringList
of the arguments as the second parameter.Your second two attempts are along the right lines, but doubtless you have something wrong with the separation/quoting of your arguments.
The whole of your
--rsh="/usr/bin/sshpass -p 'password' ssh -o StrictHostKeyChecking=no -p 22 -l user"
looks like a single command-line argument to me. But I don't think you want the"
s around it, which your command shell was dealing with. I think you want:process->start("rsync", QStringList() << "-vr" << "--rsh=/usr/bin/sshpass -p 'password' ssh -o StrictHostKeyChecking=no -p 22 -l user" << "192.168.10.10:/abc/" << "/def/");
?
-
@JonB It works! Thank you :)
Could you tell me, when QProcess::start() was changed to not accept a single command-line string?
@TomNow99
At 5.6-ish I think. See the current QT_NO_PROCESS_COMBINED_ARGUMENT_START. There is an obsolete void QProcess::start(const QString &command, QIODevice::OpenMode mode = ReadWrite).However, they factored out the internal code which must have been used to split the single-string parameter, so you can now call QStringList QProcess::splitCommand(QStringView command); requires Qt 5.15.
Note that under Linux you can still run a "string" via:
process.start("/bin/sh" /*or /bin/bash*/, QStringList() << "-c" << "echo 'hello' | wc && echo \" This is a long line with \\$PATH=$PATH\" ");
or similar. You are going via
/bin/sh -c "..."
, which has its own quoting rules you must abide by.... We could have done your command that way, but between thesh -c
quoting, the various quotings in your command, and doing it from C++ literals with\
s &"
s it might have been a worse brain-ache than my suggestion you took, which actually hopefully is quite "clean" :)