网络自动化之python(1):测试使用netmiko进行备份配置,及结合xlwt交换机上光模块SN进行统计
1、TOP如下:

2、脚本如下:
from netmiko import ConnectHandler
import xlwt
# from netmiko.ssh_exception import NetMikoTimeoutException
# from netmiko.ssh_exception import NetMikoAuthenticationException
# from getpass import getpass
# import threading
def get_mudel_info():
xl = xlwt.Workbook()
sheet1 = xl.add_sheet('sheet1')
sheet1.write(0, 0, 'IP')
sheet1.write(0, 1, 'interface')
sheet1.write(0, 2, 'Type')
sheet1.write(0, 3, 'PN')
sheet1.write(0, 4, 'SN')
print('sheet created successful!')
a = 0
b = 1
c = 2
d = 3
e = 4
r1 = 1
r2 = 1
print('-*-请先输入设备登录用户名及账号-*-')
user = input('Username:')
passwd = input('Password:')
ips = open(r".\ip_list.csv", encoding='utf-8')
hosts_info = ips.readlines()
for line_info in hosts_info:
x = r1
host_info = line_info.strip('').split(',')
ios = host_info[0]
ip = host_info[1].strip()
dev = {
"device_type":ios,
"host":ip,
"port":"22",
"username":user,
"password":passwd
}
print('>>>>')
print(f'___---___---___---___{ip} Start___---___---___---___')
try:
with ConnectHandler(**dev) as conne:
print(f'{ip} login successful!please waiting...')
# with open(r'.\logfiles\%s.txt' % ip.strip(),'w') as logfile:
# output = conne.send_command('')
# logfile.write(output)
output = conne.send_command_timing('dis trans man inte | inc Serial')
if '---- More ----' in output:
output += conne.send_command_timing(' \n',normalize=False)
print_infos = list(output.split('\n'))
output1 = conne.send_command_timing('dis tran man int | inc /0')
if '---- More ----' in output1:
output1 += conne.send_command_timing(' \n',normalize=False)
print_infos1 = list(output1.split('\n'))
for line_print in print_infos:
if ':' in line_print:
col_e = line_print.split(':')[1].strip()
sheet1.write(r1,a,ip)
sheet1.write(r1,e,col_e)
r1 += 1
for line_print1 in print_infos1:
if ':' in line_print1:
col_b = line_print1.split()[0].strip()
sheet1.write(r2,b,col_b)
r2 += 1
y = r1 - x
print(f'Transceiver of switch {ip} has %d(pcs)' % y)
print('There are %d(pcs) transceiver in total' % (r1-1))
print(f' ___---___---___---___{ip} END___---___---___---___')
except:
print(f'A wrong happening for {ip},please check it...')
print('**********************************************************')
# 此处可添加异常处理记录
continue
xl.save(r'.\test1.xls')
if __name__ == '__main__':
get_mudel_info()
3、测试前,在设备清单中放入一台不存在的设备IP;

4、测试结果如下两图:
4.1测试界面显示:

4.2 测试结果如表(手动查看显示模拟器上的光膜块SN是全部相同的,与表中结果一致):

5、补充更新1.0(添加任务选择:备份/统计光膜块SN)
from netmiko import ConnectHandler
import xlwt
# import time
# from netmiko.ssh_exception import NetMikoTimeoutException
# from netmiko.ssh_exception import NetMikoAuthenticationException
# from getpass import getpass
# import threading
def get_mudel_info():
print('-*-请先输入设备登录用户名及账号-*-')
user = input('Username:')
passwd = input('Password:')
ips = open(r".\ip_list.csv", encoding='utf-8')
hosts_info = ips.readlines()
xl = xlwt.Workbook()
sheet1 = xl.add_sheet('sheet1')
sheet1.write(0, 0, 'IP')
sheet1.write(0, 1, 'interface')
sheet1.write(0, 2, 'Type')
sheet1.write(0, 3, 'PN')
sheet1.write(0, 4, 'SN')
print('sheet created successful!')
a = 0
b = 1
c = 2
d = 3
e = 4
r1 = 1
r2 = 1
for line_info in hosts_info:
x = r1
host_info = line_info.strip('').split(',')
ios = host_info[0]
ip = host_info[1].strip()
dev = {
"device_type": ios,
"host": ip,
"port": "22",
"username": user,
"password": passwd
}
print('>>>>')
print(f'___---___---___---___{ip} Start___---___---___---___')
try:
with ConnectHandler(**dev) as conne:
print(f'{ip} login successful!please waiting...')
# with open(r'.\logfiles\%s.txt' % ip.strip(),'w') as logfile:
# output = conne.send_command('')
# logfile.write(output)
output = conne.send_command_timing('dis trans man inte | inc Serial')
if '---- More ----' in output:
output += conne.send_command_timing(' \n',normalize=False)
print_infos = list(output.split('\n'))
output1 = conne.send_command_timing('dis tran man int | inc /0')
if '---- More ----' in output1:
output1 += conne.send_command_timing(' \n',normalize=False)
print_infos1 = list(output1.split('\n'))
for line_print in print_infos:
if ':' in line_print:
col_e = line_print.split(':')[1].strip()
sheet1.write(r1,a,ip)
sheet1.write(r1,e,col_e)
r1 += 1
for line_print1 in print_infos1:
if ':' in line_print1:
col_b = line_print1.split()[0].strip()
sheet1.write(r2,b,col_b)
r2 += 1
y = r1 - x
print(f'Transceiver of switch {ip} has %d(pcs)' % y)
print('There are %d(pcs) transceiver in total' % (r1-1))
print(f' ___---___---___---___{ip} END___---___---___---___')
except:
print(f'A wrong happening for {ip},please check it...')
print('**********************************************************')
# 此处可添加异常处理记录
continue
xl.save(r'.\get_mudel_SN.xls')
def get_confg():
print('-*-请先输入设备登录用户名及账号-*-')
user = input('Username:')
passwd = input('Password:')
ips = open(r".\ip_list.csv", encoding='utf-8')
hosts_info = ips.readlines()
for line_info in hosts_info:
host_info = line_info.strip('').split(',')
ios = host_info[0]
ip = host_info[1].strip()
dev = {
"device_type": ios,
"host": ip,
"port": "22",
"username": user,
"password": passwd
}
print('>>>>')
print(f'___---___---___---___{ip} Start___---___---___---___')
try:
with ConnectHandler(**dev) as conne:
print(f'{ip} login successful!please waiting...')
with open(r'.\logfiles\%s.txt' % ip.strip(),'w') as logfile:
spe = '\n'.strip()
output = conne.send_command_timing('disp cur')
if '---- More ----' in output:
output += conne.send_command_timing(' \n', normalize=False)
logfile.write(output.replace('---- More ----','').strip())
logfile.close()
print(f' ___---___---___---___{ip} END___---___---___---___')
except:
print(f'A wrong happening for {ip},please check it...')
print('**********************************************************')
choice = True
while choice is not False:
choice = input('请选择要执行的任务["1" / "2" / "r"]:\n'
'[1]:统计光膜块信息\n'
'[2]:配置备份\n'
'[r]:退出任务\n'
'>>>')
if (choice.strip()) == '1':
get_mudel_info()
elif (choice.strip()) == '2':
get_confg()
elif (choice.strip()) == 'r':
print('已成功退出任务!欢迎下次使用!')
break
else:
print('您的选项不存在!请重新选择。或者输入"r"退出选择!')
choice = True
continue
本文来自博客园,作者:段愿仁长九,转载请注明原文链接:https://www.cnblogs.com/gaogaoing/p/15868776.html

浙公网安备 33010602011771号