2017-03-16 39 views
0

在蟒如果我有接口= netifaces.interfaces(),其是返回的列表: [“LO”,“eth0的”,“ETH1”]蟒列表在子过程for循环

如何我环在列表中传递lo和eth0,将eth1转换为ethtool的几个子进程调用?

subprocess.call(['ethtool %'] interfaces) --- lo 
subprocess.call(['ethtool %'] interfaces) --- eth0 
subprocess.call(['ethtool %'] interfaces) --- eth1 

我想在我的列表界面的项目数量运行ethtool的,但每一遍我想替换下一个项目在接口列表并传递到subprocess.call。

+0

任何你不能把它放在for循环中的原因,因为你的问题标题说明了什么? – Shadow

+0

是的。我想把它放在for循环中,但是如何在每次传递中传递接口列表中的每个项目? 接口在接口: subprocess.call(['ethtool%'] ????) – god

+0

我明白了。我已经添加了答案。你传递给'call'的列表是一个参数列表。所以只需提供界面作为列表中的第二项。 – Shadow

回答

2

据我所知,你只是想为循环做一个正常的...

interfaces = netifaces.interfaces() 
for interface in interfaces: 
    subprocess.call(["ethtool", interface]) 
0
for interface in interfaces: 
    print("[+] Fetching LLDP info for ", interface) 
    subprocess.call(['lldptool', '-t', '-n', '-i', interface]) 
    print("[+] Printing ethtool info for ", interface) 
    subprocess.call(['ethtool', interface]) 

我证明,我是路过的接口子进程调用的外部,一旦我把它放在[]中,按预期工作。我喜欢这个网站!谢谢你的影子

+0

欢迎来到Stack Overflow。这里的约定是upvote(点击向上箭头)和/或接受(点击复选标记,使其变成绿色)帮助你的答案。 – tripleee