Skip to content

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

@igorcarmona-git

Description

@igorcarmona-git

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions