我编写了以下代码来获取Linux上当前运行界面的速度,但是当我运行此脚本时,它只是抛出错误:TypeError: execv() arg 2 must contain only strings
。
非常感谢任何帮助或想法。
以下是剧本:
在下面的脚本中,我创建了2个函数
1)一个是get_Inf()
,它给出了接口信息。
2)第二个是get_intSpeed()
,它接受来自第一个功能的接口名称,并将os命令ethtool
传递给它,后者通过正则表达式进一步解析,仅获取像1000Mb/s
这样的速度。
#!/grid/common/pkgs/python/v2.7.10/bin/python
import subprocess
import netifaces
import re
''' This snippet is just to get the Speed of the running interface on the System '''
def get_Inf():
Current_inf = netifaces.gateways()['default'][netifaces.AF_INET][1]
return get_Inf
def get_intSpeed():
spd = subprocess.Popen(['/sbin/ethtool', get_Inf()], stdout=subprocess.PIPE).communicate()[0]
pat_match=re.search(".*Speed:\s+(\d+Mb/s)\s+.*", spd) # "d" means any number of digit following the "Mb/s".
speed = pat_match.group(1)
return speed
def main():
print get_intSpeed()
main()
下面是具有Speed的os命令
/sbin/ethtool
接口信息以及其他信息。
[root@tss /]# /sbin/ethtool eth0| grep Speed
Speed: 1000Mb/s
[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
我使用的是python版本2.7.10。
答案 0 :(得分:1)
您的get_Inf()
函数将使用return get_Inf
语句返回自身,该语句不是字符串,这就是execv
(由subprocess.Popen
调用)抱怨的原因。您应该从函数返回Current_Inf
。