【发布时间】:2017-04-23 11:37:47
【问题描述】:
我正在尝试将函数 get_ethname 调用到另一个函数 get_ethSpeed,但我无法理解如何调用它。
非常感谢您提前提供的意见。
第一个函数的输出返回网卡接口的名称 在系统上如下..
[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 来确定网卡的速度,它告诉接口的速度:
[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
【问题讨论】:
-
文件名及其树形结构/关系是什么?
-
popen需要参数列表。你给了它一个字符串。去掉双引号应该没问题。此外,不再支持python2.6。你应该尽快转到python2.7或者最好是python3。 -
@DaniSpringer,抱歉,我无法准确回答您的问题。我只是想通过
ethtool命令接收eth0,这是linux系统命令,所以说第二个函数需要获取/sbin/ethtool eth0输出的输出。代码中没有调用文件。 -
另外,
get_ethname需要调用你定义的函数:spd = subprocess.popen(['/sbin/ethtool', get_ethname()], stdout=subprocess.PIPE).communicate()[0] -
@DaniSpringer,很高兴收到您的意见。
标签: python