1

How can I use QProcess for Command Line Interactive arguments, I am trying to a transfer a file usimg scp which prompts for password

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);

After this the commnad Line asks for Password how to satisfy it using QProcess , can I overcome it by giving some options in my arguments only for scp, or what should be the code in my slot readOutput that throws the password to Command Line . Any suggestions would be helpful. Thanks

László Papp
  • 51,870
  • 39
  • 111
  • 135
user3110438
  • 95
  • 1
  • 7

2 Answers2

1

It seems that scp does not have such options, but pscp (sftp client does have). So, I would be writing something like this to extend your initial arguments with that option based on the following man page:

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);

Also, I would encourage you to use QStandardPaths for a path like yours. See the documentation for details:

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

So, you could eventually replace this string:

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

with the following:

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

That being said, you may wish to consider using keys instead of password in the future. It would be a bit more secure, and also convenient for your application.

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • ordering matters,as the order you stated prompts for password and even QProcess fails..arguments << "C:/Users/polaris8/Desktop/Test1GB.zip" << "-pw" << "password" << "Mrigendra@192.168.26.142:/home/"; works fine in windows command Line but Qproces fails .....Am I making Qt unhappy any where in this statement – user3110438 Jan 14 '14 at 09:09
  • Correct order is arguments << "-pw" << "polaris" << "C:/Users/polaris8/Desktop/ToDo.txt" << "Mrigendra@192.168.26.142:/home/";...It works Thanks Laszlo and @ThorKhan – user3110438 Jan 14 '14 at 09:22
  • @user3110438: Also, as per my answer and hint about QStandardPaths, please do not use hard coded paths in your code. – László Papp Jan 14 '14 at 09:47
  • will keep a look on QStandardPaths..Thanks for pointing it out...Laszlo is there a way to get rid of username and passwords and just use keys using pscp ? – user3110438 Jan 14 '14 at 09:58
  • @user3110438: http://dafis.ucdavis.edu/install/sftp/PuttyPSCPKey.cfm Does that help? – László Papp Jan 14 '14 at 10:13
0

I think you can pass the username / password as options with:

-l user
-pw passwd

So your arguments should look like this:

QStringList arguments;
arguments << "-l" << "Mrigendra" << "-pw" << "Password" <<
             "C:/Users/polaris8/Desktop/Test1GB.zip" <<
             "192.168.26.142:/home/";
ThorKhan
  • 58
  • 8
  • Fwiw, the QProcess documentation prefers the `QStringList arguments; arguments << "-style" << "fusion";` style. I think, in the Qt world, "-pw Password" are two arguments rather than one. At least terminology and logically, but your code may work. – László Papp Jan 14 '14 at 08:46
  • @ThorKhan , Thanks but its not working with QProcess but works well in Windows Command Prompt.... – user3110438 Jan 14 '14 at 08:54
  • @user3110438: for the reason, I mentioned. You would need to split "-pw password" into separate arguments as per my answer. – László Papp Jan 14 '14 at 09:00
  • Strictly speaking, there is still some room for improvement since there is no such a type as `StringList`. – László Papp Jan 14 '14 at 09:39