简体   繁体   English

在python中的system(cmd)命令中形成subprocess.call()语句

[英]form a subprocess.call() statement from system(cmd) command in python

I have a ssh command which I was using in a system() statement but I want to replace it with a subprocess.call() statement. 我有一个在system()语句中使用的ssh命令,但我想用subprocess.call()语句替换它。 My ssh command is: 我的ssh命令是:

cmd ="ssh -i pem-file.pem user@" + hostname + " 'cd /user/home/ && java -cp jar-file.jar com.packg.class -a opt1 -f text-file_" + ts + ".txt'"

system(cmd)

I want to replace above with a subprocess.call() statement as it is giving me some performance issues and I read that subprocess.call() is a much better option to use. 我想用subprocess.call()语句替换上面的内容,因为它给了我一些性能问题,我读到subprocess.call()是一个更好的选择。 I formulated this query but it is not executing: 我制定了此查询,但未执行:

result = subprocess.call(["ssh","-i", "pem-file.pem","user@" + hostname + " 'cd /user/home/ && java -cp jar-file.jar com.packg.class -a opt1 -f text-file_" + ts + ".txt'"])

What is the mistake I am doing and what is the correct syntax? 我在做什么错误,正确的语法是什么?

The function shlex.split() is useful for parsing command line arguments into the proper format. shlex.split()函数可用于将命令行参数解析为正确的格式。 This should resolve your syntax error: 这应该可以解决您的语法错误:

import shlex
import subprocess

cmd ="ssh -i pem-file.pem user@" + hostname + " 'cd /user/home/ && java -cp jar-file.jar com.packg.class -a opt1 -f text-file_" + ts + ".txt'"

result = subprocess.call(shlex.split(cmd))

If that doesn't fix your error, then you can pass subprocess.call the shell=True argument: 如果那不能解决您的错误,那么您可以传递subprocess.call调用shell=True参数:

    import subprocess

cmd ="ssh -i pem-file.pem user@" + hostname + " 'cd /user/home/ && java -cp jar-file.jar com.packg.class -a opt1 -f text-file_" + ts + ".txt'"

result = subprocess.call(cmd, shell=True)

Using the shell argument will cause your command to be executed through a shell, rather than having the interpreter parse it. 使用shell参数将使您的命令通过shell执行,而不是由解释器解析它。 However, don't use the shell option if cmd can ever come from an untrusted source. 但是,如果cmd可能来自不受信任的来源,请不要使用shell选项。 Take at look at the warning in the Python docs . 看一下Python文档中的警告。

One more note: 还有一点注意事项:

subprocess.system() is newer and more flexible than os.system() , but don't worry too much about "upgrading" to the new function. subprocess.system()是较新的,比更灵活的os.system()但不担心“升级”到新的功能太多。 The advantages of subprocess.call() are in the more flexible options for communicating with your subprocess. subprocess.call()的优点在于与subprocess.call()进行通信的更灵活的选项。 If all you're doing is executing a single command and getting the return code, os.system() is probably fine. 如果您要做的只是执行一个命令并获取返回代码,则os.system()可能很好。 If you're finding that your command is being flaky and unreliable, switching to subprocess.call() probably isn't going to help much. 如果发现命令不稳定且不可靠,则切换到subprocess.call()可能不会有太大帮助。

Assuming there are no shell meta-characters in hostname (likely), your command could look like this: each command-line argument is a separate list item: 假设hostname中没有外壳元字符(可能),您的命令应如下所示:每个命令行参数都是一个单独的列表项:

#!/usr/bin/env python
import subprocess

cmd = ["ssh", "-i", "pem-file.pem",  "user@" + hostname,
       "cd /user/home/ && java -cp jar-file.jar com.packg.class -a opt1 "
       "-f text-file_" + ts + ".txt"]
subprocess.check_call(cmd)

Unlike os.system() ; 不像os.system() ; it doesn't run the (local) shell. 它不运行(本地)shell。

You could get the argument list using shlex.split(your_original_system_command) ( mentioned by @skrrgwasme ) but shlex.split() can be fooled and therefore it is mostly useful as a hint on how the result should look like. 您可以使用shlex.split(your_original_system_command)由@skrrgwasme提及 shlex.split(your_original_system_command)来获取参数列表,但是shlex.split()可以被欺骗,因此它对于提示结果的外观非常有用。

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

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