我试图将函数get_ethname
调用到另一个函数get_ethSpeed
中,但我无法理解如何调用它。
非常感谢你提前做出的贡献。
第一个函数的输出返回NIC接口的名称 在系统上如下..
[root@tss/]# cat intDetail1.py
#!/usr/bin/python
import ethtool
def get_ethname():
inames = ethtool.get_devices()
inameCurr = inames[1]
print inameCurr
return inameCurr
def main():
get_ethname()
main()
[root@tss /]# ./intDetail1.py
eth0
以下是我尝试调用它的主要代码。
#!/usr/bin/python
import ethtool
import subprocess
def get_ethname():
inames = ethtool.get_devices()
inameCurr = inames[1]
print inameCurr
return inameCurr
def get_ethSpeed():
spd = subprocess.popen("['ethtool', 'get_ethname']", stdout=subprocess.PIPE).communicate()[0]
print spd
return spd
def main():
get_ethname()
get_ethSpeed()
main()
当我运行上面的代码时,它会给出以下错误。
File "/usr/lib64/python2.6/subprocess.py", line 1234, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
我的目标是获取系统上的主要运行接口名称,然后使用linux系统实用程序ethtool
来获取NIC的速度确定,它告诉接口的速度:
[root@tss /]# /sbin/ethtool eth0| grep Speed
Speed: 1000Mb/s
ethtool eth0
的输出外观如下:
[root@tss /]# ethtool eth0
Settings for eth0:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Supported pause frame use: No
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised pause frame use: No
Advertised auto-negotiation: Yes
Speed: 1000Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 1
Transceiver: internal
Auto-negotiation: on
MDI-X: Unknown
Supports Wake-on: g
Wake-on: g
Link detected: yes
答案 0 :(得分:1)
没有此类设备get_ethname()的设置:无可用数据
这与原始问题仍然是同一个问题。你传递一个文字字符串,并期望shell调用Python函数?
除了实际的shell命令
之外,这里没有引号spd = subprocess.Popen(['/sbin/ethtool', get_ethname()], stdout=subprocess.PIPE).communicate()[0]
或者,制作另一个变量
iface = get_ethname()
# Or
iface = ethtool.get_devices()[1]
spd = subprocess.Popen(['/sbin/ethtool', iface], stdout=subprocess.PIPE).communicate()
return spd[0]
请注意,您仍需要grep(或使用python扫描输出)以获得“Speed”