gpt4 book ai didi

Python SSH 命令

转载 作者:太空宇宙 更新时间:2023-11-04 08:58:58 24 4
gpt4 key购买 nike

我有以下代码:

def executeRemoteCommand(host, command):
cmd = "ssh " + host + " \'" + command + "\'"
print cmd
subprocess.check_call(cmd)

当我运行命令时:

java -Xss515m -Xms48g -Xmx48g -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -jar /local/experiments/helloworld/ro/server.jar /local/experiments/helloworld/ro/properties.json

使用上面的函数,我得到以下错误

File "./util/ssh_util.py", line 85, in executeRemoteCommand
subprocess.check_call(cmd)
File "/usr/lib/python2.7/subprocess.py", line 506, in check_call
retcode = call(*popenargs, **kwargs)
File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory.

但是,当我直接在命令行中输入调用时,它工作正常。

ssh foo92 'java -Xss515m -Xms48g -Xmx48g -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -jar /local/experiments/helloworld/ro/server.jar /local/experiments/helloworld/ro/properties.json'

有人知道吗?

最佳答案

您需要设置 shell=True 以通过 shell 执行该命令:

subprocess.check_call(cmd, shell=True)

如果您将参数作为列表传递,您可以直接执行它,而不是通过 shell 推送它;这样你就不必担心引用命令:

def executeRemoteCommand(host, command):
subprocess.check_call(['ssh', host, command])

请注意,此处的 hostcommand 都是传递给 ssh单个参数。通常,这就是 shell 将参数传递给 ssh 命令的方式,这就是 java ... 命令行周围的引号 for.

关于Python SSH 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27456898/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com