-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlldp-playbk.bak
executable file
·192 lines (157 loc) · 4.8 KB
/
lldp-playbk.bak
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'''
LLDP Mermaid App
Python Script to run Ansible Playbook, providing a key word to target the site
- generating a LLDP-based Mermaid Network Diagram
'''
# Import argv to provide an argument in python script
# Using "subprocess" to run commands (i.e. "ansible-playbook nameofplay.yml")
from sys import argv
import subprocess
import os
'''
My function for formatting
'''
def dev_shape(item):
fw = ["(", ")"]
sw = ["[","]"]
rtr = ["((","))"]
if "sw" in item:
sw.insert(1,item)
formatted_dev = (''.join(sw))
str(formatted_dev)
elif "pan" in item:
fw.insert(1,item)
formatted_dev = (''.join(fw))
str(formatted_dev)
else:
rtr.insert(1,item)
formatted_dev = (''.join(rtr))
str(formatted_dev)
def arista_dev(line):
my_list = line.split()
del my_list[-1]
local_port = my_list[0]
local_port = local_port.strip('"')
neigh_port = my_list[2]
neigh_port = neigh_port[0:2]+neigh_port[-2:]
lldp_neigh = my_list[1]
lldp_neigh = lldp_neigh.split(".")[0]
lldp_link = "%s -->|%s <br><br>%s|%s\n" % (target_dev, local_port, neigh_port, lldp_neigh)
# Insert your function to check for duplicate links, here.
A = local_port
B = (neigh_port[0:2]+neigh_port[-2:])
link_pair = A + B
reverse_pair = B + A
if link_pair in links or reverse_pair in links:
#print("Found a link duplicate.")
pass
else:
links.append(link_pair)
links.append(reverse_pair)
#print("Adding to MD file.")
lldp_diagram.write(lldp_link)
#Insert code to write to MD file here.
#lldp_diagram.write(lldp_link)
def juniper_dev(line):
my_list = line.split()
if my_list[-1] == '",':
del my_list[-1]
local_port = my_list[0]
local_port = local_port.strip('"')
neigh_port = my_list[-2]
neigh_port = neigh_port[0:2]+neigh_port[-2:]
lldp_neigh = my_list[-1]
lldp_neigh = lldp_neigh.strip('",')
lldp_neigh = lldp_neigh.split(".")[0]
lldp_link = "%s -->|%s <br><br>%s|%s\n" % (target_dev, local_port, neigh_port, lldp_neigh)
lldp_diagram.write(lldp_link)
# Variable to use throughout script
user_input = argv
p = subprocess
hyphen = "-"
colon = ":"
neigh_port = ""
links = []
'''
Testing via Ansible ad-hoc command:
Example of ansible command:
ansible-playbook ~/myansible/lldp.yml --limit "hostname-here-sw"
'''
# This one works:
site = user_input[1]
#site = ""
myCmd = "ansible-playbook /Users/diegoavalos/myansible/lldp.yml --limit %s >> raw.txt" % (site)
# Used for testing ; bypassing sysargv variables
# myCmd = "ansible-playbook /Users/diegoavalos/myansible/lldp.yml --limit 'hostname-here-sw' >> raw.txt"
# Telling user to wait while command runs.
print("... stay funky for about 5 seconds.")
# Run ansible command, and set output to string format
ans_output = p.call(myCmd,shell=True)
ans_output = str(ans_output)
# Create cleaned up text file for writing:
cleanedup_txt = open('cleanedup.txt','a+')
# Clean up raw output and write to cleanedup.txt file
with open('raw.txt', 'a+') as raw_txt:
raw_txt.write(ans_output)
with open('raw.txt', 'r+') as file:
list_of_lines = file.readlines()
for line in list_of_lines:
if "msg" in line:
pass
elif "changed" in line:
pass
elif "stdout" in line:
pass
elif "table" in line:
pass
elif "master:0" in line:
pass
elif hyphen in line or colon in line:
cleanedup_txt.write(line)
# Mermaid formatting:
graph_direction = "```mermaid\n \ngraph LR \n"
'''
File from previous run, containing the filtered
output of the Ansible Playbook
'''
cleanedup_txt = open('cleanedup.txt', 'a+')
diagram_txt= open('diagram.txt', 'a+')
'''
Master list of port pairs to avoid duplicate links.
Port pair: local_port + neigh_port ("Et43Et12")
'''
with open("lldp-diagram.md","a+") as lldp_diagram:
target_dev = ""
local_port = ""
lldp_neigh = ""
target_dev = ""
neighbor = ""
noise = "TTL"
# write type of file:
lldp_diagram.write(graph_direction)
# Check line by line, reformat and write to final file: lldp-diagram.md
for line in cleanedup_txt:
master_list = []
dev_shape(target_dev)
if noise in line:
pass
elif "SUCCESS" in line:
# The split() func. will create a list of your string of text,
# splitting on empty spaces
my_list = line.split()
master_list.append(my_list[0])
target_dev = my_list[0]
elif (line.index("-")) == 11:
# Juniper
juniper_dev(line)
else:
# Arista version
arista_dev(line)
lldp_diagram.write("```\n \n")
# Close all files.
raw_txt.close()
cleanedup_txt.close()
lldp_diagram.close()
print("List links:",links)
# Command to open file with default Markdown reader
open_typora = p.Popen(["open", "lldp-diagram.md"], stdout=subprocess.PIPE)