+# Quick script to grab the system description using SNMP.
+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.
+ command_generator = cmdgen.CommandGenerator() # Create pysnmp command object.
+ print "WARNING: Failed to create SNMP command generator: {0}".format(e)
+ # 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)),
+ print "WARNING: Failed execute SNMP command: {0}".format(e)
+ 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)
+ # #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)
+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()
+ if not (args.file or args.single):
+ print "CRITICAL: Specify host or file of hosts."
+ with open(args.file, "r") as f:
+ hosts.append(line.strip())
+ hosts.append(args.single)
+ err_indication, err_status, err_index, var_binds = snmp(host, community=args.community)
+ if err_indication is None:
+ oid, description = var_binds[0]
+ print "WARNING: Failed to parse var_binds. {0}".format(e)
+ description = "{0}|{1}".format(host, description)
+ descriptions.append(description)
+ description = "{0}|FAILED TO RETRIEVE DESCRIPTION: {1}".format(host, err_indication)
+ descriptions.append(description)
+ #for description in descriptions:
+ print "CRITICAL: No hosts to test."