def main(): """ Returns the System Hostname and Device Description using SNMP """ my_hosts = [{ 'ip_addr': '50.76.53.27', 'port': 7961, 'community': 'galileo' }, { 'ip_addr': '50.76.53.27', 'port': 8061, 'community': 'galileo' }] for host in my_hosts: oid = '1.3.6.1.2.1.1.5.0' my_device = (host['ip_addr'], host['community'], host['port']) snmp_data = snmp_get_oid(my_device, oid=oid) output = snmp_extract(snmp_data) print "Hostname:" print output + "\n" oid = '1.3.6.1.2.1.1.1.0' snmp_data = snmp_get_oid(my_device, oid=oid) output = snmp_extract(snmp_data) print "System Description:" print output print "-" * 80 print "\n"
def main(): ipaddr = '50.76.53.27' for i in (7961, 8061): print i,":\n"; print "name >>> ",snmp_helper.snmp_extract(snmp_helper.snmp_get_oid([ipaddr, "galileo", i], '.1.3.6.1.2.1.1.5.0')) print "sysdescr >>> ",snmp_helper.snmp_extract(snmp_helper.snmp_get_oid([ipaddr, "galileo", i], '.1.3.6.1.2.1.1.1.0'))
def main(): ''' ''' DEBUG = False COMMUNITY_STRING = 'secret' IP = '1.1.1.1' my_devices = { "Device1": (IP, COMMUNITY_STRING, 7061), "Device2": (IP, COMMUNITY_STRING, 8061), } #Uptime when running config last changed ccmHistoryRunningLastChanged = '1.3.6.1.4.1.9.9.43.1.1.1.0' # Uptime when running config last saved (note any 'write' constitutes a save) ccmHistoryRunningLastSaved = '1.3.6.1.4.1.9.9.43.1.1.2.0' # Uptime when startup config last saved ccmHistoryStartupLastChanged = '1.3.6.1.4.1.9.9.43.1.1.3.0' sys_uptime_oid = '1.3.6.1.2.1.1.3.0' for device_name, snmp_device in my_devices.items(): # Gather data from device snmp_data = snmp_get_oid(snmp_device, oid=sys_uptime_oid) sys_uptime = snmp_extract(snmp_data) uptime_hours = convert_uptime_hours(sys_uptime) snmp_data = snmp_get_oid(snmp_device, oid=ccmHistoryRunningLastChanged) last_run_change = int(snmp_extract(snmp_data)) snmp_data = snmp_get_oid(snmp_device, oid=ccmHistoryStartupLastChanged) last_start_save = int(snmp_extract(snmp_data)) # Determine whether run-start are in sync run_save_status = determine_run_start_sync_state(last_run_change, last_start_save) # Display Output print "\nDevice = %s" % device_name print "Current Uptime = %.1f hours" % uptime_hours if DEBUG: print "Run change time = %s" % last_run_change print "Last save time = %s" % last_start_save # check for a reboot and no save if not(last_start_save): print "This device has never been saved since the last reboot" else: if run_save_status: print "Running config has been saved" else: print "Running config not saved" print
def get_router_info(router_info,SYSNAME,SYSDESC): snmp_data = snmp_get_oid(router_info,oid=SYSNAME) router = snmp_extract(snmp_data) print "Router: %s" % router snmp_data = snmp_get_oid(router_info,oid=SYSDESC) output = snmp_extract(snmp_data) print "SYSDESC: %s" % output
def main(): ipaddr = '50.76.53.27' for i in (7961, 8061): print i, ":\n" print "name >>> ", snmp_helper.snmp_extract( snmp_helper.snmp_get_oid([ipaddr, "galileo", i], '.1.3.6.1.2.1.1.5.0')) print "sysdescr >>> ", snmp_helper.snmp_extract( snmp_helper.snmp_get_oid([ipaddr, "galileo", i], '.1.3.6.1.2.1.1.1.0'))
def getSnmpInfo(router,comm): rtr_ip = '50.76.53.27' if router == "rtr1": snmp_port = 7961 else: snmp_port = 8061 device = (rtr_ip,comm,snmp_port) rtr_name = snmp_get_oid(device,oid='.1.3.6.1.2.1.1.5.0',display_errors=True) rtr_desc = snmp_get_oid(device,oid='.1.3.6.1.2.1.1.1.0',display_errors=True) name_output = snmp_extract(rtr_name) desc_output = snmp_extract(rtr_desc) return(name_output,desc_output)
def main(): sysName = '.1.3.6.1.2.1.1.5.0' sysDescr = '.1.3.6.1.2.1.1.1.0' rtr1 = ('184.105.247.70', COMMUNITY, SNMP_PORT) rtr2 = ('184.105.247.71', COMMUNITY, SNMP_PORT) pynet.hr("pynet-rtr1") print('SysName: ' + snmp_extract(snmp_get_oid(rtr1, oid=sysName))) print('SysDescr: ' + snmp_extract(snmp_get_oid(rtr1, oid=sysDescr))).splitlines()[0] pynet.hr("pynet-rtr2") print('SysName: ' + snmp_extract(snmp_get_oid(rtr2, oid=sysName))) print('SysDescr: ' + snmp_extract(snmp_get_oid(rtr2, oid=sysDescr))).splitlines()[0]
def get_network_status(): """ Grabs network status (via SNMP for now) and creates the 'speech_output' """ session_attributes = {} card_title = "Network Status" # creating a few lists to write things to keys = [] values = [] list_offline = [] # community_string and snmp_port are set under global variables device = ('snmp.meraki.com', community_string, snmp_port) # snmp_data1 is the list of devNames in the SNMP get response # snmp_helper is imported on line 25, see snmp_helper.py in the example snmp_data1 = snmp_helper.snmp_get_oid(device, oid='.1.3.6.1.4.1.29671.1.1.4.1.2', display_errors=True) # snmp_data2 is the 0 or 1 value that comes back from this OID indicating # the device's online/offline status (0 = offline, 1 = online) snmp_data2 = snmp_helper.snmp_get_oid(device, oid='.1.3.6.1.4.1.29671.1.1.4.1.3', display_errors=True) """ create a dictionary of device names and their online/offline status. the following lines clean up the snmp responses in snmp_data1 and snmp_data2 individually, then add the sanitized data points to dict_status (snmp 'devName' and '0' or '1' for the status) """ for i in snmp_data1: k = snmp_helper.snmp_extract(i) keys.append(k) for j in snmp_data2: m = snmp_helper.snmp_extract(j) values.append(m) # create a new dictionary 'dict_status' with the combined name and status dict_status = dict(zip(keys, values)) # Now iterate through dict_status to capture offline devices for key in dict_status: value = dict_status[key] if value == '0': # below, 'devName' of offline devices (devStatus = 0) is appended to list_offline list_offline.append(key) else: # skip over devices which are not offline (any value other than 0) continue # Finally, count the length of 'list_offline' to get the number of offline # devices and read back each name in the list. # The extra spaces around the comma help Alexa read the names back clearly. # Adjustments may need to be made to fine tune the response. speech_output = "{0} devices are offline, {1}. would you like me to dispatch a technician ?".format( len(list_offline), " , ".join(list_offline)) reprompt_text = "" should_end_session = True return build_response(session_attributes, build_speechlet_response( card_title, speech_output, reprompt_text, should_end_session))
def main(): ''' Using SNMP Write a program that detects if the running configuration has been changed but not saved to startup-config. ''' ip_addr = '50.242.94.227' my_devices = { "pynet_rtr1": (ip_addr, COMMUNITY_STRING, 7961), "pynet_rtr2": (ip_addr, COMMUNITY_STRING, 8061), } sys_uptime_oid = '1.3.6.1.2.1.1.3.0' for device_name, snmp_device in my_devices.items(): # Gather data from device snmp_data = snmp_get_oid(snmp_device, oid=sys_uptime_oid) sys_uptime = snmp_extract(snmp_data) uptime_hours = convert_uptime_hours(sys_uptime) snmp_data = snmp_get_oid(snmp_device, oid=OID_RUN_LAST_CHANGED) last_run_change = int(snmp_extract(snmp_data)) snmp_data = snmp_get_oid(snmp_device, oid=OID_START_LAST_CHANGED) last_start_save = int(snmp_extract(snmp_data)) # Determine whether run-start are in sync run_save_status = determine_run_start_sync_state(last_run_change, last_start_save) # Display output print "\nDevice = %s" % device_name print "Current Uptime = %.1f hours" % uptime_hours if DEBUG: print "Run change time = %s" % last_run_change print "Last save time = %s" % last_start_save # Check for a reboot and no save if not last_start_save: print 'This device has never been saved since the last reboot' else: if run_save_status: print 'Running config has been saved' else: print 'Running config not saved' print
def check_routers(self): timestamp = int(time.time()) if DEBUG: print("My time is: %s" % time.ctime(timestamp)) for router in self.router_list: routerRebooted = False routerTuple = (router['ip_addr'], router['community'], router['port']) routerUptimeOIDData = snmp_helper.snmp_get_oid( routerTuple, SYSUPTIME_OID) routerUptime = int( float(snmp_helper.snmp_extract(routerUptimeOIDData)) / 100) routerRebootTime = timestamp - routerUptime lastConfigChangeSecondsOIDData = snmp_helper.snmp_get_oid( routerTuple, RUNNINGLASTCHANGED_OID) lastConfigChangeSeconds = int( float(snmp_helper.snmp_extract(lastConfigChangeSecondsOIDData)) / 100) runningLastChangedTime = routerRebootTime + lastConfigChangeSeconds if DEBUG: print( "Router: %s uptime: %s reboot time: %s last config change seconds: %s running last changed time %s" % (router['router'], routerUptime, time.ctime(routerRebootTime), lastConfigChangeSeconds, time.ctime(runningLastChangedTime))) if len(self.state) == 0: self.state['lastRebootTime'] = {} self.state['lastConfigChangeTime'] = {} if router['router'] not in self.state['lastRebootTime'].keys(): self.state['lastRebootTime'][ router['router']] = routerRebootTime self.state['lastConfigChangeTime'][ router['router']] = runningLastChangedTime if routerRebootTime > self.state['lastRebootTime'][ router['router']]: routerRebooted = True self.state['lastRebootTime'][ router['router']] = routerRebootTime if runningLastChangedTime > self.state['lastConfigChangeTime'][ router['router']]: if not routerRebooted or (lastConfigChangeSeconds > ROUTER_REBOOT_SECONDS): self.__send_email_alert(router['router'], runningLastChangedTime) self.state['lastConfigChangeTime'][ router['router']] = runningLastChangedTime self.__dump_state()
def get_snmp_info(device_ip): COMMUNITY_STRING = 'galileo' SNMP_PORT = 161 IP = device_ip SYSNAME = '1.3.6.1.2.1.1.5.0' SYSDESCR = '1.3.6.1.2.1.1.1.0' device = (IP, COMMUNITY_STRING, SNMP_PORT) snmp_sysname_data = snmp_helper.snmp_get_oid(device, oid=SYSNAME) snmp_sysdescr_data = snmp_helper.snmp_get_oid(device, oid=SYSDESCR) sysname_output = snmp_helper.snmp_extract(snmp_sysname_data) sysdescr_output = snmp_helper.snmp_extract(snmp_sysdescr_data) print sysname_output print sysdescr_output
def snmp_print_sysdescr(l_ip, l_port='161', l_community='public'): ''' Print snmp sysdescr ''' conn_dev = (l_ip, l_community, l_port) snmp_data = snmp_extract(snmp_get_oid(conn_dev, oid='1.3.6.1.2.1.1.1.0')) return snmp_data
def snmp_get(router, oids): data = {} for oid_name, oid in oids.items(): data['router_ip'] = router[0] data[oid_name] = snmp_extract(snmp_get_oid(router, oid)) print(data) return data
def main(): rtr1 = (IP, COMMUNITY, 7961) rtr2 = (IP, COMMUNITY, 8061) rtr1_sysname = snmp_helper.snmp_get_oid(rtr1, SYSNAME_OID) rtr1_sysdescr = snmp_helper.snmp_get_oid(rtr1, SYSDESCR_OID) rtr2_sysname = snmp_helper.snmp_get_oid(rtr2, SYSNAME_OID) rtr2_sysdescr = snmp_helper.snmp_get_oid(rtr2, SYSDESCR_OID) print "rtr1 sysname: %s\n" % snmp_helper.snmp_extract(rtr1_sysname) print "rtr1 sysdescr: %s\n" % snmp_helper.snmp_extract(rtr1_sysdescr) print "rtr2 sysname: %s\n" % snmp_helper.snmp_extract(rtr2_sysname) print "rtr2 sysdescr: %s\n" % snmp_helper.snmp_extract(rtr2_sysdescr)
def get_cell_status(): # community_string and snmp_port are set under global variables device = ('snmp.meraki.com', community_string, snmp_port) # devCellularStatus snmp_data = snmp_helper.snmp_get_oid(device, oid='.1.3.6.1.4.1.29671.1.1.4.1.14', display_errors=True) for row in snmp_data: # for each response with 'Active' cellular status, append the devName to cell_active_list if row[0][1] == 'Active': # dup of the original function to extract [0][0] vs. [0][1] extract_oid = snmp_helper.snmp_extract2(row) # replace the front of the oid to create a new oid for name lookup name_oid = extract_oid.replace('SNMPv2-SMI::enterprises.29671.1.1.4.1.14', '.1.3.6.1.4.1.29671.1.1.4.1.2') # lookup and extract the devName of the device with 'Active' cellular status # dup of the original function that issues a get vs. getNext snmp_name = snmp_helper.snmp_get_oid2(device, oid=name_oid, display_errors=True) dev_name = snmp_helper.snmp_extract(snmp_name) # append devName to cell_active_list cell_active_list.append(dev_name) # write devName to CSV csv_row_text = format(str(dev_name)) csv_writer.writerow([csv_row_text]) print "writing to CSV: %s" % csv_row_text # for watching live else: # ignoring anything without 'Active' status continue return cell_active_list
def main(args): parser = OptionParser() parser.add_option('-c', '--community', default='public', help='community string') parser.add_option('-D', '--debug', action='store_true', help='be verbose') options, args = parser.parse_args() data = {} for arg in args: host, port = arg.split(':') device = (host, options.community, port) data[arg] = {} for name, oid in OIDS.iteritems(): data[arg][name] = snmp_extract(snmp_get_oid(device, oid=oid)) for host in data: # From the mib: # If the value of ccmHistoryRunningLastChanged is greater than # ccmHistoryRunningLastSaved, the configuration has been # changed but not saved. if options.debug: print data[host] if data[host]['runningLastChanged'] > data[host]['runningLastSaved']: print "%s config modified (%s), but not saved (%s)" % ( host, data[host]['runningLastChanged'], data[host]['startupLastChanged'] )
def snmp_get(snmp_data, device): access_details = (device['IP'], COMMUNITY_STRING, SNMP_PORT) snmp_results = snmp_helper.snmp_get_oid(access_details, snmp_data['OID']) output = snmp_helper.snmp_extract(snmp_results) print("For "+device['name']+" the "+snmp_data['name']+" is "+'\n') print(output) print('\n')
def main(): ''' Create a script that connects to both routers (pynet-rtr1 and pynet-rtr2) and prints out both the MIB2 sysName and sysDescr. ''' try: ip_addr1 = raw_input("pynet-rtr1 IP address: ") ip_addr2 = raw_input("pynet-rtr2 IP address: ") except NameError: ip_addr1 = input("pynet-rtr1 IP address: ") ip_addr2 = input("pynet-rtr2 IP address: ") community_string = getpass.getpass(prompt="Community string: ") pynet_rtr1 = (ip_addr1, community_string, 161) pynet_rtr2 = (ip_addr2, community_string, 161) for a_device in (pynet_rtr1, pynet_rtr2): print("\n*********************") for the_oid in (SYS_NAME, SYS_DESCR): snmp_data = snmp_helper.snmp_get_oid(a_device, oid=the_oid) output = snmp_helper.snmp_extract(snmp_data) print(output) print("*********************") print()
def main(): """ Create a script that connects to both routers (pynet-rtr1 and pynet-rtr2) and prints out both the MIB2 sysName and sysDescr """ try: ip_addr1 = raw_input("pynet-rtr1 IP address: ") ip_addr2 = raw_input("pynet-rtr2 IP address: ") except NameError: ip_addr1 = input("pynet-rtr1 IP address: ") ip_addr2 = input("pynet-rtr2 IP address: ") community_string = getpass.getpass(prompt="Community string: ") # The library requires this type of tuple. # SYNTAX: (ip_addr, comm_string, SNMP default port) pynet_rtr1 = (ip_addr1, community_string, 161) pynet_rtr2 = (ip_addr2, community_string, 161) for device in [pynet_rtr1, pynet_rtr1]: print("\n************************") for oid in [SYS_NAME, SYS_DESCR]: snmp_data = snmp_get_oid(device, oid=oid) output = snmp_extract(snmp_data) print(output) print("\n************************") print()
def main(): for p in IP: for i in oid: a_device = (p, COMMUNITY_STRING, SNMP_PORT) snmp_data = snmp_get_oid(a_device, oid=i) output = snmp_extract(snmp_data) print output +"\n"
def main(): for p in IP: for i in oid: a_device = (p, COMMUNITY_STRING, SNMP_PORT) snmp_data = snmp_get_oid(a_device, oid=i) output = snmp_extract(snmp_data) print output + "\n"
def poll_print_oid(self, oid): # Combines both snmp_get_oid and snmp_extract into one class method self.oid = oid self.a_device = (self.ip_address, self.community, self.snmp_port) self.snmp_data = snmp_get_oid(self.a_device, self.oid) self.output = snmp_extract(self.snmp_data) return self.output
def get_snmp_data (router_data, oid): if router_data ['snmp_v'] < 3: a_device = ( router_data['IP'], router_data['SNMP Community'], router_data['SNMP Port'], ) snmp_data = snmp_get_oid ( a_device, oid) if snmp_data: return snmp_extract (snmp_data) if router_data ['snmp_v'] == 3: a_device = ( router_data['IP'], router_data['SNMP Port'], ) snmp_user = router_data ['snmp_user'] snmp_data = snmp_get_oid_v3 ( a_device, snmp_user, oid) if snmp_data: return snmp_extract (snmp_data) return None
def Get_OID(self, oid): ''' Given an SNMP OID, return the value in ASCII text :param oid: The SNMP OID :return: The value of the SNMP data in ASCII ''' snmp_data = snmp_get_oid(self.router_info, oid) return snmp_extract(snmp_data)
def snmp_getuptime(ip, communitystring, port): global uptime device = (ip, communitystring, port) uptime = int( snmp_extract( snmp_get_oid(device, oid=".1.3.6.1.2.1.1.3.0", display_errors=True))) / 60480000 return uptime
def snmp_get(snmp_data, device): access_details = (device['IP'], COMMUNITY_STRING, SNMP_PORT) snmp_results = snmp_helper.snmp_get_oid(access_details, snmp_data['OID']) output = snmp_helper.snmp_extract(snmp_results) print("For " + device['name'] + " the " + snmp_data['name'] + " is " + '\n') print(output) print('\n')
def main(): '''' Create a script that connects to both routers (pynet-rtr1 and pynet-rtr2) and prints out both the MIB2 sysName and sysDescr. ''' a_device = (ip_addr, comm_string, snmp_port) snmp_sys_name = snmp_get_oid(a_device, oid='1.3.6.1.2.1.1.5.0') snmp_sys_desc = snmp_get_oid(a_device, oid='1.3.6.1.2.1.1.1.0') output_sys_name = snmp_extract(snmp_sys_name) output_sys_desc = snmp_extract(snmp_sys_desc) print '\nDevice Name:' print output_sys_name print '\nSystem Description:' print output_sys_desc + '\n'
def snmp_query(node_info, oid): '''Query for OID on node - all necessary parameters should be part of node_info data structure ''' snmp_output = snmp_get_oid(node_info, oid) output = snmp_extract(snmp_output) return output
def main(): t_handler = t_login("pyclass", "88newclass", "", "184.105.247.70") print(t_command(t_handler, "term len 0").decode('ascii')) print(t_command(t_handler, "sh ver").decode('ascii')) t_handler.close snmp_data = snmp_helper.snmp_get_oid(a_device, OID) output = snmp_helper.snmp_extract(snmp_data) print(output)
def collect_snmp(community, port, hosts, verbose): DEBUG = False # Set all SNMP OID to collect OID_name = '1.3.6.1.2.1.1.5.0' OID_descr = '1.3.6.1.2.1.1.1.0' SYS_uptime = '1.3.6.1.2.1.1.3.0' IF_oper = '1.3.6.1.2.1.2.2.1.8.' IF_type = '1.3.6.1.2.1.2.2.1.3.' IF_last = '1.3.6.1.2.1.2.2.1.9.' IF_desc = '1.3.6.1.2.1.2.2.1.2.' for y in hosts: try: test = 0 x = 1 lastlist = [] intlist = [] sw = (y, community, port) if DEBUG: print "DEBUG: Connecting to switch with following parameters: {}\n".format( sw) snmp_data = snmp_get_oid(sw, oid=OID_name) print "*" * 80 print "Switch name: %s" % snmp_extract(snmp_data) snmp_data = snmp_get_oid(sw, oid=OID_descr) print "Switch description: %s" % "\n".join( textwrap.wrap(snmp_extract(snmp_data), 65)) snmp_data = snmp_get_oid(sw, oid=SYS_uptime) ticks = snmp_extract(snmp_data) seconds = float(ticks) / 100 sysup = timedelta(seconds=seconds) print "Systemuptime: %s" % sysup print "*" * 80 + "\n" while test == 0: snmp_data = snmp_get_oid(sw, oid=IF_type + str(x)) type = snmp_extract(snmp_data) if 'No Such Instance' in type: test = 1 else: oper = snmp_get_oid(sw, oid=IF_oper + str(x)) oper = snmp_extract(oper) if int(oper) == 2: last = snmp_get_oid(sw, oid=IF_last + str(x)) lastlist.append(snmp_extract(last)) descr = snmp_get_oid(sw, oid=IF_desc + str(x)) intlist.append(snmp_extract(descr)) x = x + 1 y = 0 for x in lastlist: down = float(ticks) - float(x) seconds = float(down) / 100 sysup1 = timedelta(seconds=seconds) print "Interface %s id down for: %s" % (intlist[y], sysup1) y = y + 1 except: if verbose == "y": print "Error occurred when connecting to switch: %s" % str(sw) pass
def main(args): parser = OptionParser() parser.add_option('-c', '--community', default='public', help='community string') options, args = parser.parse_args() for arg in args: host, port = arg.split(':') device = (host, options.community, port) for name, oid in OIDS.iteritems(): print snmp_extract(snmp_get_oid(device, oid=oid))
def get_hostname(device_ip, community_string): a_device=(device_ip, community_string, snmp_port) snmp_hostname = snmp_get_oid(a_device, oid='.1.3.6.1.2.1.1.5.0', display_errors=True) output = snmp_extract(snmp_hostname) if output == "No Such Instance currently exists at this OID": print 'No Such OID' sys.exit() else: hostname=output return hostname
def main(): pynet_rtr1=(IP,COMMUNITY_STRING,7961) pynet_rtr2=(IP,COMMUNITY_STRING,8061) for a_device in (pynet_rtr1,pynet_rtr2): for oid_data in (SYS_DESCRP,SYS_NAME): snmp_data=snmp_get_oid(a_device,oid=oid_data) output=snmp_extract(snmp_data) print '\n' print output
def main(): pynet_rtr1 = (IP, COMMUNITY_STRING, 7961) pynet_rtr2 = (IP, COMMUNITY_STRING, 8061) for a_device in (pynet_rtr1, pynet_rtr2): for oid_data in (SYS_DESCRP, SYS_NAME): snmp_data = snmp_get_oid(a_device, oid=oid_data) output = snmp_extract(snmp_data) print '\n' print output
def get_total_port(device_ip, community_string): a_device=(device_ip, community_string, snmp_port) ifcount = 0 for i in range(1,1000): snmp_ifType = snmp_get_oid(a_device, oid='.1.3.6.1.2.1.2.2.1.3.' + `i` , display_errors=True) output = snmp_extract(snmp_ifType) if output == "No Such Instance currently exists at this OID": return ifcount break else: ifcount = ifcount + 1
def collect_snmp(community, port, hosts, verbose): DEBUG= False # Set all SNMP OID to collect OID_name = '1.3.6.1.2.1.1.5.0' OID_descr = '1.3.6.1.2.1.1.1.0' SYS_uptime = '1.3.6.1.2.1.1.3.0' IF_oper = '1.3.6.1.2.1.2.2.1.8.' IF_type = '1.3.6.1.2.1.2.2.1.3.' IF_last = '1.3.6.1.2.1.2.2.1.9.' IF_desc = '1.3.6.1.2.1.2.2.1.2.' for y in hosts: try: test = 0 x = 1 lastlist = [] intlist = [] sw = (y, community, port) if DEBUG: print "DEBUG: Connecting to switch with following parameters: {}\n".format(sw) snmp_data = snmp_get_oid(sw, oid=OID_name) print "*" * 80 print "Switch name: %s" % snmp_extract(snmp_data) snmp_data = snmp_get_oid(sw, oid=OID_descr) print "Switch description: %s" % "\n".join(textwrap.wrap(snmp_extract(snmp_data),65)) snmp_data = snmp_get_oid(sw, oid=SYS_uptime) ticks = snmp_extract(snmp_data) seconds = float(ticks)/100 sysup = timedelta(seconds=seconds) print "Systemuptime: %s" % sysup print "*" * 80 + "\n" while test == 0: snmp_data = snmp_get_oid(sw, oid=IF_type+str(x)) type = snmp_extract(snmp_data) if 'No Such Instance' in type: test = 1 else: oper = snmp_get_oid(sw,oid=IF_oper+str(x)) oper = snmp_extract(oper) if int(oper) == 2: last = snmp_get_oid(sw, oid=IF_last+str(x)) lastlist.append(snmp_extract(last)) descr = snmp_get_oid(sw, oid=IF_desc+str(x)) intlist.append(snmp_extract(descr)) x = x+1 y = 0 for x in lastlist: down = float(ticks) - float(x) seconds = float(down)/100 sysup1 = timedelta(seconds=seconds) print "Interface %s id down for: %s" % (intlist[y], sysup1) y = y + 1 except: if verbose == "y": print "Error occurred when connecting to switch: %s" % str(sw) pass
def get_bandwidth(delay): snmp_data = snmp_get_oid(a_device, oid='.1.3.6.1.2.1.2.2.1.10.4', display_errors=True) first_data = snmp_extract(snmp_data) sleep(delay) snmp_data = snmp_get_oid(a_device, oid='.1.3.6.1.2.1.2.2.1.10.4', display_errors=True) second_data = snmp_extract(snmp_data) bps = ((float(second_data) - float(first_data)) * 8) / delay mbps = bps / 1024 / 1024 mbps = round(mbps, 2) return mbps
def main(): ip_addr = raw_input("ip address: ") community_string = getpass.getpass(prompt="Community String: ") switch = [ip_addr, community_string, 161] for a_device in switch: print("\n**********") for the_oid in (SYS_DESCR, SYS_NAME): snmp_data = snmp_helper.snmp_get_oid(a_device, oid=the_oid) output = snmp_helper.snmp_extract(snmp_data) print(output) print("**************")
def main(): ip_addr = raw_input("IP address: ") ip_addr = ip_addr.strip() community_string = getpass.getpass(prompt="Community string: ") pynet_rtr1 = (ip_addr, community_string, 7961) pynet_rtr2 = (ip_addr, community_string, 8061) for a_device in (pynet_rtr1, pynet_rtr2): print "\n*********************" for the_oid in (SYS_NAME, SYS_DESCR): snmp_data = snmp_helper.snmp_get_oid(a_device, oid=the_oid) output = snmp_helper.snmp_extract(snmp_data) print output print "*********************" print
def main(): COMMUNITY_STRING = "galileo" RTR1 = '184.105.247.70' RTR2 = '184.105.247.71' SNMP_PORT = 161 device1 = (RTR1, COMMUNITY_STRING, SNMP_PORT) device2 = (RTR2, COMMUNITY_STRING, SNMP_PORT) sysName_oid = '1.3.6.1.2.1.1.5.0' sysDescr_oid = '1.3.6.1.2.1.1.1.0' sysName_rtr1 = snmp_helper.snmp_get_oid(device1, oid=sysName_oid) sysDescr_rtr1 = snmp_helper.snmp_get_oid(device1, oid=sysDescr_oid) print(snmp_helper.snmp_extract(sysName_rtr1)) print(snmp_helper.snmp_extract(sysDescr_rtr1)) sysName_rtr2 = snmp_helper.snmp_get_oid(device2, oid=sysName_oid) sysDescr_rtr2 = snmp_helper.snmp_get_oid(device2, oid=sysDescr_oid) print(snmp_helper.snmp_extract(sysName_rtr2)) print(snmp_helper.snmp_extract(sysDescr_rtr2))
def main(): ip_address = raw_input("Provide the IP address: ") #ip_address = ip.addr.strip() comm_string = getpass.getpass(prompt="Provide the Community string: ") rtr1 = (ip_address, comm_string, 7961) rtr2 = (ip_address, comm_string, 8061) for device in (rtr1, rtr2): for each_oid in (SYS_NAME, SYS_DESCR): snmp_data = snmp_helper.snmp_get_oid(device, oid=each_oid) output = snmp_helper.snmp_extract(snmp_data) print output print
def probePdu(host=None, state=None, snmp_port=161, ro_community=None, rw_community=None, dccode=None, url=None): print 'host: %s state: %s' % (host, state) if state == 'closed': return False pdu = (host, ro_community, snmp_port) snmp_data = snmp_get_oid(pdu, oid='.1.3.6.1.2.1.1.1.0', display_errors=False) if snmp_data is None: return False output = snmp_extract(snmp_data) if output is None: return False data = {'ip_address': host, 'dccode': dccode} m = regsearch(r'SN.+([0-9A-Z]{12})', output) if m: data['real_serial'] = m.group(1) m = regsearch(r'MN:([0-9A-Z]+)', output) if m: data['real_model'] = m.group(1) m = regsearch(r'MB:([v0-9\.]+)', output) if m: data['mbver'] = m.group(1) m = regsearch(r'HR:([A-Z0-9]+)', output) if m: data['hwrev'] = m.group(1) m = regsearch(r'PF:([v0-9\.]+)', output) if m: data['apcver'] = m.group(1) m = regsearch(r'PN:(.*?)\.bin', output) if m: data['apcverfile'] = m.group(1) m = regsearch(r'AF1:([v0-9\.]+)', output) if m: data['appmodver'] = m.group(1) m = regsearch(r'AN1:(.*?)\.bin', output) if m: data['appmodverfile'] = m.group(1) r = reqpost(url, headers={'SB-Auth-Key': cfg.getConfigValue('pdu', 'api_key')}, json=data) print r.json()
def main(): ''' Create a script that connects to both routers (pynet-rtr1 and pynet-rtr2) and prints out both the MIB2 sysName and sysDescr. ''' for IP in IPS: a_device = (IP, COMMUNITY_STRING, SNMP_PORT) print for oid in (SYS_NAME, SYS_DESCR): snmp_data = snmp_get_oid(a_device, oid) output = snmp_extract(snmp_data) print output print
def main(): # My Variables # a_device is a tuple = (a_host, community_string, snmp_port) a_host = '50.76.53.27' comm_string = 'galileo' snmp_port1 = 7961 snmp_port2 = 8061 sysDescr = '.1.3.6.1.2.1.1.1.0' sysName = '.1.3.6.1.2.1.1.5.0' a_device1 = (a_host, comm_string, snmp_port1) a_device2 = (a_host, comm_string, snmp_port2) # get snmp data for rtr1 print '# RTR1' snmp_data1 = snmp_get_oid(a_device1, oid=sysDescr, display_errors=True) output_rtr1 = snmp_extract(snmp_data1) print output_rtr1 snmp_data1 = snmp_get_oid(a_device1, oid=sysName, display_errors=True) output_rtr1 = snmp_extract(snmp_data1) print output_rtr1 print '# RTR1' print # get snmp data for rtr2 print '# RTR2' snmp_data2 = snmp_get_oid(a_device2, oid=sysDescr, display_errors=True) output_rtr2 = snmp_extract(snmp_data2) print output_rtr2 snmp_data2 = snmp_get_oid(a_device2, oid=sysName, display_errors=True) output_rtr2 = snmp_extract(snmp_data2) print output_rtr2 print '# RTR2' print
def runcommands(a_device, list_of_oids): #Convert the OIDs into integers and Print ccmHistoryRunningLastChanged, ccmHistoryRunningLastSaved, ccmHistoryStartupLastChanged = list_of_oids results = [] for oid in list_of_oids: output = snmp_get_oid(a_device, oid=oid) results.append(int(snmp_extract(output))) output_StartupLastChanged, output_RunningLastSaved, output_RunningLastChanged = results #return results if output_StartupLastChanged == 0: print 'startup-config has not been saved since the last boot' elif output_RunningLastSaved > output_StartupLastChanged: print 'Running-Config has been changed but not saved to startup-config' else: print 'Running-Config has been saved to startup-config'
def get_available_port(device_ip, community_string, ifcount): a_device=(device_ip, community_string, snmp_port) snmp_hostname = snmp_get_oid(a_device, oid='1.3.6.1.2.1.1.5.0', display_errors=True) output_hostname = snmp_extract(snmp_hostname) print "Switch " + output_hostname + " at " + device_ip + " have following Ports avaiable for use." print "===============================================================================================" for i in range(1,ifcount): snmp_ifOutOctets = snmp_get_oid(a_device, oid='.1.3.6.1.2.1.2.2.1.16.' + `i` , display_errors=True) snmp_ifInOctets = snmp_get_oid(a_device, oid='.1.3.6.1.2.1.2.2.1.10.' + `i` , display_errors=True) output_ifOutOctets = snmp_extract(snmp_ifOutOctets) output_ifInOctets = snmp_extract(snmp_ifInOctets) if output_ifOutOctets == "0" and output_ifInOctets == "0": snmp_ifDescr = snmp_get_oid(a_device, oid='.1.3.6.1.2.1.2.2.1.2.' + `i`, display_errors=True) output_ifDescr = snmp_extract(snmp_ifDescr) interface_check = re.search( r'.*/1/.*', output_ifDescr, flags=0) stackinterface_check = re.search( r'StackPort.*', output_ifDescr, flags=0) if stackinterface_check or output_ifDescr == "Null0" or output_ifDescr == "GigabitEthernet0/0" or interface_check: pass else: switch = output_ifDescr.split('/') [0] switchno_len = len(switch) switchno = switch[switchno_len - 1] switchport = output_ifDescr.split('/') [2] print "Switch " + switchno + " --- Port# " + switchport
def main(): ip_addr1 = raw_input("enter pyrtr1 address: ") ip_addr2 = raw_input("enter pyrtr2 address: ") community = raw_input("enter community string: ") py_rtr1 = (ip_addr1, community, 161) py_rtr2 = (ip_addr2, community, 161) for device in (py_rtr1, py_rtr2): print "\n***" for the_oid in (NAME, DESCR): snmp_data = snmp_helper.snmp_get_oid(device, oid=the_oid) output = snmp_helper.snmp_extract(snmp_data) print output print "***"
def main(): """ Prompt user for IP address and community string. Returns output for SysDescr and SysName MIBs. """ # For reference # ip_addr = '50.76.53.27' # community = 'galileo' # Prompt for IP address and sanitize of trailing spaces ip_addr = raw_input("Please enter an IP: ") ip_addr = ip_addr.strip() # Prompt for community string, text will be masked using getpass community = getpass.getpass(prompt="Enter Community String: ") # Tuples for each router rt1 = (ip_addr, community, 7961) rt2 = (ip_addr, community, 8061) # List of routers device_list = [rt1, rt2] # List of OIDS: SysName, SysDescr oid_list = ["1.3.6.1.2.1.1.5.0", "1.3.6.1.2.1.1.1.0"] for device in device_list: print "\n********************" for oid in oid_list: snmp_data = snmp_get_oid(rt1, oid) output = snmp_extract(snmp_data) print output print "********************" print
def main(): ip_addr1 = '184.105.247.70' ip_addr2 = '184.105.247.71' community_string = 'galileo' device1 = (ip_addr1, community_string, 161) device2 = (ip_addr2, community_string, 161) for each_device in (device1, device2): print "*****************" for each_oid in (SYS_NAME, SYS_DESCRT): snmp_data = snmp_helper.snmp_get_oid(each_device, oid=each_oid) output = snmp_helper.snmp_extract(snmp_data) print output print "*****************"
def main(): " Main function definition" # " Creating snmp_devices tuples as defined by snmp_helper" pynet_rtr1 = (rtr1_ip, snmp_comm, snmp_port) pynet_rtr2 = (rtr2_ip, snmp_comm, snmp_port) print "\n" print "SNMP Information *********************** \n" for snmp_device in (pynet_rtr1, pynet_rtr2): print "Communicating with device " + colored(snmp_device, 'red') print "\n" for OID in (sysDescr, sysName): snmp_get = snmp_get_oid(snmp_device, oid=OID) snmp_response = snmp_extract(snmp_get) print colored(snmp_response, 'blue') print "\n" print "***************************************** \n"
def main(): OIDs = ['1.3.6.1.2.1.1.1.0', '1.3.6.1.2.1.1.5.0'] community_str = 'galileo' pynet_rtr1 = ('50.76.53.27', community_str, '7961') pynet_rtr2 = ('50.76.53.27', community_str, '8061') routers = [pynet_rtr1, pynet_rtr2] rtr_names = ['pynet_rtr1', 'pynet_rtr2'] count = 0 for i in routers: for j in OIDs: out = snmp_helper.snmp_get_oid(i, j) print "this is OID " + j + " for " + rtr_names[count] print snmp_helper.snmp_extract(out) + '\n' count += 1