def testRouteFromText(self): _getRouteAttrs = lambda x: (x.network, x.via, x.device, x.table) good_routes = { 'default via 192.168.99.254 dev eth0': ('0.0.0.0/0', '192.168.99.254', 'eth0', None), 'default via 192.168.99.254 dev eth0 table foo': ('0.0.0.0/0', '192.168.99.254', 'eth0', 'foo'), '200.100.50.0/16 via 11.11.11.11 dev eth2 table foo': ('200.100.50.0/16', '11.11.11.11', 'eth2', 'foo'), 'local 127.0.0.1 dev lo src 127.0.0.1': ('127.0.0.1', None, 'lo', None), 'unreachable ::ffff:0.0.0.0/96 dev lo metric 1024 error -101': ('::ffff:0.0.0.0/96', None, 'lo', None), 'broadcast 240.0.0.255 dev veth_23 table local ' 'proto kernel scope link src 240.0.0.1': ('240.0.0.255', None, 'veth_23', 'local'), 'ff02::2 dev veth_23 metric 0 \ cache': ('ff02::2', None, 'veth_23', None), } for text, attributes in good_routes.iteritems(): route = Route.fromText(text) self.assertEqual(_getRouteAttrs(route), attributes) bad_routes = \ ['default via 192.168.99.257 dev eth0 table foo', # Misformed via '200.100.50.0/16 dev eth2 table foo extra', # Key without value '288.1.2.9/43 via 1.1.9.4 dev em1 table foo', # Misformed network '200.100.50.0/16 via 192.168.99.254 table foo', # No device 'local dev eth0 table bar'] # local with no address for text in bad_routes: self.assertRaises(ValueError, Route.fromText, text)
def routes(table='all'): routes_data = routeShowTable(table) for route_data in routes_data: try: r = Route.fromText(route_data) family = 6 if _is_ipv6_addr_soft_check(r.network) else 4 rtable = r.table if table == 'all' else table yield IPRouteData(r.network, r.via, family, r.src, r.device, rtable) except ValueError: logging.warning('Could not parse route %s', route_data)
def routes(table='all'): routes_data = routeShowTable(table) for route_data in routes_data: try: r = Route.fromText(route_data) family = 6 if _is_ipv6_addr_soft_check(r.network) else 4 rtable = r.table if table == 'all' else table yield IPRouteData( r.network, r.via, family, r.src, r.device, rtable) except ValueError: logging.warning('Could not parse route %s', route_data)
def getRouteDeviceTo(destinationIP): """Return the name of the device leading to destinationIP or the empty string if none is found""" try: route = routeGet([destinationIP])[0] except (IPRoute2Error, IndexError): logging.exception("Could not route to %s", destinationIP) return "" try: return Route.fromText(route).device except ValueError: logging.exception("Could not parse route %s", route) return ""
def getRouteDeviceTo(destinationIP): """Return the name of the device leading to destinationIP or the empty string if none is found""" try: route = routeGet([destinationIP])[0] except (IPRoute2Error, IndexError): logging.exception('Could not route to %s', destinationIP) return '' try: return Route.fromText(route).device except ValueError: logging.exception('Could not parse route %s', route) return ''
def getDefaultGateway(): output = routeShowGateways("main") return Route.fromText(output[0]) if output else None
def ipv6_default_gateway(): output = route6_show_gateways('main') return Route.fromText(output[0]) if output else None
def getDefaultGateway(): output = routeShowGateways('main') return Route.fromText(output[0]) if output else None