Run local python script on remote server

128,153

Solution 1

It is possible using ssh. Python accepts hyphen(-) as argument to execute the standard input,

cat hello.py | ssh [email protected] python -

Run python --help for more info.

Solution 2

Although this question isn't quite new and an answer was already chosen, I would like to share another nice approach.

Using the paramiko library - a pure python implementation of SSH2 - your python script can connect to a remote host via SSH, copy itself (!) to that host and then execute that copy on the remote host. Stdin, stdout and stderr of the remote process will be available on your local running script. So this solution is pretty much independent of an IDE.

On my local machine, I run the script with a cmd-line parameter 'deploy', which triggers the remote execution. Without such a parameter, the actual code intended for the remote host is run.

import sys
import os

def main():
    print os.name

if __name__ == '__main__':
    try:
        if sys.argv[1] == 'deploy':
            import paramiko

            # Connect to remote host
            client = paramiko.SSHClient()
            client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            client.connect('remote_hostname_or_IP', username='john', password='secret')

            # Setup sftp connection and transmit this script
            sftp = client.open_sftp()
            sftp.put(__file__, '/tmp/myscript.py')
            sftp.close()

            # Run the transmitted script remotely without args and show its output.
            # SSHClient.exec_command() returns the tuple (stdin,stdout,stderr)
            stdout = client.exec_command('python /tmp/myscript.py')[1]
            for line in stdout:
                # Process each line in the remote output
                print line

            client.close()
            sys.exit(0)
    except IndexError:
        pass

    # No cmd-line args provided, run script normally
    main()

Exception handling is left out to simplify this example. In projects with multiple script files you will probably have to put all those files (and other dependencies) on the remote host.

Solution 3

ssh user@machine python < script.py - arg1 arg2

Because cat | is usually not necessary

Solution 4

You can do it via ssh.

ssh [email protected] "python ./hello.py"

You can also edit the script in ssh using a textual editor or X11 forwarding.

Share:
128,153
qweruiop
Author by

qweruiop

Less is more.

Updated on July 09, 2022

Comments

  • qweruiop
    qweruiop almost 2 years

    I'm debugging some python script that must run on my virtual machine. And, I prefer to edit the scripts locally(outside of virtual machines). So I find it's tedious to scp modified scripts to virtual machines every time. Can anyone suggests some effective way?

    Particularly, I'm wondering if it's possible to execute python scripts on remote PVM. Something like that:

    python --remote [email protected] hello.py //**FAKED**, served to explain ONLY
    
  • Sney
    Sney about 10 years
    This executes a remote python script on the remote server. As I understand the author wants to execute a local python script on a remote server.
  • smeso
    smeso about 10 years
    Yes, you are right. I was suggesting to do the edits via ssh too, directly on the remote machine, to avoid the "upload stage". Actually you can upload and execute the file in one line (e.g. unix.stackexchange.com/a/57809). But that solution looks a bit dirty to me...
  • DiCaprio
    DiCaprio over 8 years
    can I use this construct also as with open paramiko.SSHClien() ... in order to avoid forgetting the close()?
  • Jim M.
    Jim M. almost 8 years
    Check that len(sys.argv) > 1, otherwise the script crashes on the remote. Also, your bashrc (or shell initial configuration) on the remote must not contain any prints on stdout (echo in bash).
  • Igor Markelov
    Igor Markelov over 7 years
    what if the code has submodules located in the subfolders?
  • Stoopkid
    Stoopkid over 7 years
    @Pyaping cat hello.py | ssh [email protected] python - arg1 arg2 arg3 works for me
  • neric
    neric over 6 years
    If you are using cat a_single_file | then you are doing it wrong
  • KRoy
    KRoy almost 5 years
    It won't work if that python includes other python modules locally .