0

I hope you can help me, The goal is to get the number of clients that have conected to the ap using pysnmp, I think I'm close, I know I have to use probably pyasn1, but I get to a part that gives me the following error:

('---------->', DisplayString('', subtypeSpec=ConstraintsIntersection(ConstraintsIntersection(ConstraintsIntersection(ConstraintsIntersection(),ValueSizeConstraint(0, 255)),ValueSizeConstraint(0, 255))))

My code is this:

from pysnmp.hlapi import *
from pysnmp.proto import rfc1905

setcommunity = "public"
host = "192.168.1.51"
oid = '1.3.6.1.4.1.1.4.1.14179.2.1.1.1.38'
ssid = "Cisco1852i"
snmp_engine = SnmpEngine()

#this function gets the interface status of the cisco Switch

def show_apClients():
       clients = nextCmd (snmp_engine,
               CommunityData(setcommunity),
               UdpTransportTarget((host, 161)),
               ContextData(),

       ObjectType(ObjectIdentity('SNMPv2-SMI', 'mib-2', '1.3.6.1.4.1.14179.2.1.1.1.38')))
       errorIndication, errorStatus, errorIndex, varBinds = next(clients) 
       numberClients = varBinds[0][1]
       print("----------->", numberClients)
       return numberClients

nClients = show_apClients()

print(".....------->", nClients)

I think the OID, the MIB and the rest are all right, because I through the command:

"sudo snmpwalk.py -v 2c -c public 192.168.1.51 1.3.6.1.4.1.14179.2.1.4.1.7 | wc -l"

or

"sudo snmpwalk.py -v 2c -c public 192.168.1.51 1.3.6.1.4.1.14179.2.1.1.18" I can get the command line the number of clients

1 Answers1

0

If you want to replicate this Net-SNMP command with pysnmp:

snmpwalk.py -v 2c -c public 192.168.1.51 1.3.6.1.4.1.14179.2.1.4.1.7 | wc -l

then I guess you should do something like this:

def show_apClients():
    clients = nextCmd(
        snmp_engine,
        CommunityData(setcommunity),
        UdpTransportTarget((host, 161)),
        ContextData(),
        ObjectType(ObjectIdentity('1.3.6.1.4.1.14179.2.1.4.1.7')),
        lexicographicMode=True
    )

    # this iterates over generator
    numberClients = len(tuple(clients))
    print("----------->", numberClients)
    return numberClients

The idea is that you let pysnmp walk the 1.3.6.1.4.1.14179.2.1.4.1.7 branch and return the number of nodes (rows) under that OID prefix. I assume that that reflects the number of users being associated with the AP.

Ilya Etingof
  • 5,440
  • 1
  • 17
  • 21
  • Thank you for the reply llya, but it didn't work, the result should be 3 and it is giving me 1769. – Ricardo Alves Feb 07 '18 at 12:09
  • @RicardoAlves Pardon me, `lexicographicMode=True` was missing so your got all the OIDs past `1.3.6.1.4.1.14179.2.1.4.1.7`. – Ilya Etingof Feb 10 '18 at 08:05
  • Thank you again for the reply, but the result is still the same :) – Ricardo Alves Feb 15 '18 at 17:51
  • @RicardoAlves I'd suggest to print out `tuple(clients)` and see which OIDs are reported and whether they correspond to the number of associated APs. – Ilya Etingof Feb 15 '18 at 20:29
  • @RicardoAlves I'd advise you to print out the `tuple(clients)` contents to see which OIDs get there. If your use of SNMP data is correct, the number of OID-value parts there should reflect the number of associated clients. If you are getting wrong count, perhaps some extra OID-value pairs get in there somehow. – Ilya Etingof Feb 20 '18 at 13:12