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

标签 python linux snmp pysnmp pyasn1

我希望你能帮助我, 目标是使用 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” 我可以通过命令行获取客户端数量

最佳答案

如果你想用 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 前缀下的节点(行)数。我假设这反射(reflect)了与 AP 关联的用户数量。

关于python - 我如何从 Ap cisco 获取客户数量并将其保存在变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48661260/

相关文章:

python - 在 Linux 终端中重新着色特定应用程序

python - 如果一列中的第三个字母是 W,如何在 Pandas 中删除一行?

python - 对于 Project Euler,C++ 似乎比 Python Ruby 慢得多

c# - 在 Mono 中使用 Microsoft Chart (WinForm)

linux - boost::thread::try_join_for() 函数出错

java - 尝试了解 snmp4j Agent JMX 的作用以及它是否适合我的需要

python - Python如何检查一个句子是否包含某个词然后执行一个 Action ?

python - 在 Pandas 系列中制作缺失的时隙并填充 0 值

gosnmp 从 SNMP PDU 中获取八位字节串

java - 除了 SNMP4J 之外,还有免费的开源管理端 SNMP Java 库吗?