-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesc.py
62 lines (50 loc) · 2.62 KB
/
desc.py
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
##
## Create interface descriptions based off LLDP neighbors
## show lldp neighbor on switch
## copy/paste meat of output containing IPs and hostnames to a file
## run this script against file
## python desc.py <file>
##
import sys
import re
inputFile = sys.argv[1]
try:
with open(inputFile) as fObj:
lldpNeighbors = fObj.readlines()
except FileNotFoundError:
lldpNeighbors = None
neighbors = []
for lldpNeighbor in lldpNeighbors:
# hostname, localInterface, remoteInterface = re.findall(r'^\(^[^SEP][a-zA-Z]+\d{1,})|(\w\w\d+\/\d+\/\d+)|(\w\w\d+\/\d+)', lldpNeighbor)
# (^[^SEP][a-zA-Z]+\d{1,})|(\w\w\d+\/\d+\/\d+)|(\w\w\d+\/\d+) - begining with alpha (not SEP), unlimited length, with at least 1 or more digits // can do Vlan interfaces but easier to pick up other crap
# (^[^SEP][a-zA-Z]{10}\d{0,})|(\w\w\d+\/\d+\/\d+)|(\w\w\d+\/\d+) - begining with alpha (not SEP), at least 10 or more, can have digits or not // can't do Vlan interfaces with this
# other portions of regex
# (\w\w\d+\/\d+\/\d+) - example Gi1/0/1 or Te1/1/1
# (\w\w\d+\/\d+) - example Gi0/1 or Fa0/1
# need to figure out how to handle Vlans and other misc crap
# maybe pass an argument for Vlans like -vlan or create a seperate script since that output comes from show int desc, not from show lldp nei
# also need to handle show cdp nei output
# hostname = re.findall(r'^[^SEP][a-zA-Z]+\d{1,}', lldpNeighbor) - failed on CENCO225
# added lowercase sep for phones and first 5 chars ({4} - 0 to 4) of hostname are alpha
# added hostname begin with Vl## (|^Vl\d+) for Vlans from show interface description
hostname = re.findall(r'^[^SEP|sep][a-zA-Z]{4}\w+|^Vl\d+', lldpNeighbor)
if hostname:
hostname = hostname[0].upper()
if 'WAP' in hostname:
alert = 0
localInterface = re.findall(r'\w\w\d+/\d+/\d+|\w\w\d+/\d+', lldpNeighbor)[0]
description = hostname
elif re.match(r'^VL\d{1,2}$', hostname): # maybe use re.search instead?
alert = 1
localInterface = hostname.replace('VL', 'Vlan')
description = re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\w+$', lldpNeighbor)[0]
elif re.search(r'\w\w\w\s\d+\/\d+\/\d+|\w\w\w\s\d+\/\d+', lldpNeighbor): # WTF is this for?
print('!!! FOUND ONE !!!')
else:
alert = 1
localInterface, remoteInterface = re.findall(r'\w\w\d+/\d+/\d+|\w\w\d+/\d+', lldpNeighbor)
description = '{0} {1}'.format(hostname, remoteInterface)
neighbors.append({'interface':localInterface, 'alert':alert, 'description':description})
for nei in neighbors:
print('interface {0}'.format(nei['interface']))
print('description {0}"TYPE":"LAN","MON":1,"ALERT":{1},"DESC":"{2}"{3}'.format('{', nei['alert'], nei['description'], '}'))