#!/usr/bin/env python2# Quick script to grab the system description using SNMP.# Grimhackerimportargparsefrompysnmp.entity.rfc3413.onelinerimportcmdgendefsnmp(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.exceptExceptionase: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)exceptExceptionase:print"WARNING: Failed execute SNMP command: {0}".format(e)else:returnerr_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 Noneif__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=[]ifnot(args.fileorargs.single):print"CRITICAL: Specify host or file of hosts."parser.print_usage()else:ifargs.file:withopen(args.file,"r")asf:forlineinf:hosts.append(line.strip())ifargs.single:hosts.append(args.single)iflen(hosts)>0:descriptions=[]forhostinhosts:err_indication,err_status,err_index,var_binds=snmp(host,community=args.community)iferr_indicationisNone:try:oid,description=var_binds[0]exceptExceptionase:print"WARNING: Failed to parse var_binds. {0}".format(e)else:description="{0}|{1}".format(host,description)printdescriptiondescriptions.append(description)else:description="{0}|FAILED TO RETRIEVE DESCRIPTION: {1}".format(host,err_indication)printdescriptiondescriptions.append(description)#print#print "*****"#for description in descriptions:# print descriptionelse:print"CRITICAL: No hosts to test."
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.