我有一个输入文件 test.txt 为:
host:dc2000
host:192.168.178.2
我想通过以下方式获取这些机器的所有地址:
grep "host:" /root/test.txt
依此类推,我通过 python 获得命令输出:
import subprocess
file_input='/root/test.txt'
hosts=subprocess.Popen(['grep','"host:"',file_input], stdout= subprocess.PIPE)
print hosts.stdout.read()
但结果是空字符串。
我不知道我遇到了什么问题。你能建议我如何解决吗?
最佳答案
试试看:
import subprocess
hosts = subprocess.check_output("grep 'host:' /root/test.txt", shell=True)
print hosts
关于python - 如何获取 grep 命令的输出(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12809467/