2

I am trying to execute a python script through subprocess. First I tried to execute the python script present on my local machine through following code:

str = 'abc'
sub_result = subprocess.Popen([sys.executable,"./script.py"] + str)

this worked correctly. Now I am trying to execute the script present on remote machine through subprocess. First I couldn't find any example on how to do it through subprocess.Popen() like I did for local machine. I then tried to use subprocess.call() in the following code but I am having issues with it:

str = 'abc'
sub_result = subprocess.call('ssh username@hostname python ./script.py' + str)

I get following error:

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 524, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1308, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

What is the mistake I am doing and also how can I mention the password in the ssh command for connection?

UPDATE: Based on @musiphil's answer I modified my code and no I am using:

str = 'abc'
sub_result = subprocess.call(['ssh', 'username@hostname', 'python', './script.py'] + str)

But I get this error:

    ssh_askpass: exec(/usr/libexec/ssh-askpass): No such file or directory
Permission denied, please try again.
ssh_askpass: exec(/usr/libexec/ssh-askpass): No such file or directory
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
user2966197
  • 2,793
  • 10
  • 45
  • 77
  • 1
    The code you're running should raise a TypeError (can only concatenate list (not "str") to list) before you run into SSH issues. – jd. Mar 03 '15 at 03:45
  • @jd. I have kept `str` as list. I forgot to update the str in above code – user2966197 Mar 03 '15 at 03:46
  • @jd. I have modified the code in update part above – user2966197 Mar 03 '15 at 03:50
  • `['a']+'b'` raises TypeError in Python. – jfs Mar 03 '15 at 09:56
  • keep one issue per question. There are two issues: incorrect `subprocess` usage and how to authenticate via `ssh`. @musiphil answered the former. Ask a separate question for the latter. – jfs Mar 03 '15 at 09:59
  • related: [OSError: \[Errno 2\] No such file or directory while using python subprocess](http://stackoverflow.com/q/18962785/4279) – jfs Mar 03 '15 at 10:04

2 Answers2

3

You should give an argv-style list, unless you also specify shell=True:

str = 'abc'
sub_result = subprocess.call(
    ['ssh', 'username@hostname', 'python', './script.py', str])

See https://docs.python.org/2/library/subprocess.html#subprocess.call.

jd.
  • 10,678
  • 3
  • 46
  • 55
musiphil
  • 3,837
  • 2
  • 20
  • 26
  • and how can I mention the password with ssh? – user2966197 Mar 03 '15 at 01:47
  • I got following error when I tried to execute your above code : `ssh_askpass: exec(/usr/libexec/ssh-askpass): No such file or directory Host key verification failed.` – user2966197 Mar 03 '15 at 01:58
  • Can you connect to the remote host by running SSH manually? In any case the subprocess approach is going to be tedious to get working. You could try `paramiko` which implements the SSH protocol in Python. – jd. Mar 03 '15 at 03:41
  • @jd. How can I mention the password with ssh? – user2966197 Mar 03 '15 at 03:44
  • You didn't answer my previous question. Before sending a password you need to authenticate the remote host (this uses your known_hosts file). Then passing the password is going to be quite complex, check the SSH client documentation. I mentionned `paramiko`, this is going to be way easier to use than the SSH command-line client. – jd. Mar 03 '15 at 03:51
  • @jd. I have to use `subprocess` as it is the requirement of system and also for uniformity across application. – user2966197 Mar 03 '15 at 03:53
  • @jd. I am able to ssh to the server directly from terminal – user2966197 Mar 03 '15 at 04:16
1

You can mention password on ssh with the help of sshpass command.

    ['sshpass', '-p', 'password', 'ssh','username@%s' % address]