Search code examples
snmppysnmp

why pysnmp "get next or nextcmd" showing only two interfaces?


In my virtual box there are four interfaces and loopback interface are present as below. eth0, eth1, eth2,eth3, lo.

VirtualBox:~/pysnmp# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:e1:eb:b9 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:10:b8:4b brd ff:ff:ff:ff:ff:ff
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:94:00:83 brd ff:ff:ff:ff:ff:ff
5: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:62:2b:9f brd ff:ff:ff:ff:ff:ff

When i am trying to get the interfaces info using pysnmp nextcmd, it always shows only two interfaces eth0, lo.

Using this example:

http://snmplabs.com/pysnmp/examples/hlapi/asyncore/sync/manager/cmdgen/table-operations.html#fetch-scalar-and-table-variables

from pysnmp.hlapi import *

for (errorIndication,
     errorStatus,
     errorIndex,
     varBinds) in nextCmd(SnmpEngine(),
                          CommunityData('public', mpModel=0),
                          UdpTransportTarget(('demo.snmplabs.com', 161)),
                          ContextData(),
                          ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')),
                          ObjectType(ObjectIdentity('IF-MIB', 'ifType')),
                          ObjectType(ObjectIdentity('IF-MIB', 'ifMtu')),
                          ObjectType(ObjectIdentity('IF-MIB', 'ifSpeed')),
                          ObjectType(ObjectIdentity('IF-MIB', 'ifPhysAddress')),
                          ObjectType(ObjectIdentity('IF-MIB', 'ifType')),
                          lexicographicMode=False):

    if errorIndication:
        print(errorIndication)
        break
    elif errorStatus:
        print('%s at %s' % (errorStatus.prettyPrint(),
                            errorIndex and varBinds[int(errorIndex)-1][0] or '?'))
        break
    else:
        for varBind in varBinds:
            print(' = '.join([x.prettyPrint() for x in varBind]))

Output:

root@VirtualBox:~/pysnmp# python tutorial.py 
IF-MIB::ifDescr.1 = lo
IF-MIB::ifType.1 = softwareLoopback
IF-MIB::ifMtu.1 = 16436
IF-MIB::ifSpeed.1 = 10000000
IF-MIB::ifPhysAddress.1 = 
IF-MIB::ifType.1 = softwareLoopback
IF-MIB::ifDescr.2 = eth0
IF-MIB::ifType.2 = ethernetCsmacd
IF-MIB::ifMtu.2 = 1500
IF-MIB::ifSpeed.2 = 100000000
IF-MIB::ifPhysAddress.2 = 00:12:79:62:f9:40
IF-MIB::ifType.2 = ethernetCsmacd

How about the remaining interfaces? It is not been displayed? Any ideas?


Solution

  • To fetch the interfaces in your local machine(virtual box), Modify the logic as below.

    CommunityData('public', mpModel=0),   < "Community string is what you configured in your local machine refer snmd.conf file">
    
    
    
    UdpTransportTarget(('localhost', 161)).    <Target IP address is our local host>