Snippets

grimhacker snmp_get_system_oid.py

Created by grimhacker
#!/usr/bin/env python2
# Quick script to grab the system description using SNMP.
# Grimhacker

import argparse

from pysnmp.entity.rfc3413.oneliner import cmdgen

def snmp(host, community="public", port=161, oid='1.3.6.1.2.1.1.1.0'):
    # defaul oid is 'System description' OID.
    try:
        command_generator = cmdgen.CommandGenerator()  # Create pysnmp command object.
    except Exception as e:
        print "WARNING: Failed to create SNMP command generator: {0}".format(e)
    else:
        try:
            # Use pysnmp to get the system description oid. If we can do this then the community string is correct.
            err_indication, err_status, err_index, var_binds = command_generator.getCmd(
                cmdgen.CommunityData(community),
                cmdgen.UdpTransportTarget((host, port)),
                oid
                )
        except Exception as e:
            print "WARNING: Failed execute SNMP command: {0}".format(e)
        else:
            return err_indication, err_status, err_index, var_binds
            #if err_indication is not None:
            #    #print "DEBUG: HOST: {0} | VAR_BINDS: {1}".format(host, var_binds)
            #    return var_binds
            #else:
            #    #print "ERROR: {0} | ERR_INDICATION: {1} | ERR_STATUS: {2} | ERR_INDEX: {3} | VAR_BINDS: {4}".format(host, err_indication, err_status, err_index, var_binds)
            #    return None

            
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="SNMP System Description Grabber")
    parser.add_argument("-f", "--file", help="File containing list of hosts, 1 per line.")
    parser.add_argument("-s", "--single", help="Host IP/name")
    parser.add_argument("-c", "--community", help="Community string. Default=public", default="public")
    args = parser.parse_args()

    hosts = []

    if not (args.file or args.single):
        print "CRITICAL: Specify host or file of hosts."
        parser.print_usage()
    else:
        if args.file:
            with open(args.file, "r") as f:
                for line in f:
                    hosts.append(line.strip())
        if args.single:
            hosts.append(args.single)

        if len(hosts) > 0:
            descriptions = []
            for host in hosts:
                err_indication, err_status, err_index, var_binds = snmp(host, community=args.community)
                if err_indication is None:
                    try:
                        oid, description = var_binds[0]
                    except Exception as e:
                        print "WARNING: Failed to parse var_binds. {0}".format(e)
                    else:
                        description = "{0}|{1}".format(host, description)
                        print description
                        descriptions.append(description)
                else:
                    description = "{0}|FAILED TO RETRIEVE DESCRIPTION: {1}".format(host, err_indication)
                    print description
                    descriptions.append(description)

            #print
            #print "*****"
            #for description in descriptions:
            #    print description

        else:
            print "CRITICAL: No hosts to test."

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.