从Cisco设备返回多个序列号

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从Cisco设备返回多个序列号相关的知识,希望对你有一定的参考价值。

我正在解析show version命令以获取一系列信息。也许有更简单的方法,但我试图返回堆栈中设备的所有序列号。目前我只收回有源开关序列号。此外,我需要在多个区域搜索序列号。处理器板ID和系统序列号。

我在qazxsw poi上测试了以下正则表达式字符串,。*?^ System \ serial \ Number \ s ^系统序列号\ s([^,] +)

但在我的代码中,它们似乎并没有起作用。当我打印我的变量时,它通过For循环显示为空的所有迭代。

https://regex101.com

无论我想要什么样的Cisco设备来提取序列号,如果堆栈中有多个设备,则返回堆栈中设备的所有序列号。它还应返回IP,主机名,代码和模型版本。

答案

对于系统序列号,您的模式#!/usr/bin/python from getpass import getpass import netmiko import re def make_connection (ip, username, password): return netmiko.ConnectHandler(device_type='cisco_ios', ip=ip, username=username, password=password) def get_ip (input): return(re.findall(r'(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3} (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)', input)) def get_ips (file_name): #with does all the cleanup and prework of file open for you with open(file_name, 'r') as in_file: for line in in_file: #this is probably supposed to be lineips = get_ip(line) #line = get_ip(line) lineips = get_ip(line) for ip in lineips: ips.append(ip) def to_doc_a(file_name, varable): f=open(file_name, 'a') f.write(str(varable)) f.write('\n') f.close() def to_doc_w(file_name, varable): f=open(file_name, 'w', newline="\n") f.write(str(varable)) f.close() #This will be a list of the devices we want to SSH to ips = [] #Pull the IPs.txt is a list of the IPs we want to connect to #This function pulls those IPs out of the txt file and puts them into a #list get_ips('IPs.txt') #list where informations will be stored #devices = [] #Results string storage strresults = "" #Prompt user for account info username = input("Username: ") password = getpass() file_name = "results.csv" #Clearing all the old info out of the results.csv file to_doc_w(file_name, "") #Make a for loop to hit all the devices, for this we will be looking at #the IOS it’s running for ip in ips: #Connect to a device net_connect = make_connection(ip, username, password) #Run a command and set that to output output = net_connect.send_command('show version') #finding hostname in output using regular expressions regex_hostname = re.compile(r'(\S+)\suptime') hostname = regex_hostname.findall(output) #finding uptime in output using regular expressions regex_uptime = re.compile(r'\S+\suptime\sis\s(.+)') uptime = regex_uptime.findall(output) #finding version in output using regular expressions regex_version = re.compile(r'Cisco\sIOS\sSoftware.+Version\s([^,]+)') version = regex_version.findall(output) #finding serial in output using regular expressions regex_serial = re.compile(r'Processor\sboard\sID\s(\S+)') serial = regex_serial.findall(output) #finding serial in output using regular expressions regex_serial2 = re.compile(r'^System Serial Number\s([^,]+)') serial2 = regex_serial2.findall(output) print(serial2) #finding ios image in output using regular expressions #regex_ios = re.compile(r'System\s\image\s\file\sis\s"([^ "]+)') #ios = regex_ios.findall(output) #finding model in output using regular expressions regex_model = re.compile(r'[Cc]isco\s(\S+).*memory.') model = regex_model.findall(output) #append results to table [hostname,uptime,version,serial,ios,model] #devices.append([hostname[0], uptime[0], version[0], serial[0], #model[0]]) results = (ip, hostname, version, serial, serial2, model) #Store results for later, reduce calls to append file, greatly i #ncrease performance strresults = strresults + str(results) + "\n" #Next we will append the output to the results file #to_doc_a(file_name, results) to_doc_w(file_name, strresults) 使用锚来断言字符串的开头,以大写的^System Serial Number\s([^,]+)开头,并且在数字后缺少冒号Serial Number

您可以更新您的模式,其中:捕获的组匹配非空白字符的1倍以上。在你的模式中,你使用(\S+)来匹配不是逗号,但这也会匹配空格或换行符。

[^,]+

System serial number:\s(\S+) | Regex demo

以上是关于从Cisco设备返回多个序列号的主要内容,如果未能解决你的问题,请参考以下文章

替换从 VAST 代码返回的多个 HLS VOD 片段

如何从 FragmentPagerAdapter 返回多个片段

我需要从 cisco 设备中获取一些信息。它是 cisco-ios-xe 设备

为啥此代码片段返回意外结果?

什么是CISCO 设备oid?如何获取cisco设备各种OID

从片段返回到上一个活动 onclick back button