Skip to content
Back to top
View this page
Edit this page

# Advanced Command Generator

## SNMPv3: master auth and privacy keys

Send SNMP GET request using the following options:

- with SNMPv3, user ‘usr-md5-des’, MD5 authentication, DES encryption

- use master auth and privacy keys instead of pass-phrase

- over IPv4/UDP

- to an Agent at demo.pysnmp.com:161

- for SNMPv2-MIB::sysDescr.0 MIB object instance

Functionally similar to:

$ snmpget -v3 -l authPriv -u usr-md5-des -3m 0x1dcf59e86553b3afa5d32fd5d61bf0cf -3M 0xec5ab55e93e1d85cb6846d0f23e845e0 demo.pysnmp.com SNMPv2-MIB::sysDescr.0

    import asyncio
    from pysnmp.hlapi.v3arch.asyncio import *

    async def run_snmp_get():
        iterator = await get_cmd(
            SnmpEngine(),
            UsmUserData(
                "usr-md5-des",
                authKey=OctetString(hexValue="1dcf59e86553b3afa5d32fd5d61bf0cf"),
                privKey=OctetString(hexValue="ec5ab55e93e1d85cb6846d0f23e845e0"),
                authKeyType=USM_KEY_TYPE_MASTER,
                privKeyType=USM_KEY_TYPE_MASTER,
            ),
            await UdpTransportTarget.create(("demo.pysnmp.com", 161)),
            ContextData(),
            ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
        )

        errorIndication, errorStatus, errorIndex, varBinds = iterator

        if errorIndication:
            print(errorIndication)

        elif errorStatus:
            print(
                "{} at {}".format(
                    errorStatus.prettyPrint(),
                    errorIndex and varBinds[int(errorIndex) - 1][0] or "?",
                )
            )

        else:
            for varBind in varBinds:
                print(" = ".join([x.prettyPrint() for x in varBind]))

    asyncio.run(run_snmp_get())

`Download` script.

## SNMPv3: localized auth and privacy keys

Send SNMP GET request using the following options:

- with SNMPv3, user ‘usr-md5-des’, MD5 authentication, DES encryption

- use localized auth and privacy keys instead of pass-phrase or master keys

- configure authoritative SNMP engine ID (0x0000000000 can be used as well)

- over IPv4/UDP

- to an Agent at demo.pysnmp.com:161

- for SNMPv2-MIB::sysDescr.0 MIB object instance

Functionally similar to:

$ snmpget -v3 -l authPriv -u usr-md5-des -e 0x80004fb805636c6f75644dab22cc -3k 0x6b99c475259ef7976cf8d028a3381eeb -3K 0x92b5ef98f0a216885e73944e58c07345 demo.pysnmp.com SNMPv2-MIB::sysDescr.0

    import asyncio
    from pysnmp.hlapi.v3arch.asyncio import *

    async def run_snmp_get():
        iterator = await get_cmd(
            SnmpEngine(),
            UsmUserData(
                "usr-md5-des",
                authKey=OctetString(hexValue="6b99c475259ef7976cf8d028a3381eeb"),
                privKey=OctetString(hexValue="92b5ef98f0a216885e73944e58c07345"),
                authKeyType=USM_KEY_TYPE_LOCALIZED,
                privKeyType=USM_KEY_TYPE_LOCALIZED,
                securityEngineId=OctetString(hexValue="80004fb805636c6f75644dab22cc"),
            ),
            await UdpTransportTarget.create(("demo.pysnmp.com", 161)),
            ContextData(),
            ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
        )

        errorIndication, errorStatus, errorIndex, varBinds = iterator

        if errorIndication:
            print(errorIndication)

        elif errorStatus:
            print(
                "{} at {}".format(
                    errorStatus.prettyPrint(),
                    errorIndex and varBinds[int(errorIndex) - 1][0] or "?",
                )
            )

        else:
            for varBind in varBinds:
                print(" = ".join([x.prettyPrint() for x in varBind]))

    asyncio.run(run_snmp_get())

`Download` script.

## Concurrent queries

Send multiple SNMP GET requests at once using the following options:

- with SNMPv2c, community ‘public’

- over IPv4/UDP

- to multiple Agents at demo.pysnmp.com

- for instance of SNMPv2-MIB::sysDescr.0 MIB object

- based on asyncio I/O framework

Functionally similar to:

$ snmpget -v2c -c public demo.pysnmp.com:161 SNMPv2-MIB::sysDescr.0
$ snmpget -v2c -c public demo.pysnmp.com:161 SNMPv2-MIB::sysDescr.0
$ snmpget -v2c -c public demo.pysnmp.com:161 SNMPv2-MIB::sysDescr.0

    import asyncio
    from pysnmp.hlapi.v3arch.asyncio import *

    async def getone(snmpEngine, hostname):
        errorIndication, errorStatus, errorIndex, varBinds = await get_cmd(
            snmpEngine,
            CommunityData("public"),
            await UdpTransportTarget.create(hostname),
            ContextData(),
            ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
        )

        if errorIndication:
            print(errorIndication)
        elif errorStatus:
            print(
                "{} at {}".format(
                    errorStatus.prettyPrint(),
                    errorIndex and varBinds[int(errorIndex) - 1][0] or "?",
                )
            )
        else:
            for varBind in varBinds:
                print(" = ".join([x.prettyPrint() for x in varBind]))

    async def main():
        snmpEngine = SnmpEngine()
        await asyncio.gather(
            getone(snmpEngine, ("demo.pysnmp.com", 161)),
            getone(snmpEngine, ("demo.pysnmp.com", 161)),
            getone(snmpEngine, ("demo.pysnmp.com", 161)),
        )

    asyncio.run(main())

`Download` script.

## Sequential queries

Send multiple SNMP GET requests one by one using the following options:

- with SNMPv2c, community ‘public’

- over IPv4/UDP

- to multiple Agents at demo.pysnmp.com

- for instance of SNMPv2-MIB::sysDescr.0 MIB object

- based on asyncio I/O framework

Functionally similar to:

$ snmpget -v2c -c public demo.pysnmp.com:161 SNMPv2-MIB::sysDescr.0
$ snmpget -v2c -c public demo.pysnmp.com:161 SNMPv2-MIB::sysDescr.0
$ snmpget -v2c -c public demo.pysnmp.com:161 SNMPv2-MIB::sysDescr.0

    import asyncio
    from pysnmp.hlapi.v3arch.asyncio import *

    async def getone(snmpEngine, hostname):
        errorIndication, errorStatus, errorIndex, varBinds = await get_cmd(
            snmpEngine,
            CommunityData("public"),
            await UdpTransportTarget.create(hostname),
            ContextData(),
            ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
        )

        if errorIndication:
            print(errorIndication)
        elif errorStatus:
            print(
                "{} at {}".format(
                    errorStatus.prettyPrint(),
                    errorIndex and varBinds[int(errorIndex) - 1][0] or "?",
                )
            )
        else:
            for varBind in varBinds:
                print(" = ".join([x.prettyPrint() for x in varBind]))

    async def getall(snmpEngine, hostnames):
        for hostname in hostnames:
            await getone(snmpEngine, hostname)

    snmpEngine = SnmpEngine()

    asyncio.run(
        getall(
            snmpEngine,
            [
                ("demo.pysnmp.com", 161),
                ("demo.pysnmp.com", 161),
                ("demo.pysnmp.com", 161),
            ],
        )
    )

`Download` script.

See also: library reference.