i'm using a python script from the opencommunity for getting thi NIC card speed, which is working fine on the python2.7 but in python3.6 its not working .
Below is the script:
import subprocess
import netifaces
import socket
import re
hst_name = (socket.gethostname())
def get_Inf():
global Current_inf
Current_inf = netifaces.gateways()['default'][netifaces.AF_INET][1]
return Current_inf
def get_bondSpeed():
spd1 = open('/sys/class/net/{}/speed' .format(Current_inf)).read().strip()
return spd1
def get_intSpeed():
spd = subprocess.Popen(['/sbin/ethtool', get_Inf()], stdout=subprocess.PIPE).communicate()[0]
pat_match=re.search(".*Speed:\s+(\d+)+.*", int(spd))
speed = pat_match.group(1)
return speed
if get_intSpeed() == str(1000):
print("Service Status: System is running with 1Gbit Speed on the host",hst_name)
elif get_intSpeed() == str(10000):
print("Service Status: System is running with 10Gbit Speed on the host",hst_name)
elif get_bondSpeed() == str(10000):
print("Service Status: System is running with 10Gbit Speed on the host",hst_name, "with bond setup!")
elif get_bondSpeed() == str(1000):
print("Service Status: System is running with 1Gbit Speed on the host",hst_name, "with bond setup!")
elif get_bondSpeed() == str(2000):
print("Service Status: System is running with 2Gbit Speed on the host",hst_name, "with bond setup!")
else:
print("Service Status: System is not running with Gig Speed, Please check manually on the host",hst_name)
get_Inf()
Error while trying:
raceback (most recent call last):
File "./getSpeedInfo.py", line 36, in <module>
if get_intSpeed() == str(1000):
File "./getSpeedInfo.py", line 32, in get_intSpeed
pat_match=re.search(".*Speed:\s+(\d+)+.*", int(spd))
ValueError: invalid literal for int() with base 10: b'Settings for ens192:\n\tSupported ports: [ TP ]\n\tSupported link modes: 1000baseT/Full \n\t 10000baseT/Full \n\tSupported pause frame use: No\n\tSupports auto-negotiation:
# ethtool ens192
Settings for ens192:
Supported ports: [ TP ]
Supported link modes: 1000baseT/Full
10000baseT/Full
Supported pause frame use: No
Supports auto-negotiation: No
Supported FEC modes: Not reported
Advertised link modes: Not reported
Advertised pause frame use: No
Advertised auto-negotiation: No
Advertised FEC modes: Not reported
Speed: 10000Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 0
Transceiver: internal
Auto-negotiation: off
MDI-X: Unknown
Supports Wake-on: uag
Wake-on: d
Link detected: yes
any help on this will be much appreciated
You get ValueError because you are trying to convert a binary string (containing non-numeric) to int.
To fix it, try :
pat_match=re.search(b".*Speed:\s+(\d+).*", spd)
speed = pat_match.group(1).decode()
As the result of subprocess.Popen
is binary string, so I added b
in b".*Speed:\s+(\d+).*"
and removed int()
around spd
.
Now the result of pat_match.group(1)
is again binary string, .decode()
is needed.