Python 执行外部命令的脚本
Python script to execute external commands
我需要向当前正在执行的程序发出一些命令,并将当前(远程)正在执行的程序的输出获取到脚本中的某个字符串中。我目前遇到的问题是我不知道每个命令的输出哪个输出也可以变化,因为它可以从用户那里读取。
例如
- ./my_program
- print_output_1 [ 与 my_program ]
- print_output_2 [在my_program]
- 退出[与my_program]
如果我 运行 手动终端命令将得到类似这样的东西
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$
所以我应该在 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 )
可以使用 ssh(即 ssh
命令)和 ssh,就像任何 shell- 可执行命令一样可以包装在 Python 中,所以答案是是的。像这样的东西可以工作(虽然我没有尝试):
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
您还可以使用 Paramiko,它是 Python 中的一个 ssh 实现,但如果您不需要交互性,那可能就太过分了。
您可以使用subprocess模块来执行外部命令。最好先从更简单的命令开始,以了解所有内容的要点。下面是一个虚拟示例:
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()
这将从 echo %USERNAME%
获取输出并存储它。很简单地给你一个大概的想法。
来自上述文档:
Warning: Using shell=True can be a security hazard. See the warning
under Frequently Used Arguments for details.
我需要向当前正在执行的程序发出一些命令,并将当前(远程)正在执行的程序的输出获取到脚本中的某个字符串中。我目前遇到的问题是我不知道每个命令的输出哪个输出也可以变化,因为它可以从用户那里读取。
例如
- ./my_program
- print_output_1 [ 与 my_program ]
- print_output_2 [在my_program]
- 退出[与my_program]
如果我 运行 手动终端命令将得到类似这样的东西
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$
所以我应该在 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 )
可以使用 ssh(即 ssh
命令)和 ssh,就像任何 shell- 可执行命令一样可以包装在 Python 中,所以答案是是的。像这样的东西可以工作(虽然我没有尝试):
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
您还可以使用 Paramiko,它是 Python 中的一个 ssh 实现,但如果您不需要交互性,那可能就太过分了。
您可以使用subprocess模块来执行外部命令。最好先从更简单的命令开始,以了解所有内容的要点。下面是一个虚拟示例:
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()
这将从 echo %USERNAME%
获取输出并存储它。很简单地给你一个大概的想法。
来自上述文档:
Warning: Using shell=True can be a security hazard. See the warning under Frequently Used Arguments for details.