简体   繁体   English

Python脚本执行外部命令

[英]Python script to execute external commands

I need to issue some commands to the currently executing program and get the output of the current (remotely) executing program into some string in the script. 我需要向当前正在执行的程序发出一些命令,并将当前(远程)正在执行的程序的输出输出到脚本中的某些字符串中。 The problem I am currently encountering is that I don't know the output of each command which output can also vary as it can be read from user. 我目前遇到的问题是我不知道每个命令的输出,因为可以从用户那里读取它的输出也会有所不同。

eg 例如

  1. ./my_program ./my_program
  2. print_output_1 [ with in my_program ] print_output_1 [在my_program中使用 ]
  3. print_output_2 [ with in my_program ] print_output_2 [在my_program中使用 ]
  4. exit [ with in my_program ] 退出[与my_program中的一起 ]

and if i run the commands manually terminal will get something like this 如果我手动运行命令,终端将显示以下内容

bash$ ./my_programe
my_program: print_output_1
my_program:first_line_printed_with_unknown_length
my_program: print_output_2
my_program:second_line_printed_with_unknown_length
my_program: exit
bash$

so i should get "first_line_printed_with_unknown_length" and "second_line_printed_with_unknown_length" in a python string like 所以我应该在像这样的python字符串中获得“ first_line_printed_with_unknown_length”和“ second_line_printed_with_unknown_length”

execute(my_program)
str1 = execute( print_output_1 )
str2 = execute( print_output_2 )
val = execute( exit )

You can make use of the subprocess module to execute external commands. 您可以使用子流程模块执行外部命令。 It is best to start with much simpler commands first to get the gist of it all. 最好先从简单得多的命令入手,以掌握所有内容。 Below is a dummy example: 下面是一个虚拟的示例:

import subprocess
from subprocess import PIPE

def main():
    process = subprocess.Popen('echo %USERNAME%', stdout=PIPE, shell=True)
    username = process.communicate()[0]
    print username #prints the username of the account you're logged in as

if __name__ == '__main__':
    main()

This will grab the output from echo %USERNAME% and store it. 这将从echo %USERNAME%获取输出并将其存储。 Pretty simply to give you the general idea. 只是为了给您大致的思路。

From the aforementioned documentation: 从上述文档中:

Warning: Using shell=True can be a security hazard. 警告:使用shell = True可能存在安全隐患。 See the warning under Frequently Used Arguments for details. 有关详细信息,请参见“常用参数”下的警告。

It is possible to do with ssh (ie the ssh command), and ssh, like any shell-executable command can be wrapped in Python, so the answer is yes. 可以使用ssh(即ssh命令)进行操作,并且ssh可以像任何shell可执行命令一样包装在Python中,因此答案是肯定的。 Something like this could work (I did not try though): 这样的事情可能会起作用(我没有尝试):

import subprocess

remote_command = ['ls', '-l']
ssh_command = ['ssh', 'user@hostname.com'] + remote_command
proc = subprocess.Popen(ssh_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()

# stdout now contains the output of the remote command
# stderr now contains the error stream from the remote command or from ssh

You can also use Paramiko which is an ssh implementation in Python, but that might be overkill if you do not need interactivity. 您还可以使用Paramiko,它是Python中的ssh实现,但是如果不需要交互性,则可能会过高。

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

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