Ejemplo n.º 1
0
    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"
Ejemplo n.º 2
0
    def add_art_route_listener(self, approutetable):

        aL3UnicastScope = L3UnicastScope("", L3UnicastScope.AFIType.IPV4,
                                         L3UnicastScope.SAFIType.UNICAST, "")
        #  START SNIPPET: addARTRouteListener
        #  Add a listener to receive route state change events.
        #  When events arrive, listener.handleEvent() will be invoked.
        exampleARTRouteListener1 = self.ExampleARTRouteListener()
        logger.info("Adding ART listener...")
        exampleARTRouteListenerEventHandle = approutetable.add_route_state_listener(
            exampleARTRouteListener1, aL3UnicastScope, None)
        logger.info(str(exampleARTRouteListenerEventHandle))
        return exampleARTRouteListenerEventHandle
Ejemplo n.º 3
0
    def OnepEvent_L3UcastARTRouteEventIDL(self, base, artState):
        """
            For internal use only
        
            """
        from onep.routing.ARTRouteStateEvent import ARTRouteStateEvent
        if base == None:
            return 
        sessionHandle = base.sessionHandle
        eventHandle = base.eventHandle
        protocol = base.protocol
        safi = L3UnicastScope.SAFIType.UNICAST
        afi = 0
        if base.prefix != None and base.prefix.addr != None:
            afi = base.prefix.addr.family
        metric = base.metric
        distance = base.distance
        prefix = 0
        if base.prefix != None:
            prefix = base.prefix.prefix_len
        network = None
        if base.prefix != None and base.prefix.addr != None:
            network = base.prefix.addr.addr
        topoName = base.topoName
        vrfName = base.vrfName
        tagName = base.tagName
        if self.terminated:
            return 
        scope = None
        try:
            scope = L3UnicastScope(vrfName, afi, safi, topoName)
        except OnepIllegalArgumentException as e1:
            scope = None
        destNetwok = NetworkPrefix(network, prefix)
        nhList = set()
        if base.hopList != None:
            for nh_idl in base.hopList:
                nh = L3UnicastNextHop._from_idl(nh_idl, self.element)
                if nh != None:
                    nhList.add(nh)

        route = L3UnicastRoute(destNetwok, nhList, protocol, tagName, L3UnicastRoute.RouteType.NONE, distance, metric, Route.RouteErrorCode.NONE)
        event = ARTRouteStateEvent(self.element, eventHandle, scope, route, artState)
        self.enque(event)
Ejemplo n.º 4
0
    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
        eth_interface = tutorial.get_an_interface()
        if eth_interface == None:
            logger.error(
                "Could not find a suitable interface to add routes to.")
            return
        logger.info(eth_interface)

        route_scope = L3UnicastScope("", L3UnicastScope.AFIType.IPV4,
                                     L3UnicastScope.SAFIType.UNICAST, "")

        #aL3UnicastNextHopList = HashSet()
        aL3UnicastNextHopList = list()
        aL3UnicastNextHop = L3UnicastNextHop(eth_interface, "10.1.1.24")
        aL3UnicastNextHopList.append(aL3UnicastNextHop)
        aL3UnicastNextHopList.append(
            L3UnicastNextHop(eth_interface, "10.1.1.25"))

        destNetworkPrefix = NetworkPrefix("160.10.0.0", 16)

        aRoute = L3UnicastRoute(destNetworkPrefix, aL3UnicastNextHopList)
        aRoute.admin_distance = 1
        #  Now update the app route table with this route.
        routeOperation = L3UnicastRouteOperation(0, aRoute)

        routeOperationList = list()
        routeOperationList.append(routeOperation)
        mylist = approutetable.update_routes(route_scope, routeOperationList)
        logger.info(mylist)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
    def add_rib_route_listener(self, rib):
        """
        Adds a RIB Route listener.
        
        @return Event handler identifier for the listener.
        
        @param rib: Routing Information Base to be applied
        
        """

        #  START SNIPPET: addRIBRouteListener
        aL3UnicastScope = L3UnicastScope("", L3UnicastScope.AFIType.IPV4,
                                         L3UnicastScope.SAFIType.UNICAST, "")
        rib_filter = L3UnicastRIBFilter()
        #  Add a listener to receive route state change events.
        #  When events arrive, listener.handleEvent() will be invoked.
        exampleRIBRouteListener = self.ExampleRIBRouteListener()
        logger.info("adding RIB listener...")
        #         exampleRIBRouteListenerEventHandle = None
        exampleRIBRouteListenerEventHandle = rib.add_route_state_listener(
            exampleRIBRouteListener, aL3UnicastScope, rib_filter, 0, None)
        logger.info(str(exampleRIBRouteListenerEventHandle))
        return exampleRIBRouteListenerEventHandle
Ejemplo n.º 7
0
        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))
        #  END SNIPPET: addARTRouteListener

        #  Add custom application routes.
        logger.info("Adding Routes...")
        tutorial.addRoutes(approutetable)

        #  Removes the ART Listener.
        logger.info("Removing ART route Listener...")
        tutorial.remove_art_route_listener(approutetable, aARTEventHandler)
Ejemplo n.º 8
0
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(
                    [next_hop.address, next_hop.network_interface.name],
                    key=len)
                break

            full_prefix = '%s/%s' % (route.prefix.address,
                                     route.prefix.prefix_length)
Ejemplo n.º 9
0
    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)