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).
6
  • 1
    The code you're running should raise a TypeError (can only concatenate list (not "str") to list) before you run into SSH issues. Commented Mar 3, 2015 at 3:45
  • @jd. I have kept str as list. I forgot to update the str in above code Commented Mar 3, 2015 at 3:46
  • @jd. I have modified the code in update part above Commented Mar 3, 2015 at 3:50
  • ['a']+'b' raises TypeError in Python. Commented Mar 3, 2015 at 9: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. Commented Mar 3, 2015 at 9:59

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

and how can I mention the password with ssh?
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.
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. How can I mention the password with ssh?
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.
|
1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.