简体   繁体   中英

Running a local python script with args on a remote host

I got a python script from which I use ssh to open a connection to a different host. During this session I'm trying to run another python script, with several arguments (the script is on my machine and not on the server) but the arguments are messing it up. It links the arguments to the python interpreter rather than my script.

My code looks like this:

ssh remotehost python2 < /path/to/script —-arg1 hello —-arg2 goodbye

And the error I'm getting is unknown option per each character in the argument's name, for example: Unknown option: —-

Unknown option:a

Unknown option:r

etc.. anyway I can pass arguments without getting this error? I tried using sys args but my arguments are sentences sometimes and it doesn't parse it well..

To execute Scripts on a different server use paramiko to connect and to execute commands:

    Import paramiko
    
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(host,port,user,password)
    
    stdin, stdout, stderr = client.exec_command('python2 /path/to/script hello goodbye') 

    client.close()

Full doc: http://docs.paramiko.org/en/stable/api/client.html

to run a local script on a different server use:

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

see Run local python script on remote server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM