-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsing.py
More file actions
165 lines (117 loc) · 5.34 KB
/
Copy pathparsing.py
File metadata and controls
165 lines (117 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import re
# str1 = """2025-09-10 12:01:45 INFO User login from 192.168.1.10
# 2025-09-10 12:03:21 ERROR Failed connection attempt from 10.0.0.5
# 2025-09-10 12:01:45 INFO User login from 192.168.1.10
# 2025-09-10 12:03:21 ERROR Failed connection attempt from 10.0.0.5
# 2025-09-10 12:05:12 INFO User logout from 172.16.0.7
# 2025-09-10 12:07:33 WARN Suspicious traffic from 192.168.1.10"""
# pattern = r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
# matching = re.findall(pattern,str1)
# matchingunqiue = set(matching)
# for i in matchingunqiue:
# if matching.count(i) > 1 :
# print(i , ": ", matching.count(i))
# str2 = """
# Interface IP-Address OK? Method Status Protocol
# GigabitEthernet0/0 192.168.1.1 YES manual up up
# GigabitEthernet0/1 unassigned YES unset administratively down down
# FastEthernet0/0 10.0.0.1 YES manual up up
# Loopback0 127.0.0.1 YES manual up up
# Vlan1 172.16.0.1 YES manual down down
# Serial0/0/0 unassigned YES unset down down
# """
# pattern = r"(\S+\d+).*?(up|down|administratively down)"
# res = dict(re.findall(pattern,str2))
# for i,j in res.items() :
# print("'interface' :'" ,i, "'status : '" , j)
# print(type(res))
# st1 = """
# Router#show ip ospf neighbor Neighbor ID Pri State Dead Time Address Interface
# 100.1.1.1 1 FULL/BDR 00:00:35 2.0.0.1 GigabitEthernet0/0
# 102.1.1.1 1 FULL/DR 00:00:35 3.0.0.2 GigabitEthernet0/1
# 107.1.1.1 0 FULL/ 00:00:35 4.0.0.2 Serial0/0/0
# """
# patt = r'(\S+\d+)\s+\S+\s+\S+\s+(\S+\s+)\S+\d+\s+\S+\d+'
# res = re.findall(patt,st1)
# print(res)
# str1 = """
# Protocol Address Age (min) Hardware Addr Type Interface
# Internet 192.168.1.1 0 aabb.cc00.0100 ARPA GigabitEthernet0/0
# Internet 192.168.1.2 15 aabb.cc00.0200 ARPA GigabitEthernet0/1
# Internet 192.168.1.3 12 aabb.cc00.0300 ARPA GigabitEthernet0/2
# """
# pattern = r'\S+\s+(\S+\d+)\s+\d+\s+\S+\s+\S+\s+(\S+\d+)'
# res = re.findall(pattern,str1)
# for address,interface in res :
# print(address ,"is reachable via " , interface)
# print(res)
# str1 = """
# Interface IP-Address OK? Method Status Protocol
# FastEthernet0/0 192.168.1.1 YES manual up up
# FastEthernet0/1 unassigned YES unset administratively down down
# FastEthernet1/0 10.0.0.1 YES manual up up
# """
# # FastEthernet0/0 has IP 192.168.1.1 and is up/up
# pattern = r'(\S+\d+)\s+(\S+)\s+\S+\s+\S+\s+(administratively down |down|up)\s+(\S+)'
# res = re.findall(pattern,str1)
# for interface,ipaddress,status,protocol in res:
# print(interface,"has IP ",ipaddress ,"and is ",status,"/",protocol)
# print(res)
# str1 = """
# 10.0.0.0/24 is subnetted, 1 subnets
# C 10.0.0.0 is directly connected, FastEthernet0/0
# O 192.168.1.0/24 [110/2] via 10.0.0.2, 00:00:12, FastEthernet0/1
# O 172.16.0.0/16 [110/3] via 10.0.0.2, 00:00:10, FastEthernet0/1
# """
# # 192.168.1.0/24 reachable via 10.0.0.2 over FastEthernet0/1
# # 172.16.0.0/16 reachable via 10.0.0.2 over FastEthernet0/1
# pattern=r"\S+\s+(\S+\d+)\s+\S+\s+\S+\s+(\S+\d+)\,\s+\S+\s+(\S+\d+)"
# res = re.findall(pattern,str1)
# for ipaddress,viaip,interface in res:
# print(f"{ipaddress} reachable via {viaip} over {interface}")
# print(res)
# str1 = """
# Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd
# 10.0.0.2 4 65001 120 118 3 0 0 00:05:23 5
# 10.0.0.3 4 65002 200 199 3 0 0 00:10:42 8
# """
# # Neighbor 10.0.0.2 in AS 65001 has 5 prefixes
# pattern = r"(\S+\d+)\s+\S+\s+(\S+\d+)\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+(\S+)"
# res = re.findall(pattern,str1)
# for neighbor,asnumber,state in res:
# print(f"Neighbor {neighbor} in AS {asnumber} has {state} prefixes")
# print(res)
# str1 = """
# Mar 1 00:00:30.415: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
# Mar 1 00:01:12.123: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/1, changed state to down
# """
# # FastEthernet0/0 changed state to up
# # FastEthernet0/1 changed state to down
# pattern = r"(\S+\d+\/\d+)\S+\s+\S+\s+\S+\s+\S+\s+(\S+)"
# res = re.findall(pattern,str1)
# for interface, status in res:
# print(f'{interface} changed state to {status} ')
# print(res)
# str1 = """
# Tracing route to 8.8.8.8 over a maximum of 30 hops
# 1 2 ms 1 ms 1 ms 192.168.1.1
# 2 10 ms 11 ms 9 ms 10.0.0.1
# 3 25 ms 23 ms 22 ms 203.0.113.1
# 4 40 ms 39 ms 41 ms 8.8.8.8
# """
# pattern = r"\s+(\d+).*?(\S+\d+)$"
# res = re.findall(pattern,str1,re.MULTILINE)
# print(res)
str1 = """
VLAN Name Status Ports
1 default active Fa0/1, Fa0/2
10 Sales active Fa0/3, Fa0/4
20 Marketing active Fa0/5
"""
# VLAN 1 (default) active on ports Fa0/1, Fa0/2
# VLAN ID, Name, Ports)
pattern = r"\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+.*?)$"
res = re.findall(pattern,str1,re.MULTILINE)
for vlanid,name,status,ports in res:
print(f"VLAN {vlanid} ({name}) {status} on ports {ports} ")
print(res)