简体   繁体   English

在远程主机上运行带有 args 的本地 python 脚本

[英]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.我得到了一个 python 脚本,我从中使用 ssh 打开到不同主机的连接。 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.在这个会话期间,我试图运行另一个 python 脚本,有几个参数(脚本在我的机器上而不是在服务器上)但是参数把它搞砸了。 It links the arguments to the python interpreter rather than my script.它将参数链接到 python 解释器而不是我的脚本。

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未知选项:a

Unknown option:r未知选项: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..我尝试使用 sys args 但我的参数有时是句子并且它不能很好地解析它..

To execute Scripts on a different server use paramiko to connect and to execute commands:要在不同的服务器上执行脚本,请使用 paramiko 连接并执行命令:

    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完整文档: 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请参阅在远程服务器上运行本地 python 脚本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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