def get_ifs_info_SNMP(address, number_if, community_name): if_list = {} i = 1 # i is the number of interface n = 1 # n is used to increment the cycle while n <= number_if: errorIndication, errorStatus, errorIndex, varBinds = next( getCmd( SnmpEngine(), CommunityData(community_name, mpModel=0), UdpTransportTarget((address, 161)), # Address and port ContextData(), ObjectType( ObjectIdentity("1.3.6.1.2.1.2.2.1.5." + ` i `) ), # IfSpeed (The last number is the interface ID) ObjectType(ObjectIdentity("1.3.6.1.2.1.2.2.1.2." + ` i `)), # ifTableEntry ) ) if errorIndication: print "Error indication:" print (errorIndication) continue elif errorStatus: print "Error Status" print ("%s at %s" % (errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or "?")) continue ifSpeed = varBinds[0].prettyPrint().split("= ", 1)[1] ifName = varBinds[1].prettyPrint().split("= ", 1)[1] try: if_speed_int = int(ifSpeed) n = n + 1 x = if_res(i, ifName, ifSpeed) if_list[i] = x except ValueError: if debug: print "No interface with ID ", i i = i + 1 oid = "1.3.6.1.2.1.4.20.1.2" errorIndication, errorStatus, errorIndex, varBindTable = cmdgen.CommandGenerator().bulkCmd( cmdgen.CommunityData("public"), cmdgen.UdpTransportTarget((address, 161)), 0, 25, oid, lookupMib=False ) # ipAddrTable OID i = 0 for varBindTableRow in varBindTable: for var in varBindTableRow: interface_ID = int(var[1]) ifAddress = str(var[0][-4:]) if debug: print interface_ID, ifAddress, if_list[interface_ID].get_id() if_list[interface_ID].set_address_if(ifAddress) i = i + 1 return if_list
def get_ifs_info_SNMP(address, number_if, community_name): if_list = {} i = 1 #i is the number of interface n = 1 #n is used to increment the cycle while(n<=number_if): errorIndication, errorStatus, errorIndex, varBinds = next( getCmd(SnmpEngine(),CommunityData(community_name, mpModel=0), UdpTransportTarget((address, 161)),#Address and port ContextData(), ObjectType(ObjectIdentity("1.3.6.1.2.1.2.2.1.5."+`i`)),#IfSpeed (The last number is the interface ID) ObjectType(ObjectIdentity("1.3.6.1.2.1.2.2.1.2."+`i`))#ifTableEntry )) if errorIndication: print 'Error indication:' print(errorIndication) continue elif errorStatus: print 'Error Status' print('%s at %s' % (errorStatus.prettyPrint(),errorIndex and varBinds[int(errorIndex)-1][0] or '?')) continue ifSpeed=varBinds[0].prettyPrint().split("= ",1)[1] ifName=varBinds[1].prettyPrint().split("= ",1)[1] try: if_speed_int=int(ifSpeed) n=n+1; x= if_res(i,ifName, ifSpeed) if_list[i] = x except ValueError: if (debug): print 'No interface with ID ',i; i=i+1; oidIPaddress = "1.3.6.1.2.1.4.20.1.2" oidIPmask = "1.3.6.1.2.1.4.20.1.3" #Ask for the interface id and the subnet mask, the ip address related to that interface is intergated in the response errorIndication, errorStatus, errorIndex, \ varBindTable = cmdgen.CommandGenerator().bulkCmd( cmdgen.CommunityData(community_name), cmdgen.UdpTransportTarget((address, 161)), 0, 25, oidIPaddress,oidIPmask, lookupMib = False) i = 0 for varBindTableRow in varBindTable: interface_ID = 0 for var in varBindTableRow: if (debug): print var if i % 2 == 0: #Row related to the ip address interface_ID = int(var[1]) ifAddress = str(var[0][-4:]) if (debug): print interface_ID, ifAddress if_list[interface_ID].set_address_if(ifAddress) else: #Row related to the subnet mask subnet = var[1].prettyPrint() if_list[interface_ID].set_subnet_if(subnet) i += 1 return if_list
def get_ifs_info_SNMP(address, number_if, community_name): """Given in input the router address, router interfaces number and the community name, the function returns the interfaces list through SNMP protocol. The interface list is a list of interfaces object. For more details see the class ifs_res""" if_list = {} i = 1 #i is the number of interface n = 1 #n is used to increment the cycle while(n<=number_if): errorIndication, errorStatus, errorIndex, varBinds = next( getCmd(SnmpEngine(),CommunityData(community_name, mpModel=0), UdpTransportTarget((address, SNMP_DEFAULT_PORT)),#Address and port ContextData(), ObjectType(ObjectIdentity(OID_IFSPEED+`i`)),#IfSpeed (The last number is the interface ID) ObjectType(ObjectIdentity(OID_IF_TABLE_ENTRY+`i`))#ifTableEntry )) if errorIndication: print 'Error indication:' print(errorIndication) continue elif errorStatus: print 'Error Status' print('%s at %s' % (errorStatus.prettyPrint(),errorIndex and varBinds[int(errorIndex)-1][0] or '?')) continue ifSpeed=varBinds[0].prettyPrint().split("= ",1)[1] ifName=varBinds[1].prettyPrint().split("= ",1)[1] try: if_speed_int=int(ifSpeed) n=n+1; x= if_res(i,ifName, ifSpeed) if_list[i] = x except ValueError: if (debug): print 'No interface with ID ',i; i=i+1; #Ask for the interface id and the subnet mask, the ip address related to that interface is intergated in the response errorIndication, errorStatus, errorIndex, \ varBindTable = cmdgen.CommandGenerator().bulkCmd( cmdgen.CommunityData(community_name), cmdgen.UdpTransportTarget((address, SNMP_DEFAULT_PORT)), 0, 25, OID_IP_ADDRESS,OID_IP_MASK, lookupMib = False) i = 0 for varBindTableRow in varBindTable: interface_ID = 0 for var in varBindTableRow: if (debug): print var if i % 2 == 0: #Row related to the ip address interface_ID = int(var[1]) ifAddress = str(var[0][-4:]) if (debug): print interface_ID, ifAddress if_list[interface_ID].set_address_if(ifAddress) else: #Row related to the subnet mask subnet = var[1].prettyPrint() if_list[interface_ID].set_subnet_if(subnet) i += 1 return if_list