[Tutor] How to get ipRouteTable from Cisco router?

Костиков Сергей Николаевич SERKOS at avalon.ru
Thu Apr 29 17:17:01 CEST 2010


Hello! 

I want to get route tables from Cisco routers in the network. What i 
have: 

import re 

from pysnmp.entity.rfc3413.oneliner import cmdgen 

s = r'(%s)' % ('(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.)\ 
{3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)') 
pattern = re.compile(s) 
file = 'routers.txt' 
s = open(file).read() 
i = 0 
router_list = [] 
while True: 
    match = pattern.search(s, i) 
    if match: 
        router_list.append(match.group(1)) 
        i = match.end() + 1 
    else: 
        break 

class router: 
    def __init__(self, who): 
        self.name = who 

    routetable = {} 

router1 = router(router_list[0]) 

cmdGen = cmdgen.CommandGenerator() 
errorIndication, errorStatus, errorIndex, varBindTable = 
cmdGen.nextCmd( 
    cmdgen.CommunityData('test-agent', public, 0), 
    cmdgen.UdpTransportTarget((router1.name, 161)), 
    (1,3,6,1,2,1,4,21,1,1)) 

if errorIndication: 
        print errorIndication 
else: 
    if errorStatus: 
            print '%s at %s\n' % 
(errorStatus.prettyPrint(),varBindTable[-1][int(errorIndex)-1]) 
    else: 
        for varBindTableRow in varBindTable: 
            for oid, val in varBindTableRow: 
                 print varBindTableRow 

Result: 

Code: Select all 
[(ObjectName('1.3.6.1.2.1.4.21.1.1.0.0.0.0'), IpAddress('0.0.0.0'))] 
[(ObjectName('1.3.6.1.2.1.4.21.1.1.10.9.0.0'), IpAddress('10.9.0.0'))] 
[(ObjectName('1.3.6.1.2.1.4.21.1.1.192.168.1.0'), 
IpAddress('192.168.1.0'))] 

How can i get IpAddress values from this list and put they in the 
dictionary? Or may be there is much better solution?


More information about the Tutor mailing list