从Cisco设备返回多个序列号

问题描述 投票:1回答:1

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

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

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

https://regex101.com

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

regex python-3.x
1个回答
0
投票

对于系统序列号,您的模式#!/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

© www.soinside.com 2019 - 2024. All rights reserved.