Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_net_if_addrs(self):
nics = psutil.net_if_addrs()
assert nics, nics
# Not reliable on all platforms (net_if_addrs() reports more
# interfaces).
# self.assertEqual(sorted(nics.keys()),
# sorted(psutil.net_io_counters(pernic=True).keys()))
families = set([socket.AF_INET, AF_INET6, psutil.AF_LINK])
for nic, addrs in nics.items():
self.assertEqual(len(set(addrs)), len(addrs))
for addr in addrs:
self.assertIsInstance(addr.family, int)
self.assertIsInstance(addr.address, str)
self.assertIsInstance(addr.netmask, (str, type(None)))
self.assertIsInstance(addr.broadcast, (str, type(None)))
self.assertIn(addr.family, families)
def test_internal_state_updated(self, serve_data_url) -> None:
check = NetworkBandwidth(
"name", psutil.net_if_addrs().keys(), sys.float_info.max, sys.float_info.max
)
check.check()
old_state = check._previous_values
requests.get(serve_data_url)
check.check()
assert old_state != check._previous_values
def checkNetVPNV():
"""Check if there's a working VPN connection. Verbose."""
# Checking for VPN interfaces
INTERFACE_VPN = (config['NETWORK']['INTERFACE_VPN'])
print(bc.ENDC + '\t[*] Checking if VPN connection is active' + bc.ENDC)
for s in psutil.net_if_addrs():
if any(f in s for f in INTERFACE_VPN.split(',')):
print(bc.OKGREEN + '\t[+] Indications of a VPN. Good. Will continue.' + bc.ENDC)
return None
else:
print(bc.WARN + '\t[-] WARN! No indication of a VPN connection on "tun" or "ppp" found.')
return 'ERROR'
def getNetworkInfo():
ans = {}
d = psutil.net_if_addrs()
for n in d.keys():
for addr in d[n]:
m = n+':'+str(addr.family).replace('AddressFamily.','')
ans[m]={}
ans[m]['Address'] = addr.address
ans[m]['Netmask'] = addr.netmask
ans[m]['Broadcast'] = addr.broadcast
ans[m]['PTP'] = addr.ptp
def get_network():
"""Get Network IPs."""
ips = []
try:
for det in psutil.net_if_addrs().values():
ips.append(det[0].address)
except Exception:
logger.exception('Failed to enumerate network interfaces')
return ips
# ignore virtual ethernet interfaces
if key.startswith('veth'):
continue
# Quick fix - ignore interfaces with dots because our server
# refuses to accept keys with dots.
if '.' in key:
continue
interfaces[key] = {
'isup': value.isup,
'speed': value.speed,
'mtu': value.mtu
}
addresses = psutil.net_if_addrs()
for key, value in six.iteritems(addresses):
if key not in interfaces:
continue
for i in value:
if i.family == 2:
interfaces[key]['ipv4'] = i.address
interfaces[key]['netmask'] = i.netmask
elif i.family == 17:
interfaces[key]['mac'] = i.address
traffic = psutil.net_io_counters(pernic=True)
for key, value in six.iteritems(traffic):
if key not in interfaces:
continue
def get_host_ip(self):
intf = "docker0"
intfs = psutil.net_if_addrs()
intf_info = intfs.get(intf, None)
if intf_info:
for address in intf_info:
if address.family == 2:
host_address = address.address
return host_address
return None
def render(draw, width, height):
margin = 3
title_text(draw, margin, width, text="Net:{0}".format(interface))
try:
address = psutil.net_if_addrs()[interface][0].address
counters = psutil.net_io_counters(pernic=True)[interface]
draw.text((margin, 20), text=address, font=tiny_font, fill="white")
draw.text((margin, 35), text="Rx:", font=tiny_font, fill="white")
draw.text((margin, 45), text="Tx:", font=tiny_font, fill="white")
right_text(draw, 35, width, margin, text=bytes2human(counters.bytes_recv))
right_text(draw, 45, width, margin, text=bytes2human(counters.bytes_sent))
except:
draw.text((margin, 20), text="n/a", font=tiny_font, fill="white")
def getNetiInfo(self):
result = []
neti_dict = psutil.net_if_addrs()
for neti in neti_dict:
ipv4_ip = ''
ipv6_ip = ''
ipv4_netmask = ''
ipv6_netmask = ''
mac = ''
snic_list = neti_dict[neti]
for snic in snic_list:
if snic.family.name == 'AF_INET':
ipv4_ip = snic.address
ipv4_netmask = snic.netmask
elif snic.family.name == 'AF_INET6':
ipv6_ip = re.sub('%.*$', '', snic.address)
ipv6_netmask = snic.netmask