0

I am trying to run a script on a remote system using ssh, when i run the command on the terminal it executes fine but when i embed the command in a shell script it returns an error.

Command 1:

cmd="ssh root@IP \"python /pathtoscript\""
`$cmd` > log.log

for the above command i tried the following suggestions from stack exchange:

 cmd="ssh root@IP \"bash -c 'python /pathtoscript'"
 cmd="ssh root@IP '/user/bin/python /pathtoscript'"

Command 2:

ssh root@IP "ls -1v /path"

Note: All these commands work on the terminal.

Edit: If i run the command on the terminal it executes fine and displays the output Now if the same command is added to a script (.sh), I see the following error: bash: ssh root@IP 'python /pathtoscript' : No such file or directory

  • 1
    What error is returned? Please edit your post to include the exact details. – countermode Feb 8 '17 at 7:54
  • No such file or directory – ShivaPahwa Feb 8 '17 at 8:12
  • 3
    I don't see the use of backquote. You are trying to execute a program named ssh root@IP 'python /pathtoscript' which is probably not what you want. simply run $cmd – Archemar Feb 8 '17 at 10:07
  • @Archemar thanks for your suggestion. I tried issuing the command without back quote but i still am facing the same issue. – ShivaPahwa Feb 9 '17 at 0:04
1

well, i am not a specialist myself but this may help,

when you use ssh command,you virtually enter the other machine "root@IP" as you mentioned

when you are in other machine,how can you execute your local file there,first you need to send your file there ,then only you can execute it there,

now try this

#scp file_you_need_to_send UserName@RemoteHost : Target_location_on_target_system this should copy your script on target system

#ssh UserName@RemoteHost to enter Target system

then exexute your respective script.

:P

  • Thanks for the suggestion, I have the script in the remote machine not the local machine. I found a workaround for now. I dont create a cmd string anymore, i just run the command i.e. $(ssh root@IP "python /pathtoscript") – ShivaPahwa Feb 10 '17 at 1:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.