2

I have the script below (test.py on 1.1.1.1) to run another remote script on another server (script.py on 2.2.2.2). I have set up the ssh keys so I don't get prompted for password.

import subprocess
USER="user"
SERVER_IP="2.2.2.2"
SCRIPT_PATH="/home/abc/script.py"
print ("ssh {0}@{1} '/usr/bin/python {2} aaa bbb'".format(USER, SERVER_IP, SCRIPT_PATH))
rc = subprocess.check_output("ssh {0}@{1} '/usr/bin/python {2} aaa bbb'".format(USER, SERVER_IP, SCRIPT_PATH))

script.py itself is on 1.2.3.4, and takes in 2 arguments.

If I copy the command that is printed out in the script, I can execute script.py successfully on 1.1.1.1. But running test.py on 1.1.1.1 gives me an error:

OSError: [Errno 2] No such file or directory

I don't understand why the script didn't work but the exact same command works on its own.

asked Jul 4, 2018 at 6:59

2 Answers 2

1

Use the additional argument:

shell=True

Your command will be:

rc = subprocess.check_output("ssh {0}@{1} '/usr/bin/python {2} aaa bbb'".format(USER, SERVER_IP, SCRIPT_PATH),shell=True)

I assume you need a shell to run a python script.

answered Jul 4, 2018 at 7:16
Sign up to request clarification or add additional context in comments.

Comments

1

If your question is to address the need of executing a remote command and not making your script working - then if I could introduce Paramiko:

import paramiko
ssh_handle = paramiko.SSHClient()
ssh_handle.load_system_host_keys()
ssh_handle.connect(
 hostname=address,
 port=int(port),
 username=login)
stdin, stdout, stderr = ssh_handle.exec_command("whoami")

IMO it's currently the most "usable" SSH library and works just fine in my projects.

answered Jul 4, 2018 at 7:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.