有人能帮我从cisco路由器/交换机配置的"sh run“输出文本文件中只获取接口配置吗?
if not os.path.exists(Destpath):
os.makedirs(Destpath)
#time.sleep(10)
File_SList=os.listdir(sourcepath)
print(File_SList)
interface_pattern=re.compile(r'(interface \S+)|( ip helper-address [\d.]+)')
Number_File=0
for X in File_SList:
SpFile=X
DFile=X
print()
print(SpFile)
Number_File+=1
with open(os.path.join(sourcepath,SpFile),"r") as file :
Output=file.readlines()
print("Check")
helper_list=[]
with open(os.path.join(Destpath,DFile),"a") as put :
for key in Output:
key=key.lower()
if "interface" in key :
interface_name=key
print(key)
for key in Output:
if "!" not in key:
helper_list.append(key)
else :
break
for i in range(len(helper_list)) :
put.write(helper_list[i])
helper_list=[]从网络交换机的以下共享"sh run“输出,我们如何复制只与接口相关的配置(接口端口-通道1、接口GigabitEthernet1 0/0、接口FastEthernet1 1/0/1、接口FastEthernet1 1/0/2、接口GigabitEthernet1 1/0/1.)然后把它保存到另一个文件。
#
service tcp-keepalives-in
service tcp-keepalives-out
service timestamps debug datetime msec localtime show-timezone
service timestamps log datetime msec localtime show-timezone
service password-encryption
service sequence-numbers
service call-home
platform punt-keepalive disable-kernel-core
!
hostname Switch01
!
!
vrf definition Mgmt-vrf
!
address-family ipv4
exit-address-family
!
address-family ipv6
exit-address-family
!
logging userinfo
logging buffered 20000
!
stack-mac persistent timer 0
boot system bootflash:packages.conf
boot system flash:packages.conf
power redundancy-mode combined
power supply autoLC shutdown
power supply autoLC priority 1 2 5 6 7
!
ip arp gratuitous local
no ip gratuitous-arps
!
ip icmp rate-limit unreachable DF 200
no ip domain lookup
ip dhcp bootp ignore
!
no ip igmp snooping
login block-for 120 attempts 12 within 120
login quiet-mode access-class 109
login on-failure log
login on-success log
!
interface Port-channel1
description Po1
switchport trunk native vlan 998
switchport trunk allowed vlan 10-12,20,100-107,110,301-401,556,601,602,691
switchport trunk allowed vlan add 701-706,710,800,998,999
switchport mode trunk
!
interface Port-channel2
description Core Po2
switchport trunk native vlan 999
switchport trunk allowed vlan 10-12,20,100-106,110,301-306,401,601,602,691
switchport trunk allowed vlan add 701-706,710,800,998,999
switchport mode trunk
!
interface GigabitEthernet0/0
vrf forwarding Mgmt-vrf
no ip address
shutdown
negotiation auto
!
interface GigabitEthernet1/0/1
description VEDGE_1_UPLINK
switchport access vlan 901
switchport mode access
spanning-tree portfast
spanning-tree bpduguard enable
service-policy output QOS-OUTPUT-POLICY
!
interface FastEthernet1/0/1
description **Connected to PC**
switchport access vlan 111
switchport mode access
switchport port-security aging time 2
no logging event link-status
speed 100
duplex full
priority-queue out
mls qos trust dscp
no snmp trap link-status
no cdp enable
spanning-tree portfast
hold-queue 120 in
hold-queue 200 out
ip dhcp snooping trust
!
interface FastEthernet1/0/2
description **Connected to Access Point****
switchport access vlan 900
switchport mode access
switchport port-security maximum 15
switchport port-security
switchport port-security aging time 2
switchport port-security aging type inactivity
ip access-group 100 in
no logging event link-status
srr-queue bandwidth shape 0 0 0 10
queue-set 2
!
priority-queue out
no snmp trap link-status
storm-control broadcast level pps 100 50
storm-control multicast level pps 100 50
storm-control action trap
spanning-tree portfast
spanning-tree bpduguard enable
service-policy input WIRELESS_IN
ip dhcp snooping limit rate 15发布于 2022-07-10 20:28:42
以下是一个适合您的解决方案:
import os
sourcepath = "/your/path"
Destpath = "/your/other/path"
if not os.path.exists(Destpath):
os.makedirs(Destpath)
# The code below has been unindented: otherwise this code will run only when Despath
# does not exist!
File_SList = os.listdir(sourcepath)
print(File_SList)
for X in File_SList:
SpFile = X
DFile = X
print()
print(SpFile)
with open(os.path.join(sourcepath, SpFile), "r") as file:
Output = file.readlines()
print("Check")
helper_list = []
# Use 'w' to write, not 'a' to append because you would end up with the whole content
# of your original file plus your custom extraction below in that same file.
# I also unindented this `with` statement to make your code more readable.
with open(os.path.join(Destpath, DFile), "w") as put:
found_interface = False
# Loop only once through the file!
# (Notice there is no other `for key in Output` loop below)
for key in Output:
# Check if we have a start for a match, we assume no match by default
if found_interface is False:
key_lower = key.lower()
if "interface" in key_lower:
interface_name = key
print(key)
found_interface = True
# If you want to keep the interface in lowercase, use `key_lower`
helper_list.append(key)
# Important: while you are looping through each line, you do not want
# to check for the end of a match (`!`) until you are effectively inside
# an `interface` block, so don't run the rest of the logic in this loop
continue
# When `found_interface` is True, we know we want to capture any line until
# we get an exclamation point
if "!" not in key:
helper_list.append(key)
else:
# Here, we have reached the end of the `interface` block. There is no
# need to use range(len(...)), I have simplified your statement a bit.
for line in helper_list:
put.write(line)
helper_list = []
found_interface = False我在注释中指出了对代码所做的一些更改(并删除了与清晰度无关的内容),但最重要的是,主要的逻辑错误位于第二个for循环中:
for key in Output:
if "!" not in key:想法很清楚:只要您没有遇到一个interface块,就可以在每个!块内循环。但是,这意味着代码将不止一次地查看每一行,这将很快变得混乱!一个更好的方法是只循环一次文件的每一行,并跟踪您所在的位置。
换句话说,在这种方法中,标记匹配(interface),并一直追加一行直到找到不想要的东西(!)。在这一点上,您“取消”您的匹配,以便您不再附加行。对于随后的每一行,您将检查是否正在启动一个新的interface块。
希望这能有所帮助!干杯,欢迎!
https://stackoverflow.com/questions/72901686
复制相似问题