简体   繁体   English

从本地python脚本运行远程python脚本并传递一些参数

[英]Run a remote python script from a local python script and pass some arguments

I have a remote python script which has the following code in it: 我有一个remote python script ,其中包含以下代码:

    from os import system
    import sys


    if __name__ == '__main__':

        arg1 = sys.argv[1] // ip address of host
        arg2 = sys.argv[2]

        cmd_txt = "ssh -i pem-file user@" + arg1 + " 'cd /folder1/folder2/ && java -cp jar-file.jar pack1.pack2.py-file -f folder1/folder2/file_" + ts + ".txt'"
        system(cmd_txt)

Now I want to run this remote python script from a python script which is present on my local machine alongwith passing the two arguments to it - arg1 & arg2 . 现在,我想从本地计算机上存在的python script运行此remote python script ,并将两个参数arg1arg2传递给它。 So basically my local python script will make a call to remote python script and pass the two arguments to it. 因此,基本上我的local python script将调用remote python script ,并将两个参数传递给它。 I know how to execute a local python script from another python script but not the remote python script. 我知道如何从另一个python脚本执行本地python脚本,但不能从远程python脚本执行。 How can I do this? 我怎样才能做到这一点?

UPDATE: I executed this script from my local machine which makes a call to remote python script and passes two arguments. 更新:我从本地计算机执行了此脚本,该脚本调用了远程python脚本并传递了两个参数。 When I run this script I get Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password) error: 当我运行此脚本时,出现Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password)错误:

host = [host-ip] #host_ip in string format
args1 = 'str1'
args2 = 'str2'

cmd_txt = "sudo ssh -i pem-file user@" + host + " 'cd folder1/folder2/ && python file1.py "+ args1 +" "+ args2 +"'"
system(cmd_txt)

What am I missing in my code above? 我上面的代码中缺少什么?

Prerequisite: SSH and key sync (the client's public key should be in the server's authorized_keys file). 先决条件:SSH和密钥同步(客户端的公共密钥应在服务器的authorized_keys文件中)。

I see that your local command starts with sudo . 我看到您的本地命令以sudo开头。 Yet the .pem file will provide the same access to the server regardless if you're connecting from a root or standard account on your local system (different situation if you weren't using the .pem file but the default ${HOME}/.ssh/id_rsa ). 但是,无论您是从本地系统上的根帐户还是标准帐户进行连接,.pem文件都将提供对服务器的相同访问权限(如果您不使用.pem文件,而是使用默认${HOME}/.ssh/id_rsa )。 I recommend making sure the normal local user can make outgoing ssh connections and dropping the sudo . 我建议确保普通本地用户可以建立传出ssh连接并删除sudo

Take a look at the Fabric package (pip install fabric). 看一下Fabric软件包(pip install fabric)。 It really rocks at what you want to achieve. 您想要实现的目标确实很困难。 The big advantage is that Fabric provides a good abstraction over the os or subprocess statements and allows you to run server-side commands from within the client script, ie 最大的优点是,Fabric提供了对os或子流程语句的良好抽象,并允许您从客户端脚本中运行服务器端命令,即

from fabric.api import put, run
from fabric.contrib.files import exists

try:
    with cd("/folder1/folder2"):
        put("file1.py")
        run("python file1.py %s" % " ".join([args1, args2, ...]))
except:
    # handle exceptions gracefully on client side
finally:
    try:
        if exists("/folder1/folder2/file1.py"):
            run ("rm -f /folder1/folder2/file1.py")
    except:
        pass

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

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