当前位置: 首页 > news >正文

砀山县住房和城乡建设局网站联通专线做网站

砀山县住房和城乡建设局网站,联通专线做网站,wordpress图片菜单,淮南网站建设服务文章目录 介绍使用基本原理父子关系 属性ioscfg 获取配置信息,返回列表is_config_line 判断是否是配置行is_intf 判断IOSCfgLine是不是interfaceis_subintf 判断IOSCfgLine是不是子接口lineage 不知道用法is_ethernet_intf 判断IOSCfgLine是否是以太网接口is_loopback_intf 判断… 文章目录 介绍使用基本原理父子关系 属性ioscfg 获取配置信息,返回列表is_config_line 判断是否是配置行is_intf 判断IOSCfgLine是不是interfaceis_subintf 判断IOSCfgLine是不是子接口lineage 不知道用法is_ethernet_intf 判断IOSCfgLine是否是以太网接口is_loopback_intf 判断IOSCfgLine是不是loopback接口intf_in_portchannel 判断接口是否在port-channel中portchannel_number 返回port-channel的序号 方法find_objects() 检索文本find_objects_w_child在父对象中检索特定文本re_match 使用regex搜索IOSCfgLine文本并返回整数索引处的正则表达式组re_match_iter_typed()获取配置的值re_match_typedre_search 使用regex搜索此IOSCfgLine的文本re_search_children 使用regex搜索此子级中包含的文本 实例获取所有接口(interface)获取包含特定配置的父配置将接口名称和槽位建立关联获取接口名称和接口描述获取接口名称和IPv4地址 介绍 文档github虽然项目名称为ciscoconfparse,但可以分析华为等格式类似的设备安装 全新安装pip3 install ciscoconfparse升级安装pip3 install --upgrade ciscoconfparse安装指定版本pip install ciscoconfparse1.5.6使用 导入库创建对象代码from ciscoconfparse import CiscoConfParse file1 /mnt/share/bak/pc1/share/IDC网络运维表格/备份文件/20210506/SW03.txt cisco_cfg CiscoConfParse(file1)基本原理 父子关系 以缩进定义父子关系,示例policy-map QOS_1class GOLDpriority percent 10class SILVERbandwidth 30random-detectclass default !示例说明 第1行为父级第2 4 7 行为第1行的子级第3行是第2行的子级 属性 ioscfg 获取配置信息,返回列表 返回的是list类型示例from ciscoconfparse import CiscoConfParse config [!,interface port-channel1, ip address 112.54.105.38/30,!,interface Ethernet1/1, channel-group 1 mode active, no shutdown,interface Ethernet1/2, no shutdown,!,] parse CiscoConfParse(config) cfg parse.ioscfg print(type(cfg)) print(cfg)示例输出class list [!, interface port-channel1, ip address 112.54.105.38/30, !, interface Ethernet1/1, channel- group 1 mode active, no shutdown, interface Ethernet1/2, no shutdown, !]is_config_line 判断是否是配置行 如果是配置行返回True如果是空行、注释行么返回False示例from ciscoconfparse import CiscoConfParse config [interface Ethernet1/2, no shutdown,!,] parse CiscoConfParse(config) obj1 parse.find_objects(^!)[0] obj2 parse.find_objects(no shutdown)[0] print(obj1.is_config_line) print(obj2.is_config_line)is_intf 判断IOSCfgLine是不是interface 返回的是布尔值子接口也是接口示例from ciscoconfparse import CiscoConfParse config [!,interface Serial1/0, ip address 1.1.1.1 255.255.255.252,!,interface ATM2/0, no ip address,!,interface ATM2/0.100 point-to-point, ip address 1.1.1.5 255.255.255.252, pvc 0/100, vbr-nrt 704 704,!,] parse CiscoConfParse(config) obj parse.find_objects(^interface\sSerial)[0] print(obj.is_intf) obj parse.find_objects(^interface\sATM)[0] print(obj.is_intf)is_subintf 判断IOSCfgLine是不是子接口 用法同上 lineage 不知道用法 is_ethernet_intf 判断IOSCfgLine是否是以太网接口 返回布尔值任何以太网接口10M到10G都被视为以太网接口示例from ciscoconfparse import CiscoConfParse config [interface port-channel1, no shutdown,!,interface Ethernet1/2, no shutdown,!,] parse CiscoConfParse(config) obj1 parse.find_objects(interface port-channel1)[0] obj2 parse.find_objects(interface Ethernet1/2)[0] print(obj1.is_ethernet_intf ) print(obj2.is_ethernet_intf )示例输出False Trueis_loopback_intf 判断IOSCfgLine是不是loopback接口 判断是不是loopback接口返回的是布尔值示例from ciscoconfparse import CiscoConfParse config [!,interface FastEthernet1/0, ip address 1.1.1.1 255.255.255.252,!,interface Loopback0, ip address 1.1.1.5 255.255.255.255,!,] parse CiscoConfParse(config) obj parse.find_objects(r^interface\sFast)[0] print(obj.is_loopback_intf) obj parse.find_objects(r^interface\sLoop)[0] print(obj.is_loopback_intf)intf_in_portchannel 判断接口是否在port-channel中 返回的是布尔值,表示接口是否在port-channel中示例from ciscoconfparse import CiscoConfParse config [!,interface port-channel1, ip address 112.54.105.38/30,!,interface Ethernet1/1, channel-group 1 mode active, no shutdown,interface Ethernet1/2, no shutdown,!,] parse CiscoConfParse(config) interface1 parse.find_objects(^interface Ethernet1/1) interface2 parse.find_objects(^interface Ethernet1/2) print (interface1:,interface1[0].intf_in_portchannel) print (interface2:,interface2[0].intf_in_portchannel)示例输出interface1: True interface2: Falseportchannel_number 返回port-channel的序号 返回的是int类型如果不属于任何port-channel,则返回-1示例from ciscoconfparse import CiscoConfParse config [interface port-channel1, no shutdown,!,interface Ethernet1/1, channel-group 2 mode active, no shutdown,interface Ethernet1/2, no shutdown,!,] parse CiscoConfParse(config) obj1 parse.find_objects(^interface Ethernet1/1)[0] obj2 parse.find_objects(^interface Ethernet1/2)[0] print(obj1.portchannel_number) print(obj2.portchannel_number)示例输出2 -1方法 find_objects() 检索文本 在对象中查找特定文本语法配置对象.find_objects(文本内容(可用正则))示例 查找cisco-nxos中所有trunk组# 获取所有接口 interfaces cisco_cfg.find_objects(^interface port-channel) # 打印索引值为3的接口 print(interfaces[3]) # 打印所有接口名称 for obj in interfaces:print(obj.text)find_objects_w_child在父对象中检索特定文本 在父对象中检索特定文本语法配置对象.find_objects_w_child(parentspecr父对象包含的文本, childspecr子对象中包含的文本)示例,获取所有二层接口(N7K为例)# 获取所有二层接口 interfaces cisco_cfg.find_objects_w_child(parentspecr^interface, childspecrswitchport) # 打印所有接口名称 for obj in interfaces:print(obj.text)re_match 使用regex搜索IOSCfgLine文本并返回整数索引处的正则表达式组 介绍 使用regex搜索IOSCfgLine文本并返回整数索引处的正则表达式组 语法re_match(regex, group1, default)选项 选项类型说明regexstr字符串或python正则表达式应该匹配。此正则表达式应包含括号括号用于绑定匹配组groupint一个整数指定要返回的所需正则表达式组。组默认为1defaultstr如果不匹配则返回默认值。默认情况下如果不匹配则返回空字符串。返回结果str正则表达式组匹配的文本如果不匹配则返回默认值。 示例,打印接口下的子网掩码from ciscoconfparse import CiscoConfParse config [!,interface Serial1/0, ip address 1.1.1.1 255.255.255.248,!,interface Serial1/1, ip address 1.1.1.5 255.255.255.252,!,] parse CiscoConfParse(config) for obj in parse.find_objects(rip\saddress):netmask obj.re_match(r1\.1\.1\.5\s(\S)) print(The netmask is, netmask)示例输出The netmask is 255.255.255.252re_match_iter_typed()获取配置的值 语法re_match_iter_typed(regex, group1, result_typeclass str, default, untyped_defaultFalse, recurseFalse)选项 选项说明regex字符串,字符串或python正则表达式应该匹配。此正则表达式应包含括号括号用于绑定匹配组group整数,一个整数指定要返回的所需正则表达式组。组默认为1。result_type类型,一种类型通常是str、int、float或IPv4Obj中的一种。所有返回值都转换为结果类型默认为strdefault任意,如果不匹配则返回默认值untyped_default布尔值,如果不希望键入默认值请设置True 示例import re from ciscoconfparse import CiscoConfParse from ciscoconfparse.ccp_util import IPv4Obj config [!,interface Serial1/0, ip address 1.1.1.1 255.255.255.252,!,interface Serial2/0, ip address 1.1.1.5 255.255.255.252,!,] parse CiscoConfParse(config) INTF_RE re.compile(rinterface\s\S) ADDR_RE re.compile(rip\saddress\s(\S\s\S)) for obj in parse.find_objects(INTF_RE):print({} {}.format(obj.text, obj.re_match_iter_typed(ADDR_RE, result_typeIPv4Obj)))示例输出interface Serial1/0 IPv4Obj 1.1.1.1/30 interface Serial2/0 IPv4Obj 1.1.1.5/30re_match_typed 语法re_match_typed(regex, group1, untyped_defaultFalse, result_typeclass str, default)选项 选项类型说明regexstr字符串或python正则表达式应该匹配。此正则表达式应包含括号括号用于绑定匹配组groupint一个整数指定要返回的所需正则表达式组。组默认为1。result_typetype一种类型通常是str、int、float或IPv4Obj中的一种。所有返回值都转换为结果类型默认为str.defaultany如果不匹配则返回默认值untyped_defaultbool如果不希望键入默认值请设置True返回结果result_type正则表达式组匹配的文本如果不匹配则返回默认值。除非untyped\u default为True否则所有值都转换为结果类型 示例 见实例将接口名称和槽位建立关联 re_search 使用regex搜索此IOSCfgLine的文本 语法re_search(regex, default)选项 选项类型说明regexstr字符串或python正则表达式应该匹配。defaultstr如果在查找regex时re\u search未找到匹配项则返回的值返回结果str匹配的IOSCfgLine文本。如果不匹配则返回默认值 示例 re_search_children 使用regex搜索此子级中包含的文本 介绍 使用regex搜索此子级中包含的文本 语法re_search_children(regex, recurseFalse)选项 选项类型说明regexstr字符串或python正则表达式recursebool如果要搜索所有子级子级、孙子级、曾孙级等请设置为True返回结果list匹配的IOSCfgLine对象的列表。如果没有匹配项则返回空list 示例from ciscoconfparse import CiscoConfParse config [!,interface Serial1/0, ip address 1.1.1.1 255.255.255.252,!,interface Serial2/0, ip address 1.1.1.5 255.255.255.252,!,] parse CiscoConfParse(config) interfaces parse.re_search_children(regexr^interface\s(\S), recurseFalse) print(interfaces)示例输出[IOSCfgLine # 1 interface Serial1/0, IOSCfgLine # 4 interface Serial2/0]实例 获取所有接口(interface) 获取的是对象每个接口名称使用.text属性可基于interfaces的索引获取指定接口,获取的也是对象,接口名称是对象.text代码# 获取所有接口 interfaces cisco_cfg.find_objects(^interface) # 打印索引值为3的接口(对象),接口名使用对象.text print(interfaces[3]) # 打印所有接口名称 for obj in interfaces:print(obj.text)获取包含特定配置的父配置 获取no shutdown的接口方法一效率更高 方法一interfaces_All cisco_cfg.find_objects(r^interf) interfaces_AdminUp list() for obj in interfaces_All:if obj.re_search_children(rno shutdown):interfaces_AdminUp.append(obj) print(interfaces_AdminUp)方法二interface_NoShutdown cisco_cfg.find_parents_w_child(^interface, no shutdown) for i in interface_NoShutdown:print (i)将接口名称和槽位建立关联 名称将转换为str槽将转换为int代码from ciscoconfparse import CiscoConfParse config [!,interface Serial1/0, ip address 1.1.1.1 255.255.255.252,!,interface Serial2/0, ip address 1.1.1.5 255.255.255.252,!,] parse CiscoConfParse(config) slots dict() for obj in parse.find_objects(r^interface):name obj.re_match_typed(regexr^interface\s(\S),defaultUNKNOWN)slot obj.re_match_typed(regexrSerial(\d),result_typeint,default-1)print(Interface {0} is in slot {1}.format(name, slot))获取接口名称和接口描述 代码parse CiscoConfParse(file1) for obj in parse.find_objects_w_child(parentspecr^interface, childspecrdescript):name obj.textdesc obj.re_match_iter_typed(rdescription\s(.*), result_typestr)print(name,desc)获取接口名称和IPv4地址 支持同一接口下有多个IPv4地址代码from pathlib import Path from netaddr import IPNetwork from ciscoconfparse import CiscoConfParsefile Path(/mnt/share/00temp/01_DongHuan/config/核心交换机.txt) #file Path(/mnt/share/00temp/01_DongHuan/config/A508-xi-2.log) parse CiscoConfParse(file) # 使用ip address 192.168.1.1/24格式的正则 REGEX_ip1 rip\saddress\s(\S/\d{1,2}) # 使用ip address 192.168.1.1 255.255.255.0格式的正则 REGEX_ip2 rip\saddress\s(\S[\s]\d\S) # interface的正则 REGEX_interface r^interface\s(\S) for intf_obj in parse.find_objects(REGEX_interface):interface_name intf_obj.text.replace(interface ,)# 每个元素为一个IP地址IPv4s list()for child_obj in intf_obj.children: # Iterate over intf children#print(child_obj.text)if / in child_obj.text:REGEX_ip REGEX_ip1else:REGEX_ip REGEX_ip2ipv4 child_obj.re_match_typed(REGEX_ip)if ipv4:if in ipv4:ipv4 ipv4.split()ipv4 IPNetwork(f{ipv4[0]}/{ipv4[1]})IPv4s.append(str(ipv4))if IPv4s:# 显示接口有IPv4地址的接口名称,和IPv4(逗号分隔的ipv4地址)print(interface_name,,.join(IPv4s))
http://www.dnsts.com.cn/news/85156.html

相关文章:

  • wordpress建站工具360建筑网忘记密码怎么办
  • 湛江市手机网站建设企业开发者模式打开有什么影响
  • 网站建设 开票税率高端品牌网站
  • 浙江大学陈越做的刷题网站python网站开发框架
  • 购物网站建设 成都wordpress主题修改视频教程
  • 兰州网站在哪备案产品设计主要学什么
  • 网站产品推广旅游电子商务 网站建设
  • 网站dns如何修改不了网网站建设的销售怎么做
  • 一女被多男做的视频网站地域名网址ip查询
  • 网站建设报价怎么差别那么大普宁17网站一起做淘宝
  • 做网站 学什么怎么导入视频到wordpress
  • 有人情味的网站企业网站空间
  • asp程序网站后台发布产品的时候前台怎么不显示产品名称国内跨境电商建站系统
  • wordpress简介企业模板网站建设乐云seo效果好
  • 容桂网站建设找顺的云南技术网站建设销售
  • 长沙高端网站建设服务wordpress的字体颜色如何更改
  • 国内高端大气的网站设计怎么做QQ信任网站
  • 公司做网站需要哪些资料足球排名最新排名世界
  • 军队信息化建设网站上海4a广告公司排名
  • 提供企业网站建设定制网站集约化建设会议议程
  • 信誉好的昆明网站建设湖南正规竞价优化服务
  • 注册建设通网站手机免费建站教程
  • 网站描述设置华硕建设公司网站
  • 宝安高端网站建设公司小白学编程应该从哪里开始学
  • 如何用vps做网站南宁工作室注册
  • 公司想制作网站企业自助建站系统源码
  • 手机域名做网站中国谁第一家手机软件开发的模式
  • 服装网站建设规划方案如何做好网络营销?
  • 东莞做网站的公司徐州一级资质钢结构网架公司
  • 关于网站开发的会议纪要网页qq官网