【问题标题】:how do i get the number of clients from an Ap cisco and save it in a variable?如何从 Ap cisco 获取客户端数量并将其保存在变量中?
【发布时间】:2018-02-07 10:21:01
【问题描述】:

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

('---------->', DisplayString('', subtypeSpec=ConstraintsIntersection(ConstraintsIntersection(ConstraintsIntersection(ConstraintsIntersection(),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等都可以,因为我通过命令:

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

sudo snmpwalk.py -v 2c -c public 192.168.1.51 1.3.6.1.4.1.14179.2.1.1.18” 我可以在命令行获取客户端的数量

【问题讨论】:

    标签: python linux snmp pysnmp pyasn1


    【解决方案1】:

    如果你想用 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 关联的用户数量。

    【讨论】:

    • 谢谢llya的回复,但是没有用,结果应该是3,给了我1769。
    • @RicardoAlves 请原谅我,lexicographicMode=True 丢失了,所以你的所有 OID 都超过了 1.3.6.1.4.1.14179.2.1.4.1.7
    • 再次感谢您的回复,结果还是一样:)
    • @RicardoAlves 我建议打印出tuple(clients) 并查看报告了哪些 OID 以及它们是否对应于关联 AP 的数量。
    • @RicardoAlves 我建议您打印出tuple(clients) 内容以查看哪些 OID 到达那里。如果您对 SNMP 数据的使用是正确的,那么那里的 OID 值部分的数量应该反映关联客户端的数量。如果你的计数有误,可能是一些额外的 OID 值对以某种方式进入了那里。
    猜你喜欢
    • 1970-01-01
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 2021-01-12
    • 2016-03-06
    • 2021-04-17
    • 2018-11-11
    相关资源
    最近更新 更多