def _functions_accepting_devnames(self, devname): self.assertIsString(devname) scraped = ifconfig.get_device_by_name(devname) try: broadcast = ethtool.get_broadcast(devname) except (OSError, IOError): # Some devices might end up with # [Errno 99] Cannot assign requested address # That's IOError on 2.7, OSError on 3 assignable = False else: assignable = True self.assertIsString(broadcast) # Broadcast is optional in ifconfig output if scraped.broadcast: self.assertEqualIpv4Str(broadcast, scraped.broadcast) self.assertIsStringExceptForLoopback( ethtool.get_businfo, devname, '[Errno 95] Operation not ' 'supported') self.assertIsInt(ethtool.get_flags(devname)) # flagsint cannot be obtained from old ifconfig format if not ifconfig.oldFormat: self.assertEqual(ethtool.get_flags(devname), scraped.flagsint) self.assertIsInt(ethtool.get_gso(devname)) self.assertIsInt(ethtool.get_gro(devname)) self.assertIsString(ethtool.get_hwaddr(devname)) self.assertEqualHwAddr(ethtool.get_hwaddr(devname), scraped.hwaddr) if assignable: self.assertIsString(ethtool.get_ipaddr(devname)) self.assertEqual(ethtool.get_ipaddr(devname), scraped.inet) self.assertIsStringExceptForLoopback( ethtool.get_module, devname, '[Errno 95] Operation not ' 'supported') if assignable: self.assertIsString(ethtool.get_netmask(devname)) self.assertEqual(ethtool.get_netmask(devname), scraped.netmask) # Operation is not supported only on loopback device if devname == 'lo': self.assertRaisesIOError(ethtool.get_ringparam, (devname, ), '[Errno 95] Operation not supported') self.assertIsInt(ethtool.get_sg(devname)) try: self.assertIsInt(ethtool.get_ufo(devname)) except (OSError, IOError): # This test may fail due to insufficient privileges # That's IOError on 2.7, OSError (PermissionError) on 3 pass self.assertIsInt(ethtool.get_tso(devname))
def _functions_accepting_devnames(self, devname): self.assertIsString(devname) scraped = ifconfig.get_device_by_name(devname) self.assertIsString(ethtool.get_broadcast(devname)) self.assertEqualIpv4Str(ethtool.get_broadcast(devname), scraped.broadcast) self.assertIsStringExceptForLoopback( ethtool.get_businfo, devname, '[Errno 95] Operation not supported') # ethtool.get_coalesce(devname) # this gives me: # IOError: [Errno 95] Operation not supported # on my test box self.assertIsInt(ethtool.get_flags(devname)) # flagsint cannot be obtained from old ifconfig format if not ifconfig.oldFormat: self.assertEqual(ethtool.get_flags(devname), scraped.flagsint) self.assertIsInt(ethtool.get_gso(devname)) self.assertIsString(ethtool.get_hwaddr(devname)) self.assertEqualHwAddr(ethtool.get_hwaddr(devname), scraped.hwaddr) self.assertIsString(ethtool.get_ipaddr(devname)) self.assertEqual(ethtool.get_ipaddr(devname), scraped.inet) self.assertIsStringExceptForLoopback( ethtool.get_module, devname, '[Errno 95] Operation not supported') self.assertIsString(ethtool.get_netmask(devname)) self.assertEqual(ethtool.get_netmask(devname), scraped.netmask) #self.assertRaisesIOError(ethtool.get_ringparam, (devname, ), # '[Errno 95] Operation not supported') # Disabling until BZ#703089 is investigated #self.assertIsInt(ethtool.get_sg(devname)) #self.assertIsInt(ethtool.get_ufo(devname)) self.assertIsInt(ethtool.get_tso(devname))
def _functions_accepting_devnames(self, devname): self.assertIsString(devname) scraped = ifconfig.get_device_by_name(devname) self.assertIsString(ethtool.get_broadcast(devname)) self.assertEqualIpv4Str(ethtool.get_broadcast(devname), scraped.broadcast) self.assertIsStringExceptForLoopback(ethtool.get_businfo, devname, "[Errno 95] Operation not supported") # ethtool.get_coalesce(devname) # this gives me: # IOError: [Errno 95] Operation not supported # on my test box self.assertIsInt(ethtool.get_flags(devname)) # flagsint cannot be obtained from old ifconfig format if not ifconfig.oldFormat: self.assertEqual(ethtool.get_flags(devname), scraped.flagsint) self.assertIsInt(ethtool.get_gso(devname)) self.assertIsString(ethtool.get_hwaddr(devname)) self.assertEqualHwAddr(ethtool.get_hwaddr(devname), scraped.hwaddr) self.assertIsString(ethtool.get_ipaddr(devname)) self.assertEqual(ethtool.get_ipaddr(devname), scraped.inet) self.assertIsStringExceptForLoopback(ethtool.get_module, devname, "[Errno 95] Operation not supported") self.assertIsString(ethtool.get_netmask(devname)) self.assertEqual(ethtool.get_netmask(devname), scraped.netmask) # self.assertRaisesIOError(ethtool.get_ringparam, (devname, ), # '[Errno 95] Operation not supported') # Disabling until BZ#703089 is investigated # self.assertIsInt(ethtool.get_sg(devname)) # self.assertIsInt(ethtool.get_ufo(devname)) self.assertIsInt(ethtool.get_tso(devname))
def show_config(device): ipaddr = ethtool.get_ipaddr(device) netmask = ethtool.get_netmask(device) flags = ethtool.get_flags(device) print '%-9.9s' % device, if not (flags & ethtool.IFF_LOOPBACK): print "HWaddr %s" % ethtool.get_hwaddr(device), print ''' inet addr:%s''' % ipaddr, if not (flags & (ethtool.IFF_LOOPBACK | ethtool.IFF_POINTOPOINT)): print "Bcast:%s" % ethtool.get_broadcast(device), print ''' Mask:%s %s ''' % (netmask, flags2str(flags))
def show_config(device): ipaddr = ethtool.get_ipaddr(device) netmask = ethtool.get_netmask(device) flags = ethtool.get_flags(device) print '%-9.9s' % device, if not (flags & ethtool.IFF_LOOPBACK): print "HWaddr %s" % ethtool.get_hwaddr(device), print ''' inet addr:%s''' % ipaddr, if not (flags & (ethtool.IFF_LOOPBACK | ethtool.IFF_POINTOPOINT)): print "Bcast:%s" % ethtool.get_broadcast(device), print ' Mask:%s' % netmask for info in ethtool.get_interfaces_info(device): for addr in info.get_ipv6_addresses(): print (" inet6 addr: %s/%s Scope: %s" % (addr.address, addr.netmask, addr.scope)) print ' %s' % flags2str(flags) print
def read_network_interfaces(): intDict = {} intDict['class'] = "NETINTERFACES" if not ethtool_present and not netifaces_present: # ethtool is not available on non-linux platforms (as kfreebsd), skip it sys.stderr.write( "Warning: information about network interfaces could not be retrieved on this platform.\n" ) return intDict if ethtool_present: interfaces = list( set(ethtool.get_devices() + ethtool.get_active_devices())) else: interfaces = netifaces.interfaces() for interface in interfaces: try: if ethtool_present: hwaddr = ethtool.get_hwaddr(interface) else: hwaddr = netifaces.ifaddresses(interface)[ netifaces.AF_LINK][0]['addr'] except: hwaddr = "" # slave devices can have their hwaddr changed try: master = os.readlink('/sys/class/net/%s/master' % interface) except: master = None if master: master_interface = os.path.basename(master) hwaddr = get_slave_hwaddr(master_interface, interface) try: if ethtool_present: module = ethtool.get_module(interface) else: driver_file = open( '/sys/class/net/%s/device/uevent' % interface, 'r') module = driver_file.readline().split('=')[1].strip() driver_file.close() except: if interface == 'lo': module = "loopback" else: module = "Unknown" try: if ethtool_present: ipaddr = ethtool.get_ipaddr(interface) else: ipaddr = netifaces.ifaddresses(interface)[ netifaces.AF_INET][0]['addr'] except: ipaddr = "" try: if ethtool_present: netmask = ethtool.get_netmask(interface) else: netmask = netifaces.ifaddresses(interface)[ netifaces.AF_INET][0]['netmask'] except: netmask = "" try: if ethtool_present: broadcast = ethtool.get_broadcast(interface) else: broadcast = netifaces.ifaddresses(interface)[ netifaces.AF_INET][0]['broadcast'] except: broadcast = "" ip6_list = [] if ethtool_present: dev_info = ethtool.get_interfaces_info(interface) for info in dev_info: # one interface may have more IPv6 addresses for ip6 in info.get_ipv6_addresses(): scope = ip6.scope if scope == 'global': scope = 'universe' ip6_list.append({ 'scope': scope, 'addr': ip6.address, 'netmask': ip6.netmask }) else: try: for dev_info in netifaces.ifaddresses(interface)[ netifaces.AF_INET6]: ip6_addr = dev_info['addr'].split('%')[0] scope_info = ipaddress.IPv6Address(ip6_addr) if scope_info.is_global: scope = 'universe' elif scope_info.is_link_local: scope = 'link' elif scope_info.is_loopback: scope = 'host' elif scope_info.is_site_local: scope = 'site' # count number of '1' bits in netmask returned by netifaces ip6_netmask = dev_info['netmask'] netmask_bits = 0 for two_octets in ip6_netmask.split(':'): if not two_octets: break elif two_octets.lower() == 'ffff': netmask_bits += 16 else: # remove '0b' from begin and find last '1' in the string netmask_bits += 1 + bin( int(two_octets.split('/')[0], 16))[2:].rindex('1') ip6_list.append({ 'scope': scope, 'addr': ip6_addr, 'netmask': netmask_bits }) except KeyError: pass # no ipv6 for this interface intDict[interface] = { 'hwaddr': hwaddr, 'ipaddr': ipaddr, 'netmask': netmask, 'broadcast': broadcast, 'module': module, 'ipv6': ip6_list } return intDict
def read_network_interfaces(): intDict = {} intDict['class'] = "NETINTERFACES" if not ethtool_present: # ethtool is not available on non-linux platforms (as kfreebsd), skip it return intDict interfaces = list(set(ethtool.get_devices() + ethtool.get_active_devices())) for interface in interfaces: try: hwaddr = ethtool.get_hwaddr(interface) except: hwaddr = "" # slave devices can have their hwaddr changed try: master = os.readlink('/sys/class/net/%s/master' % interface) except: master = None if master: master_interface = os.path.basename(master) hwaddr = get_slave_hwaddr(master_interface, interface) try: module = ethtool.get_module(interface) except: if interface == 'lo': module = "loopback" else: module = "Unknown" try: ipaddr = ethtool.get_ipaddr(interface) except: ipaddr = "" try: netmask = ethtool.get_netmask(interface) except: netmask = "" try: broadcast = ethtool.get_broadcast(interface) except: broadcast = "" ip6_list = [] dev_info = ethtool.get_interfaces_info(interface) for info in dev_info: # one interface may have more IPv6 addresses for ip6 in info.get_ipv6_addresses(): scope = ip6.scope if scope == 'global': scope = 'universe' ip6_list.append({ 'scope': scope, 'addr': ip6.address, 'netmask': ip6.netmask }) intDict[interface] = {'hwaddr':hwaddr, 'ipaddr':ipaddr, 'netmask':netmask, 'broadcast':broadcast, 'module': module, 'ipv6': ip6_list} return intDict
def read_network_interfaces(): intDict = {} intDict['class'] = "NETINTERFACES" if not ethtool_present: # ethtool is not available on non-linux platforms (as kfreebsd), skip it return intDict interfaces = list(set(ethtool.get_devices() + ethtool.get_active_devices())) for interface in interfaces: try: hwaddr = ethtool.get_hwaddr(interface) except: hwaddr = "" # slave devices can have their hwaddr changed try: master = os.readlink('/sys/class/net/%s/master' % interface) except: master = None if master: master_interface = os.path.basename(master) hwaddr = get_slave_hwaddr(master_interface, interface) try: module = ethtool.get_module(interface) except: if interface == 'lo': module = "loopback" else: module = "Unknown" try: ipaddr = ethtool.get_ipaddr(interface) except: ipaddr = "" try: netmask = ethtool.get_netmask(interface) except: netmask = "" try: broadcast = ethtool.get_broadcast(interface) except: broadcast = "" ip6_list = [] dev_info = ethtool.get_interfaces_info(interface) for info in dev_info: # one interface may have more IPv6 addresses for ip6 in info.get_ipv6_addresses(): scope = ip6.scope if scope == 'global': scope = 'universe' ip6_list.append({ 'scope': scope, 'addr': ip6.address, 'netmask': ip6.netmask }) intDict[interface] = { 'hwaddr': hwaddr, 'ipaddr': ipaddr, 'netmask': netmask, 'broadcast': broadcast, 'module': module, 'ipv6': ip6_list } return intDict
def read_network_interfaces(): intDict = {} intDict['class'] = "NETINTERFACES" interfaces = ethtool.get_devices() for interface in interfaces: try: hwaddr = ethtool.get_hwaddr(interface) except: hwaddr = "" # slave devices can have their hwaddr changed try: master = os.readlink('/sys/class/net/%s/master' % interface) except: master = None if master: master_interface = os.path.basename(master) hwaddr = get_slave_hwaddr(master_interface, interface) try: module = ethtool.get_module(interface) except: if interface == 'lo': module = "loopback" else: module = "Unknown" try: ipaddr = ethtool.get_ipaddr(interface) except: ipaddr = "" try: netmask = ethtool.get_netmask(interface) except: netmask = "" try: broadcast = ethtool.get_broadcast(interface) except: broadcast = "" ip6_list = [] try: # one interface may have more IPv6 addresses for ip6 in dev.get_ipv6_addresses(): ip6_list.append({ 'scope': ip6.scope, 'addr': ip6.address, 'netmask': ip6.netmask }) except: pass intDict[interface] = {'hwaddr':hwaddr, 'ipaddr':ipaddr, 'netmask':netmask, 'broadcast':broadcast, 'module': module, 'ipv6': ip6_list} return intDict
def read_network_interfaces(): intDict = {} intDict['class'] = "NETINTERFACES" if not ethtool_present and not netifaces_present: # ethtool is not available on non-linux platforms (as kfreebsd), skip it sys.stderr.write("Warning: information about network interfaces could not be retrieved on this platform.\n") return intDict if ethtool_present: interfaces = list(set(ethtool.get_devices() + ethtool.get_active_devices())) else: interfaces = netifaces.interfaces() for interface in interfaces: try: if ethtool_present: hwaddr = ethtool.get_hwaddr(interface) else: hwaddr = netifaces.ifaddresses(interface)[netifaces.AF_LINK][0]['addr'] except: hwaddr = "" # slave devices can have their hwaddr changed try: master = os.readlink('/sys/class/net/%s/master' % interface) except: master = None if master: master_interface = os.path.basename(master) hwaddr = get_slave_hwaddr(master_interface, interface) try: if ethtool_present: module = ethtool.get_module(interface) else: driver_file = open('/sys/class/net/%s/device/uevent' % interface, 'r') module = driver_file.readline().split('=')[1].strip() driver_file.close() except: if interface == 'lo': module = "loopback" else: module = "Unknown" try: if ethtool_present: ipaddr = ethtool.get_ipaddr(interface) else: ipaddr = netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['addr'] except: ipaddr = "" try: if ethtool_present: netmask = ethtool.get_netmask(interface) else: netmask = netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['netmask'] except: netmask = "" try: if ethtool_present: broadcast = ethtool.get_broadcast(interface) else: broadcast = netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['broadcast'] except: broadcast = "" ip6_list = [] if ethtool_present: dev_info = ethtool.get_interfaces_info(interface) for info in dev_info: # one interface may have more IPv6 addresses for ip6 in info.get_ipv6_addresses(): scope = ip6.scope if scope == 'global': scope = 'universe' ip6_list.append({ 'scope': scope, 'addr': ip6.address, 'netmask': ip6.netmask }) else: try: for dev_info in netifaces.ifaddresses(interface)[netifaces.AF_INET6]: ip6_addr = dev_info['addr'].split('%')[0] scope_info = ipaddress.IPv6Address(ip6_addr) if scope_info.is_global: scope = 'universe' elif scope_info.is_link_local: scope = 'link' elif scope_info.is_loopback: scope = 'host' elif scope_info.is_site_local: scope = 'site' # count number of '1' bits in netmask returned by netifaces ip6_netmask = dev_info['netmask'] netmask_bits = 0 for two_octets in ip6_netmask.split(':'): if not two_octets: break elif two_octets.lower() == 'ffff': netmask_bits += 16 else: # remove '0b' from begin and find last '1' in the string netmask_bits += 1 + bin(int(two_octets.split('/')[0], 16))[2:].rindex('1') ip6_list.append({ 'scope': scope, 'addr': ip6_addr, 'netmask': netmask_bits }) except KeyError: pass # no ipv6 for this interface intDict[interface] = {'hwaddr': hwaddr, 'ipaddr': ipaddr, 'netmask': netmask, 'broadcast': broadcast, 'module': module, 'ipv6': ip6_list} return intDict