代码如下:
import subprocess
cmd1=['ifconfig','-a']
cmd2=['grep','inet']
cmd3=['grep', '-v', '127.0.0.1']
cmd4=['grep', '-v', 'inet6']
cmd5=['awk','{print $2}']
cmd6=['tr','-d','addr:']
cmd7=['head','-n','1']
def exec_cmd(*args):
for index,cmd in enumerate(args):
if index == 0:
p=subprocess.Popen(cmd,stderr=subprocess.PIPE,stdout=subprocess.PIPE,
shell=False)
else:
p=subprocess.Popen(cmd,stdin=p.stdout,stderr=subprocess.PIPE,
stdout=subprocess.PIPE,shell=False)
ret = p.communicate()[0]
print(ret.decode())
exec_cmd(cmd1,cmd2,cmd3,cmd4,cmd5)
我的预期是要得到本机的ip地址,上面的代码执行后获取不到,在执行第一个subprocess后会卡在那里,请问怎么解决!