我正在创建一个用于运行linux命令的Python脚本,该命令是“ snmpwalk -v2c -c aL1walsh0aL <> ifInError”
我需要为hostname.txt文件中的多个主机IP执行命令。
当我运行下面的python脚本时,我得到“ snmpwalk -v2c -c aL1walsh0aL”的输出,并且主机名的错误未定义。 所以基本上我的完整命令没有执行,这给了我正确的输出。
但是我需要输出例如:snmpwalk -v2c -c aL1walsh0aL 10.0.0.1 ifInError
我的python脚本:
import subprocess
with open("hostname.txt","rw") as f:
for line in f.readlines():
subprocess.Popen(['snmpwalk', '-v2c','-c',line],stdout=subprocess.PIPE)
f.close()
答案 0 :(得分:0)
import subprocess
with open("mac.txt","rw") as f: # mac.txt is the text file with mac address
for line in f.readlines():
print (line)
print ("************")
process=subprocess.Popen(['macsearch', line],stdout=subprocess.PIPE)
out=process.communicate() """ using process.communicate to save the
output."""
for o in out:
print o
print ('\n')