def handle_event(self, event, clientData): print "-----" print "Caught: " + event.message print "-----" blackhole = False vtyService = VtyService(router) vtyService.open() TEST_CMD1 = "who" cli_result = vtyService.write(TEST_CMD1) vtyService.close() victim_string = " " + victim + " " lines = cli_result.split("\n") for line in lines: if " vty " and victim_string in line: print "-----" print "User is on the system: " + victim entries = line.split() for entry in entries: if is_ip_address(entry): blackhole = True blackhole_ip = entry if blackhole: print "Blackholing ip: " + blackhole_ip out_if = router.get_interface_by_name("Null0") routing = Routing.get_instance(router) approutetable = routing.app_route_table route_scope = L3UnicastScope("", L3UnicastScope.AFIType.IPV4, L3UnicastScope.SAFIType.UNICAST, "") aL3UnicastNextHop = L3UnicastNextHop(out_if, "") aL3UnicastNextHopList = list() aL3UnicastNextHopList.append(aL3UnicastNextHop) destNetworkPrefix = NetworkPrefix(blackhole_ip, 32) aRoute = L3UnicastRoute(destNetworkPrefix, aL3UnicastNextHopList) aRoute.admin_distance = 1 routeOperation = L3UnicastRouteOperation(0, aRoute) routeOperationList = list() routeOperationList.append(routeOperation) mylist = approutetable.update_routes(route_scope, routeOperationList) print "-----" print "Type a key to exit script"
def getRIB(self): """ Gets the RIB table for the connected network element. @return RIB table for the connected network element. @throws OnepConnectionException """ # START SNIPPET: getRIB # Create a Routing object for the network element. routing = Routing.getInstance(tutorial.get_network_element()) # Get the instance of the RIB table. rib = routing.getRib() return rib
def get_app_route_table(self): """ Gets the Application Route Table generated by this application. @return AppRouteTable for this application @throws OnepConnectionException """ # START SNIPPET: getAppRouteTable # Create a Routing object for the network element. routing = Routing.get_instance(tutorial.get_network_element()) # Get the instance of application route table. approutetable = routing.get_app_route_table() return approutetable
def display_routes(net_element): ROUTES_TO_RETURN = 10 # Create a Routing object routing = Routing.get_instance(net_element) # IPv4 Unicast routes only scope = L3UnicastScope("", L3UnicastScope.AFIType.IPV4, L3UnicastScope.SAFIType.UNICAST, "") # Get all routes (limited by ROUTES_TO_RETURN) prefix = NetworkPrefix("0.0.0.0", 0) range = L3UnicastRouteRange(prefix, RouteRange.RangeType.EQUAL_OR_LARGER, ROUTES_TO_RETURN) # Create a blank filter object filter = L3UnicastRIBFilter() # Get the routes route_list = routing.rib.get_route_list(scope, filter, range) for route in route_list: print route.prefix.address + "/" + str(route.prefix.prefix_length)
import sys tutorial = ARTTutorial(sys.argv) logger.info("Reading arguments...") if not tutorial.parse_command_line(): logger.error("Error in parsing arguments") sys.exit(1) try: logger.info("Connecting to Network Element...") if not tutorial.connect("ARTTutorial"): logger.error("Error in connecting to network element") sys.exit(1) logger.info("Done") # Create a Application Routing Table. logger.info("Getting a Routing Instance...") routing = Routing.get_instance(tutorial.get_network_element()) logger.info("Getting a Application Route Table...") approutetable = routing.app_route_table # Add a ART listener to listen for changes in the ART. logger.info("Adding ART Listener...") # START SNIPPET: addARTRouteListener aL3UnicastScope = L3UnicastScope("", L3UnicastScope.AFIType.IPV4, L3UnicastScope.SAFIType.UNICAST, "") # Add a listener to receive route state change events. When events arrive, listener.handleEvent() will be invoked. artRouteListener = ExampleARTRouteListener() aARTEventHandler = approutetable.add_route_state_listener( artRouteListener, aL3UnicastScope, 0, None) logger.info("aARTEventHandler : ") logger.info(str(aARTEventHandler))
# This script uses the onep_connect.py module from onep_connect import connect from onep.routing import RIB, Routing, L3UnicastScope, L3UnicastRouteRange, L3UnicastRIBFilter, RouteRange, AppRouteTable from onep.interfaces import NetworkPrefix import sys if len(sys.argv) != 4: print 'Usage: python script_name.py [ip_address] [username] [password]' quit() # Connect using passed in connection values # (will raise a ValueError if bad IP address or credentials) ne = connect(sys.argv[1], sys.argv[2], sys.argv[3]) try: routing = Routing.get_instance(ne) # We need to get routes separately for IPv4 and IPv6 # since we can't specify a Scope.AFIType of both address families :( for afi_type in (L3UnicastScope.AFIType.IPV4, L3UnicastScope.AFIType.IPV6): prefix = NetworkPrefix("::", 0) scope = L3UnicastScope("", afi_type) range = L3UnicastRouteRange(prefix, RouteRange.RangeType.EQUAL_OR_LARGER, 0) filter = L3UnicastRIBFilter() route_list = routing.rib.get_route_list(scope, filter, range) for route in route_list: #get the first next hop only, either the interface or IP for next_hop in route.next_hop_list: next_hop = max(
def addRoutes(self, approutetable): """ Adds custom application routes to the network element. @param approutetable: application route table to be updated @throws OnepConnectionException @throws OnepIllegalArgumentException @throws OnepRemoteProcedureException @throws UnknownHostException """ # START SNIPPET: addRoutes # Create a Routing object for the network element. routing = Routing.get_instance(tutorial.get_network_element()) # Specify scope, filter and range; aL3UnicastScope = L3UnicastScope("", L3UnicastScope.AFIType.IPV4, L3UnicastScope.SAFIType.UNICAST, "") networkPrefix = NetworkPrefix("10.0.0.0", 32) rib_filter = L3UnicastRIBFilter() # Get the instance of RIB information. rib = routing.rib range = L3UnicastRouteRange(networkPrefix, RouteRange.RangeType.EQUAL_OR_LARGER, 10) # Get all routes from RIB. ##TODO: Revisit START #routeList = rib.get_route_list(aL3UnicastScope, rib_filter, range) # Print the route in the list if it is a layer 3 unicast route. #for route in routeList: # if isinstance(route, L3UnicastRoute): # logger.info("Route is : " + route) ##TODO: Revisit END # Create a new route and change its administrative distance # to make it more trusted. This operation will have the same effect # as the adding/replacing static route using the following IOS # config command: # # ip route 10.1.1.0 255.255.255.0 10.15.1.7 # destNetwork = NetworkPrefix("10.1.1.0", 24) eth_interface = tutorial.get_network_element().get_interface_by_name( "Ethernet1/1") route_scope = L3UnicastScope("", L3UnicastScope.AFIType.IPV4, L3UnicastScope.SAFIType.UNICAST, "") aL3UnicastNextHop = L3UnicastNextHop(eth_interface, "10.15.1.7", route_scope) #aL3UnicastNextHopList = HashSet() aL3UnicastNextHopList = list() aL3UnicastNextHopList.append(aL3UnicastNextHop) aRoute = L3UnicastRoute(destNetwork, aL3UnicastNextHopList) aRoute.admin_distance = 1 # Now update the app route table with this route. routeOperation = L3UnicastRouteOperation( RouteOperation.RouteOperationType.REPLACE, aRoute) routeOperationList = list() routeOperationList.append(routeOperation) approutetable.update_routes(aL3UnicastScope, routeOperationList)