0

I am trying to execute a copy command using Apache Commons API. Below is my effort :

   String privateKey = "/Users/TD/.ssh/id_rsa";
    String currentFile = "/Users/TD/One.txt";
    String target = "root@my.server.com:";

    // Space is present in destination
    String destination="/Leo/Ta/San Diego";

    CommandLine commandLine = new CommandLine("scp");
    commandLine.addArgument("-i");
    commandLine.addArgument(privateKey);
    commandLine.addArgument(currentFile);
    commandLine.addArgument(target + destination);

    System.out.println(commandLine.toString());
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);
    executor.execute(commandLine);

Output :

scp -i /Users/TD/.ssh/id_rsa /Users/TD/One.txt "root@my.server.com:/Leo/Ta/San Diego"

org.apache.commons.exec.ExecuteException: Process exited with an error: 1(Exit value: 1)

Same program works fine with destination folder with no space in it:

String destination="/Leo/Ta/SanJose";

scp -i /Users/TD/.ssh/id_rsa /Users/TD/One.txt root@my.server.com:/Leo/Ta/SanJose

Community
  • 1
  • 1

2 Answers2

0

Instead of constructing the command string, use CommandLine's addArgument method. That will ensure that your command is syntactically correct.

The following code demonstrates it:

    CommandLine commandLine = new CommandLine("scp");
    commandLine.addArgument("-i", false);
    commandLine.addArgument(privateKey, false);
    commandLine.addArgument(currentFile, false);
    commandLine.addArgument(target + destination, false);
Dakshinamurthy Karra
  • 5,353
  • 1
  • 17
  • 28
  • Thanks for response ! I added the argument as mentioned but still getting "org.apache.commons.exec.ExecuteException: Process exited with an error: 1(Exit value: 1) Output now is : scp -i /Users/TD/.ssh/id_rsa /Users/TD/One.txt "root@my.server.com:/Leo/Ta/San\ Diego" –  Aug 26 '15 at 04:41
  • Print out the command and execute from a shell and see what message you get. – Dakshinamurthy Karra Aug 26 '15 at 04:50
  • Printed command : scp -i /Users/TD/.ssh/id_rsa /Users/TD/One.txt "root@my.server.com:/Leo/Ta/San\ Diego" –  Aug 26 '15 at 04:53
  • Concatenation of "target + destination" is appearing with inverted commas and thats creating issue –  Aug 26 '15 at 05:04
  • Did you remove this statement: `destination=destination.replaceAll(" ", "\\\\ ");`? – Dakshinamurthy Karra Aug 26 '15 at 05:05
  • Still same error : org.apache.commons.exec.ExecuteException: Process exited with an error. Although output changed as : scp -i /Users/TD/.ssh/id_rsa /Users/TD/One.txt "root@my.server.com:/Leo/Ta/San Diego" –  Aug 26 '15 at 05:35
  • What is the output when executing the command on a command prompt? – Dakshinamurthy Karra Aug 26 '15 at 05:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87947/discussion-between-kdm-and-techdevsmu). – Dakshinamurthy Karra Aug 26 '15 at 05:39
0
commandLine.addArgument(target + destination,false);

public CommandLine addArguments(String addArguments, boolean handleQuoting)

this worked !!