使用正则表达式提取并显示使用Python的所有接口的接口和方法状态


  •     Copyright (C) 2014-2018 Huawei Technologies Co., Ltd.         *
    
  •                       All rights reserved.                        *
    
  •           Without the owner's prior written consent,              *
    
  •    no decompiling or reverse-engineering shall be allowed.        *
    

Info: The max number of VTY users is 10, and the number
of current VTY users on line is 1.
The current login time is 2022-04-07 08:55:10+00:00.
dis interface brief
2022-04-07 08:55:10.620
PHY: Physical
*down: administratively down
(l): loopback
(s): spoofing
(b): BFD down
(d): Dampening Suppressed
InUti/OutUti: input utility/output utility
Interface PHY Protocol InUti OutUti inErrors outErrors
GigabitEthernet0/0/0 up up 0% 0% 0 0
GigabitEthernet1/0/0 up down 0% 0% 0 0
GigabitEthernet1/0/1 down down 0% 0% 0 0
GigabitEthernet1/0/2 down down 0% 0% 0 0
GigabitEthernet1/0/3 down down 0% 0% 0 0
GigabitEthernet1/0/4 down down 0% 0% 0 0
GigabitEthernet1/0/5 down down 0% 0% 0 0
GigabitEthernet1/0/6 down down 0% 0% 0 0
NULL0 up up(s) 0% 0% 0 0
Virtual-if0 up up(s) -- -- 0 0

用正则表达式将里面的接口信息interface phy 和 protocal 提取出来

试试这样:

import re
import pandas as pd
col=re.findall('(?:Interface).*',s,re.M)[0]
data=re.findall('(?:outErrors)(.*)',s,re.M|re.S)[0].strip()
df=pd.DataFrame(data=[d.split() for d in data.split('\n')],columns=col.split())
print(df)
Interface   PHY Protocol InUti OutUti inErrors outErrors
0  GigabitEthernet0/0/0    up       up    0%     0%        0         0
1  GigabitEthernet1/0/0    up     down    0%     0%        0         0
2  GigabitEthernet1/0/1  down     down    0%     0%        0         0
3  GigabitEthernet1/0/2  down     down    0%     0%        0         0
4  GigabitEthernet1/0/3  down     down    0%     0%        0         0
5  GigabitEthernet1/0/4  down     down    0%     0%        0         0
6  GigabitEthernet1/0/5  down     down    0%     0%        0         0
7  GigabitEthernet1/0/6  down     down    0%     0%        0         0
8                 NULL0    up    up(s)    0%     0%        0         0
9           Virtual-if0    up    up(s)    --     --        0         0
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632