如何从Ap cisco获取客户机数量并将其保存在变量中?

2024-04-18 20:45:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望你能帮我, 我们的目标是获得使用pysnmp连接到ap的客户机的数量,我想我已经接近了,我知道我必须使用pyasn1,但是我得到了一个部分,该部分给出了以下错误:

('------------>;',DisplayString('',subspec=constraintIntersection(constraintIntersection(constraintIntersection(),ValueSizeConstraint(0,255)),ValueSizeConstraint(0,255)))

我的代码是:

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)

我认为OID,MIB和其他的都很好,因为我通过命令:

苏多snmpwalk.py文件-v 2c-c公共192.168.1.51 1.3.6.1.4.1.14179.2.1.4.1.7 | wc-l

或者

苏多snmpwalk.py文件-v 2c-c公共192.168.1.51 1.3.6.1.4.1.14179.2.1.1.18“ 我可以得到命令行的客户数量


Tags: thefromimporthost数量showenginesnmp
1条回答
网友
1楼 · 发布于 2024-04-18 20:45:21

如果要用pysnmp复制此Net SNMP命令:

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

我想你应该这样做:

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

其思想是让pysnmp遍历1.3.6.1.4.1.14179.2.1.4.1.7分支并返回该OID前缀下的节点(行)数。我假设这反映了与AP相关联的用户数量。你知道吗

相关问题 更多 >