2

I can execute a script from python environment locally using subprocess but due to cross platform issues, I have to execute it on a remote server and get back the results on my local machine.

The directory parserpath contains some third party modules that can be executed using a script run.sh present in parserpath directory. However this parserpath directory is present on a remote server. This is what I have, but this will work only if parserpath is a local directory. How can I ssh to a remote directory and run the script run.sh?

def run_parser(filename):

     current_dir = os.getcwd()
     parser_path="/parserpath"
     os.chdir(parser_path)
     subprocess.call("./run.sh " + filename, shell=True)
     os.chdir(current_dir)

3 Answers 3

4

With most linux shells, you can run a command in a different working directory by executing a subshell as in

/home/usr> (cd /usr/local/bin;pwd)
/usr/local/bin
/home/usr>

You can do the same thing through ssh to the remote system. Depending on which ssh client you use, you may thin that up a bit. For instance, with paramikos exec_command, a new remote shell is created for each command so cd /path/on/remote/machine;./run.sh is sufficient.

A minimalist example for paramiko on python 2.x is

import sys
import paramiko

try:
    hostname, username, password, targetpath = sys.argv[1:5]
except ValueError:
    print("Failed, call with hostname username password targetpath")

command = "cd {};pwd".format(targetpath)
print("Command to send: {}".format(command))

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=hostname, username=username, password=password)
stdin, stdout, stderr = ssh.exec_command("cd {};pwd".format(targetpath))
print(stdout.read())
ssh.close()

python3 should be similar. There are other options like libssh2 bindings for python, pexpects ssh support and etc...

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

Comments

1

Use SSH keys to automate the process of logging in via SSH. Here is the following code to execute a script remotely.

ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND],
                   shell=False,
                   stdout=subprocess.PIPE,
                   stderr=subprocess.PIPE)

2 Comments

but I need to change the working directory to a directory in the remote machine. How can I do that?
For the purpose of simplicity, I would just execute the strict in your home directory. However, if you do not want to do that, I suggest passing the absolute path of the script you want to run, that negates the need to change directories. Look at this link for more direction: gist.github.com/bortzmeyer/1284249. The libraries required for managing an active SSH connection are needlessly complex for your specific use case.
0

Try, ssh user@host sh path/run.sh

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.