<commit_before># GETNEXT Command Generator
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.proto import rfc1902

# ( ( authData, transportTarget, varNames ), ... )
targets = (
    # 1-st target (SNMPv2c -- can't handle v1&v2c with equal communities)
    ( cmdgen.CommunityData('public'),  # , mpModel=0),
      cmdgen.UdpTransportTarget(('localhost', 161)),
      ('1.3.6.1.2.1', '1.3.6.1.3.1') ),
    # 2-nd target (SNMPv2c over IPv4/UDP)
    ( cmdgen.CommunityData('public'),
      cmdgen.UdpTransportTarget(('localhost', 161)),
      ('1.3.6.1.4.1', ) ),
    # 3-nd target (SNMPv3 over IPv4/UDP)
    ( cmdgen.UsmUserData('test-user', 'authkey1', 'privkey1'),
      cmdgen.UdpTransportTarget(('localhost', 161)),
      ('1.3.6.1.5.1', ) )
    # 4-th target (SNMPv3 over IPv6/UDP)
#    ( cmdgen.UsmUserData('test-user', 'authkey1', 'privkey1'),
#      cmdgen.Udp6TransportTarget(('::1', 161)),
#      ('1.3.6.1.6.1', ) )
    # N-th target
    # ...
    )

def cbFun(sendRequestHandle, errorIndication, errorStatus, errorIndex,
          varBindTable, cbCtx):
    (varBindHead, authData, transportTarget) = cbCtx
    print('%s via %s' % (authData, transportTarget))
    if errorIndication:
        print(errorIndication)
        return 1
    if errorStatus:
        print('%s at %s' % (
            errorStatus.prettyPrint(),
            errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
            )
        )
        return 1
    varBindTableRow = varBindTable[-1]
    for idx in range(len(varBindTableRow)):
        name, val = varBindTableRow[idx]
        if val is not None and varBindHead[idx] <= name:
            # still in table
            break
    else:
        print('went out of table at %s' % (name, ))
        return
    
    for varBindRow in varBindTable:
        for oid, val in varBindRow:
            if val is None:
                print(oid.prettyPrint())
            else:
                print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))

    return 1 # continue table retrieval

cmdGen  = cmdgen.AsynCommandGenerator()

for authData, transportTarget, varNames in targets:
    cmdGen.nextCmd(
        authData, transportTarget, varNames,
        # User-space callback function and its context
        (cbFun, (varNames, authData, transportTarget))
        )

cmdGen.snmpEngine.transportDispatcher.runDispatcher()
<commit_msg>use OIDs in tuple form for easier comparation<commit_after># GETNEXT Command Generator
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.proto import rfc1902

# ( ( authData, transportTarget, varNames ), ... )
targets = (
    # 1-st target (SNMPv2c -- can't handle v1&v2c with equal communities)
    ( cmdgen.CommunityData('public'),  # , mpModel=0),
      cmdgen.UdpTransportTarget(('localhost', 161)),
      ((1,3,6,1,2,1), (1,3,6,1,3,1)) ),
    # 2-nd target (SNMPv2c over IPv4/UDP)
    ( cmdgen.CommunityData('public'),
      cmdgen.UdpTransportTarget(('localhost', 161)),
      ((1,3,6,1,4,1), ) ),
    # 3-nd target (SNMPv3 over IPv4/UDP)
    ( cmdgen.UsmUserData('test-user', 'authkey1', 'privkey1'),
      cmdgen.UdpTransportTarget(('localhost', 161)),
      ((1,3,6,1,5,1), ) )
    # 4-th target (SNMPv3 over IPv6/UDP)
#    ( cmdgen.UsmUserData('test-user', 'authkey1', 'privkey1'),
#      cmdgen.Udp6TransportTarget(('::1', 161)),
#      ((1,3,6,1,6,1), ) )
    # N-th target
    # ...
    )

def cbFun(sendRequestHandle, errorIndication, errorStatus, errorIndex,
          varBindTable, cbCtx):
    (varBindHead, authData, transportTarget) = cbCtx
    print('%s via %s' % (authData, transportTarget))
    if errorIndication:
        print(errorIndication)
        return 1
    if errorStatus:
        print('%s at %s' % (
            errorStatus.prettyPrint(),
            errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
            )
        )
        return 1
    varBindTableRow = varBindTable[-1]
    for idx in range(len(varBindTableRow)):
        name, val = varBindTableRow[idx]
        if val is not None and varBindHead[idx] <= name:
            # still in table
            break
    else:
        print('went out of table at %s' % (name, ))
        return
    
    for varBindRow in varBindTable:
        for oid, val in varBindRow:
            if val is None:
                print(oid.prettyPrint())
            else:
                print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))

    return 1 # continue table retrieval

cmdGen  = cmdgen.AsynCommandGenerator()

for authData, transportTarget, varNames in targets:
    cmdGen.nextCmd(
        authData, transportTarget, varNames,
        # User-space callback function and its context
        (cbFun, (varNames, authData, transportTarget))
        )

cmdGen.snmpEngine.transportDispatcher.runDispatcher()

{'fraction_non_alphanumeric': 0.13642052565707133, 'fraction_numerical': 0.03170629954109303, 'mean_word_length': 2.6025544703230654, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 5, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '128525', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1587, 'n_words': 355}