Deprecate calling sub process 'ifconfig' use netifaces instead

This commit is contained in:
Yunusemre Şentürk 2017-11-21 13:49:09 +03:00
parent 998010cb1b
commit 9b784bb656

View file

@ -10,6 +10,7 @@ import platform
import re import re
import socket import socket
import struct import struct
import netifaces
from uuid import getnode as get_mac from uuid import getnode as get_mac
import cpuinfo import cpuinfo
@ -460,9 +461,14 @@ class System:
@staticmethod @staticmethod
def ip_addresses(): def ip_addresses():
arr = [] arr = []
for iface in psutil.net_io_counters(pernic=True): for iface in netifaces.interfaces():
f = os.popen('ifconfig {0} | grep "inet\ addr" | cut -d: -f2 | cut -d" " -f1'.format(iface)) link = None
ip = str(f.read()).replace('\n', '') try:
link = netifaces.ifaddresses(iface)[netifaces.AF_INET][0]
except:
link = None
if link is not None:
ip = link['addr']
if re.match(r'^((\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])$', if re.match(r'^((\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])$',
ip) and str(ip) != 'localhost' and str(ip) != '127.0.0.1': ip) and str(ip) != 'localhost' and str(ip) != '127.0.0.1':
arr.append(ip) arr.append(ip)