发布信息

lfl网络上是什么意思 (网络fl是什么意思啊)

     2023-05-31 12:29:21     46
7分钟前

对于一个运维人员来说,DevOps绝对是近几年最火的几个词之一。而python和ansible作为DevOps使用最多并且最简单的2种语言也成为运维工程师必须学会的语言(当然ansible并不算一种语言)。

作为一个老网工,对SDN的研究也或多或少的要和API、自动化脚本打交道。抛开SDN,当手里管理的设备太多,一个小的配置变化、设备每日巡检和备份的重复性工作会消耗大量时间,这时候python、ansible成为了解放双手的不二之选。本次给大家介绍kyanite常用的python脚本①批量更改配置②设备备份,由于本人只是个人爱好并不是

程序猿

出身,代码不会那么优雅,仅为网络工程师抛砖引玉。废话不多说,直接开始:

本次代码编辑和测试使用pycharm,环境介绍:excle格式的网络设备清单、python模块:netmiko(登录设备)、datetime(获取时间,为备份增加时间戳)、xlrd&xlwt(excle读取和写入)、re(正则表达式)、os(文件目录)

Step1:下载pycharm并导入相应模块:

下载地址:https://www.jetbrains.com/pycharm/

license:MTW881U3Z5-eyJsaWNlbnNlSWQiOiJNVFc4ODFVM1o1IiwibGljZW5zZWVOYW1lIjoiTnNzIEltIiwiYXNzaWduZWVOYW1lIjoiIiwiYXNzaWduZWVFbWFpbCI6IiIsImxpY2Vuc2VSZXN0cmljdGlvbiI6IkZvciBlZHVjYXRpb25hbCB1c2Ugb25seSIsImNoZWNrQ29uY3VycmVudFVzZSI6ZmFsc2UsInByb2R1Y3RzIjpbeyJjb2RlIjoiSUkiLCJwYWlkVXBUbyI6IjIwMTktMTEtMDYifSx7ImNvZGUiOiJBQyIsInBhaWRVcFRvIjoiMjAxOS0xMS0wNiJ9LHsiY29kZSI6IkRQTiIsInBhaWRVcFRvIjoiMjAxOS0xMS0wNiJ9LHsiY29kZSI6Il *** IiwicGFpZFVwVG8iOiIyMDE5LTExLTA2In0seyJjb2RlIjoiR08iLCJwYWlkVXBUbyI6IjIwMTktMTEtMDYifSx7ImNvZGUiOiJETSIsInBhaWRVcFRvIjoiMjAxOS0xMS0wNiJ9LHsiY29kZSI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gppoSV18XMxrWVdo *** 3nuMHwxGhFyde05OdDtLpCv+jlWf5REAHHA201pAU6b *** ZINyHDUTB+Beo28rRXSwSh3OUIvYwKNVeoBY+KwOJ7WnuTCUq1meE6GkKc4D/cXmgpOyW/1SmBz3XjVIi/zprZ0zf3qH5mkphtg6ksjKgKjmx1cXfZAAX6wcDBNaCL+Ortep1Dh8xDUbqbBVNBL4jbiL3i3x *** NiyJgaZ5sX7i8tmStEpLbPwvHcByuf59qJhV/bZOl8KqJBETCDJcY6O2aqhTUy+9x93ThKs1GKrRPePrWPluud7ttlgtRveit/pcBrnQcXOl1rHq7ByB8CFAxNotRUYL9IF5n3wJOgkPojMy6jetQA5Ogc8Sm7RG6vg1yow==

添加模块:file-settings-Project:pycharm-Project Interpreter

修改更新源:

修改源地址

添加相应模块:

添加模块

Step2:创建设备信息表:

设备信息

Step3:

File-New-Python file代码如下:

#!/usr/bin/python3.6# -*- coding=utf-8 -*-# keep studing!!from netmiko import ConnectHandlerimport xlrd,xlwtfrom netmiko import NetMikoAuthenticationException,NetmikoTimeoutErrorfrom datetime import datetimeimport reimport osnetwork_workbook = xlrd.open_workbook(r'E:likeyupythonpycharmstudynetworkdevice.xlsx')#打开Step2的Network Device excle表sheet = network_workbook.sheet_by_index(6)# 打开Network Device表的第一个sheet表并命名为sheetbctime = re.search("d{4}-d{2}-d{2}" , str(datetime.now())).group()pathstr_dir = r"E:likeyubackup"pathlist = [pathstr_dir, str(bctime)]join_path = "\".join(pathlist)fs_path = os.fspath(join_path)os.mkdir(fs_path)# 创建保存备份文件的目录for index in range(6,sheet.nrows):hostname = sheet.row(index)[0].valueip = sheet.row(index)[1].valueusername = sheet.row(index)[2].valuepassword = sheet.row(index)[3].valuevendor = sheet.row(index)[4].value# 遍历sheet,第一列赋值给hostname,第二列赋值给ip,以此类推device = {'device_type':vendor,'ip':ip,'username':username,'password':password,}# 定义设备的几项参数,为netmiko连接设备使用print('nn================ connect to ' + hostname + '========================')try:connect = ConnectHandler(**device)# 使用netmiko的connectHandler连接设备print('######## begin to config ' + hostname + '##########')runningconfig = connect.send_command('show run')openlist = [r'E:likeyubackup','\',bctime,'\',hostname,'_backup_',bctime,'.cfg']openpath = "".join(openlist)#创建backup的config文件夹with open(openpath,'w') as backup:backup.write(runningconfig)# 保存备份文件print('well done!!!!')connect.disconnect()except NetmikoTimeoutError:print("******** Someting wrong with the device " + hostname + "**************")# 连接超时异常except NetMikoAuthenticationException:print("******** Login erro with " + hostname + "**************")# 用户名密码错误异常print("n#################All devices h *** e backuped!!!#################")

执行代码如下:

备份路径E:likeyupythonpycharmstudybackup下的文件如下:

当设备数量庞大,优势就会更加明显,修改少量代码,可以实现批量修改固定的配置,如下代码可以批量修改NTP服务器、修改时区并保存:

#!/usr/bin/python3.6# -*- coding=utf-8 -*-# keep studing!!from netmiko import ConnectHandlerimport xlrd,xlwtfrom netmiko import NetMikoAuthenticationException,NetmikoTimeoutErrorfrom datetime import datetimeimport renetwork_workbook = xlrd.open_workbook(r'E:likeyupythonpycharmstudynetworkdevice.xlsx')sheet = network_workbook.sheet_by_index(0)for index in range(1,sheet.nrows):hostname = sheet.row(index)[0].valueip = sheet.row(index)[1].valueusername = sheet.row(index)[2].valuepassword = sheet.row(index)[3].valuevendor = sheet.row(index)[4].valuedevice = {'device_type':vendor,'ip':ip,'username':username,'password':password,}print('nn================ connect to ' + hostname + '========================')try:connect = ConnectHandler(**device)print('######## begin to config ' + hostname + '##########')configs = ['ntp server ntp.aliyun.com','clock timezone GTM +8','do wr']for config in configs:connect.send_config_set(config)# # connect.send_command('username admin pri 15 pass Cisco@123n')# input = connect.send_command('ntp server ntp.aliyun.comn')# print(input)# input = connect.send_command('clock timezone GTM +8n')# print(input)# bctime = re.search("d{4}-d{2}-d{2}" , str(datetime.now())).group()# print(bctime)# with open(r'E:likeyupythonpycharmstudybackupDev_' + hostname + '_backup_' + bctime+ '.cfg','w') as backup:# backup.write(runningconfig)print('config command: ' + config + 'n well done!')connect.disconnect()except NetmikoTimeoutError:print("******** Someting wrong with the device " + hostname + "**************")except NetMikoAuthenticationException:print("******** Login erro with " + hostname + "**************")

以上代码为大家提供参考,有任何问题可以提出交流,后续我们继续一起研究ansible对网络设备自动化的案例,欢迎大家转发关注。

原文链接:http://www.36sw.com/news/132355.html,转载和复制请保留此链接。
以上就是关于lfl网络上是什么意思 (网络fl是什么意思啊)全部的内容,关注我们,带您了解更多相关内容。
关键词: 设备 代码 模块