etingof / pysnmp

Python SNMP library
http://snmplabs.com/pysnmp/
BSD 2-Clause "Simplified" License
576 stars 195 forks source link

I'm trying to get information of some Printers using SNMP #464

Open igorcarmona-git opened 1 week ago

igorcarmona-git commented 1 week ago

Good afternoon!

I'm trying to get information from some printers using SNMP. I followed the steps in the official documentation, but when I run the code, it always says: "module 'pysnmp.hlapi' has no attribute 'getCmd'."

This is my code below:

import pysnmp.hlapi as hlapi

# OIDs Comuns
SerialNumber = '1.3.6.1.2.1.43.5.1.1.17.1'
totalPageCounter = '1.3.6.1.2.1.43.10.2.1.4.1.1'
tonerLevel = '1.3.6.1.2.1.43.11.1.1.9.1.1'

def get_snmp_data(ip, community, oid):
    for (errorIndication, errorStatus, errorIndex, varBinds) in hlapi.getCmd(
        hlapi.SnmpEngine(),
        hlapi.CommunityData(community, mpModel=0),  # mpModel=0 para SNMPv1 ou mpModel=1 para SNMPv2c
        hlapi.UdpTransportTarget((ip, 161)),
        hlapi.ContextData(),
        hlapi.ObjectType(hlapi.ObjectIdentity(oid))
    ):
        if errorIndication:
            print(f"Erro: {errorIndication}")
            return None
        elif errorStatus:
            print(f"Erro no status: {errorStatus.prettyPrint()} em {errorIndex}")
            return None
        else:
            for var_bind in varBinds:
                return var_bind[1].prettyPrint()

def main():
    print(get_snmp_data("192.168.1.150", "public", SerialNumber))

if __name__ == "__main__":
    main()

I'm using a Windows 10 system. errorCode

lextm commented 1 week ago

You must be reading some very old materials, as the latest releases 6.x/7.x have moved away completely from import pysnmp.hlapi as hlapi.

Please read about the latest status from #429 and this upgrade guide.

This repo is no longer in use, so new issues should go to https://github.com/lextudio/pysnmp/issues.

igorcarmona-git commented 1 week ago

I tried to follow these steps on this page: https://pysnmp.readthedocs.io/en/latest/quick-start.html. Was I in the wrong place?

lextm commented 1 week ago

That's surely the wrong place and some random guy set it up with incorrect branch from Ilya's repo.

429 covered that already.