我正在解析show version命令以获取一系列信息。也许有任何更简单的方法,但是我试图返回堆栈中设备的所有序列号。目前,我只找回活动交换机的序列号。另外,我需要在多个区域中搜索序列号。处理器板ID和系统序列号。
我已经在https://regex101.com上测试了以下正则表达式字符串, 。*?^ System \ sSerial \ sNumber \ s ^系统序列号\ s([^,] +)
但是在我的代码中,它们似乎不起作用。当我打印变量时,它在For循环中的所有迭代中都显示为空。
#!/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)
无论我想用什么Cisco设备提取序列号,并且如果堆栈中有多个设备,请返回该堆栈中所有设备的序列号。还应该返回IP,主机名,代码版本和型号。
答案 0 :(得分:0)
对于系统序列号,您的模式^System Serial Number\s([^,]+)
使用锚来声明字符串的开头,以大写Serial Number
开头,并且在数字后缺少冒号:
。
您可以更新模式,其中(\S+)
捕获与非空格字符匹配1+倍的组。在您的模式中,您使用[^,]+
不匹配逗号,但是也匹配空格或换行符。
System serial number:\s(\S+)