Ejemplo n.º 1
0
def main():
    pynet_rtr1 = dict(ip='50.242.94.227',
                      username='******',
                      password='******',
                      pin_file='pynet-rtr1-pin.txt',
                      port=15002)

    pynet_rtr2 = dict(ip='50.242.94.227',
                      username='******',
                      password='******',
                      pin_file='pynet-rtr2-pin.txt',
                      port=8002)

    for a_rtr in (pynet_rtr1, pynet_rtr2):
        rtr_obj = NetworkDevice(**a_rtr)
        rtr_obj.establish_session()

        vty_service = VtyService(rtr_obj.net_element)
        vty_service.open()

        router_name = rtr_name = rtr_obj.net_element.properties.sys_name

        cmd = "show version"
        cli = vty_service.write(cmd)

        print rtr_name
        print cli

        rtr_obj.disconnect()
Ejemplo n.º 2
0
def main():
    """
    reate an onep object, establish a connection with an element
    :return: output of the show version CLI command
    """
    for rtr in (rtr1, rtr2):
        rtr_obj = NetworkDevice(**rtr)
        rtr_obj.establish_session()
        vty_service = VtyService(rtr_obj.net_element)
        vty_service.open()
        print 'Stats for {}:'.format(rtr_obj.net_element.properties.sys_name)
        set_command(vty_service, 'terminal length 0')
        print set_command(vty_service, 'show version')
        rtr_obj.disconnect()
Ejemplo n.º 3
0
Archivo: 6-3.py Proyecto: sfromm/pyn
def main(args):
    ''' main '''
    rtrs = yaml.load(file(args[0]).read())
    for rtr in rtrs:
        ndev = NetworkDevice(**rtr)
        ndev.connect()

        print "Connection established to: %s" % ndev.net_element.properties.sys_name
        vty_svc = VtyService(ndev.net_element)
        vty_svc.open()
        for cmd in CMDS:
            out = vty_svc.write(cmd)
            print out

        ndev.disconnect()
Ejemplo n.º 4
0
def main():
    '''
    Using onePK and the VTY Service obtain the output from the 'show version'
    command.  Use 'terminal length 0' to disable output paging (i.e. disable
    '--More--' prompting).
    '''

    pynet_rtr1 = dict(
        ip='10.10.10.10',
        username='******',
        password='******',
        pin_file='pynet-rtr1-pin.txt',
        port=15002
    )

    pynet_rtr2 = dict(
        ip='10.10.10.10',
        username='******',
        password='******',
        pin_file='pynet-rtr2-pin.txt',
        port=8002
    )

    for a_rtr in (pynet_rtr1, pynet_rtr2):

        rtr_obj = NetworkDevice(**a_rtr)
        rtr_obj.establish_session()

        rtr_name = rtr_obj.net_element.properties.sys_name

        vty_service = VtyService(rtr_obj.net_element)
        vty_service.open()

        cmd = "terminal length 0"
        cli = vty_service.write(cmd)
        cmd = "show version"
        cli = vty_service.write(cmd)

        print "\n" + "*" * 80
        print "** CONNECTED TO: {}".format(rtr_name)
        print "*" * 80
        print cli
        print "*" * 80
        print "\n\n"

        rtr_obj.disconnect()
Ejemplo n.º 5
0
 def prepare(self):
     if not self.vty:
         if not self.ne._vty_service:
             self.ne._vty_service = VtyService(self.ne)
         self.vty = self.ne._vty_service
         if not self.vty.is_open():
             try:
                 self.vty._set_idle_timeout(self.timeout)
             except Exception as e:
                 self.log.info('Set idle timeout error ' + str(e))
             self.vty.open()
Ejemplo n.º 6
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"
def main():
    '''
    Using onePK and the VTY Service obtain the output from the 'show version'
    command.  Use 'terminal length 0' to disable output paging (i.e. disable
    '--More--' prompting).
    '''

    pynet_rtr1 = dict(ip='10.10.10.10',
                      username='******',
                      password='******',
                      pin_file='pynet-rtr1-pin.txt',
                      port=15002)

    pynet_rtr2 = dict(ip='10.10.10.10',
                      username='******',
                      password='******',
                      pin_file='pynet-rtr2-pin.txt',
                      port=8002)

    for a_rtr in (pynet_rtr1, pynet_rtr2):

        rtr_obj = NetworkDevice(**a_rtr)
        rtr_obj.establish_session()

        rtr_name = rtr_obj.net_element.properties.sys_name

        vty_service = VtyService(rtr_obj.net_element)
        vty_service.open()

        cmd = "terminal length 0"
        cli = vty_service.write(cmd)
        cmd = "show version"
        cli = vty_service.write(cmd)

        print "\n" + "*" * 80
        print "** CONNECTED TO: {}".format(rtr_name)
        print "*" * 80
        print cli
        print "*" * 80
        print "\n\n"

        rtr_obj.disconnect()
Ejemplo n.º 8
0
def main():

    pynet_rtr1 = dict(
        ip = '10.10.10.10',
        username = '******',
        password = '******',
        pin_file = 'pynet-rtr1-pin.txt',
        port = 15002
    )

    pynet_rtr2 = dict(
        ip = '10.10.10.10',
        username = '******',
        password = '******',
        pin_file = 'pynet-rtr2-pin.txt',
        port = 8002
    )

    for a_rtr in (pynet_rtr1, pynet_rtr2):

        rtr_obj = NetworkDevice(**a_rtr)
        rtr_obj.establish_session()

        rtr_name = rtr_obj.net_element.properties.sys_name

        vty_service = VtyService(rtr_obj.net_element)
        vty_service.open()

        CMD = "terminal length 0"
        cli = vty_service.write(CMD)
        CMD = "show version"
        cli = vty_service.write(CMD)

        print "\n" + "*" * 80
        print "** CONNECTED TO: {}".format(rtr_name)
        print "*" * 80
        print cli
        print "*" * 80
        print "\n\n"

        rtr_obj.disconnect()
Ejemplo n.º 9
0
 """
 import sys
 tutorial = VTYTutorial(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("VTYTutorial"):
         logger.error("Error in connecting to network element")
         sys.exit(1)
     logger.info("Done")
     """Get a VTYService from the NE"""
     #  START SNIPPET: create_vty
     vtyService = VtyService(tutorial.get_network_element())
     #  END SNIPPET: create_vty
     """Open the VTY on the NE with the default command interpreter"""
     #  START SNIPPET: open_vty
     vtyService.open()
     #  END SNIPPET: open_vty
     """Get a timeout of the VTY on NE"""
     #  START SNIPPET: get_vty_timeout
     logger.info("VTY Time Out - %s", vtyService.timeout)
     #  END SNIPPET: get_vty_timeout
     """Write a string to the VTY on NE"""
     #  START SNIPPET: write_vty_command
     TEST_CMD1 = "show onep status"
     cli_result = vtyService.write(TEST_CMD1)
     logger.info("Test Command : %s", TEST_CMD1)
     logger.info("CLI Result for Test Command : %s", cli_result)
Ejemplo n.º 10
0
        for configureIP in resolvedIPs:
            # need some tricks to make teh ACL modification work correctly
            # 1. remove any occurrence of IP in the ACL
            commandList+="no deny ip any "+configureIP+" 0.0.0.0\n"
            # 2. remove the entry with the index to modify
            commandList+="no "+str(i+1)+"\n"
            # 3. modify the entry
            commandList+=str(i+1)+" deny ip any "+configureIP+" 0.0.0.0\n"
            i+=1
        # clear all entries at the end of the ACL
        while i<maxACLLines:
            commandList+="no "+str(i+1)+"\n"
            i+=1
        commandList+=str(maxACLLines+1)+" permit any any\n"

        ne_vty=VtyService(ne)

        try:
            ne_vty.open()
        except:
            print("ERROR: Couldn't connect to VtyService "+ne.host_address)
        else:
            cli_result=ne_vty.write(commandList)

            ne_vty.close()
            ne.disconnect()

    #print the ACL to screen for visual check
    
    # ------ START - remove this for production -------
    print("ip access-list extended "+aclName)
Ejemplo n.º 11
0
 def check_target(self):
     logger.info("******* CHECKING MEDIATRACE CONFIG *******")
     try:
         vty = VtyService(self.get_network_element())
         vty.open()
         resp = vty.write('sho run')
         resping = vty.write('ping '+ self.dst_ip)
         vty.close()
         if resp:
             vty.open()
             if resp.find('mediatrace initiator source-ip') == -1:
                 logger.info('"mediatrace initiator ..." not set on target')
                 logger.info("******* ADDING INITIATOR SOURCE *******")
                 vty.write('conf t')
                 vty.write('mediatrace initiator source-ip '+ self.initiater_src)
             if resp.find('mediatrace responder') == -1:
                 logger.info('"mediatrace responder" not set on target ')
                 logger.info("******* ADDING RESPONDER *******")
                 vty.write('conf t)
                 vty.write('mediatrace responder')
             if resp.find('service set mediatrace') > -1:
                 if resping.find('Success rate is 0') > -1:
                     logger.info('Target cannot reach %s so ECHO '
                                 'traceroute_status will TIMEOUT',
                                 self.dst_ip)
                 return True
         raise Exception('forced fail')
     except Exception as e:
         logger.info("Target not setup correctly")
         res = raw_input('''
             Target test device must have the following configurations:
             -------------------------------------------------------------------
             |conf t                                                           |
             | mediatrace responder                                            |
             | mediatrace initiator source-ip <IP address of Target interface> |
             | onep                                                            |
             |  service set mediatrace                                         |
             -------------------------------------------------------------------
             Enter yes if config has been applied or quit to exit: 
             ''')
         if res.startswith('y'):
             return self.check_target()
         else:
             return False
     return True