-
Notifications
You must be signed in to change notification settings - Fork 5
/
find_data.py
258 lines (219 loc) · 7.08 KB
/
find_data.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
from getpass import getpass
from pprint import pprint
#import requests
import MySQLdb
import os
import datetime
import time
import calendar
import smtplib
from os.path import basename
import os
from common_functions import *
from db_work import *
file_name = "file_for_reports.txt"
hostnames = read_doc_list (file_name)
username = 'root'
password = "password"
location = db_location
database = 'network_data_db'
def to_doc_w(file_name, varable):
f=open(file_name, 'w')
f.write(varable)
f.close()
def pull_cdp_data(hostname):
output = []
pprint (hostname)
conn = MySQLdb.connect(location,username, password,database)
command = "select local_host,deviceId,platform,capabilities,localInterface,interface,version from cdp where local_host ='{}';".format(hostname)
cur = conn.cursor()
cur.execute(command)
deviceids_lists = cur.fetchall()
for devices in deviceids_lists:
output.append(devices)
cur.close
conn.close
return (output)
def pull_inventory_data(hostname):
output = []
pprint (hostname)
conn = MySQLdb.connect(location,username, password,database)
command = "select local_host,descr,name,pid,sn from inventory where local_host ='{}';".format(hostname)
cur = conn.cursor()
cur.execute(command)
deviceids_lists = cur.fetchall()
for devices in deviceids_lists:
output.append(devices)
cur.close
conn.close
return (output)
def pull_int_brief_data(hostname):
output = []
pprint (hostname)
conn = MySQLdb.connect(location,username, password,database)
command = "select local_host,intf,ipaddr from ip_int_brief where local_host ='{}';".format(hostname)
cur = conn.cursor()
cur.execute(command)
deviceids_lists = cur.fetchall()
for devices in deviceids_lists:
output.append(devices)
cur.close
conn.close
return (output)
def pull_osfp_data(hostname):
output = []
pprint (hostname)
conn = MySQLdb.connect(location,username, password,database)
command = "select local_host,address,interface,neighbor_id,state from ospf where local_host ='{}';".format(hostname)
cur = conn.cursor()
cur.execute(command)
deviceids_lists = cur.fetchall()
for devices in deviceids_lists:
output.append(devices)
cur.close
conn.close
return (output)
def pull_show_ver_data(hostname):
output = []
pprint (hostname)
conn = MySQLdb.connect(location,username, password,database)
command = "select local_host,version,uptime,running_image,config_register from show_ver where local_host ='{}';".format(hostname)
cur = conn.cursor()
cur.execute(command)
deviceids_lists = cur.fetchall()
for devices in deviceids_lists:
output.append(devices)
cur.close
conn.close
return (output)
def pull_raw_data_data(hostname):
output = []
pprint (hostname)
conn = MySQLdb.connect(location,username, password,database)
command = "select show_run from raw_data where local_host ='{}';".format(hostname)
cur = conn.cursor()
cur.execute(command)
deviceids_lists = cur.fetchall()
for devices in deviceids_lists:
output.append(devices)
cur.close
conn.close
return (output)
def pull_subnet_data(hostname):
output = []
pprint (hostname)
conn = MySQLdb.connect(location,username, password,database)
command = "select local_host,nexthop_if,network,mask from connected_subnets where local_host ='{}';".format(hostname)
cur = conn.cursor()
cur.execute(command)
deviceids_lists = cur.fetchall()
for devices in deviceids_lists:
output.append(devices)
cur.close
conn.close
return (output)
def html_build_start_body():
body = """<html>
<style>
#customers {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
#customers td, #customers th {
border: 1px solid #ddd;
padding: 8px;
}
#customers tr:nth-child(even){background-color: #f2f2f2;}
#customers tr:hover {background-color: #ddd;}
#customers th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #4CAF50;
color: white;
}
</style>
<body>
<!DOCTYPE html>
<html>
<body>
"""
return body
def html_build_table(body,headers,entires):
body = body + ''' <table> <table id="customers">
<tr>'''
for header in headers:
body = body+"<th>{}</th>\n".format(header)
body = body + "</tr>\n"
body = body + "<tr>\n"
for entry in entires:
for line in entry:
body = body + '<td>{}</td>\n'.format(line)
body = body + "</tr>\n"
body = body+'</table>'
return body
#print ('Running Config: ')
#pprint (pull_raw_data_data(hostname))
#print ('\n\n\nCDP DATA: ')
#pprint (pull_cdp_data(hostname))
#print ('\n\n\nInventory Data:')
#pprint (pull_inventory_data(hostname))
#print ("IP Int Brief Data:")
#pprint (pull_int_brief_data(hostname))
#print ("\n\n\nOSPF Data:")
#pprint (pull_osfp_data(hostname))
#print ("\n\n\nVersion Data:")
#pprint (pull_show_ver_data(hostname))
#print ("\n\n\nSubnet Data:")
#pprint (pull_subnet_data(hostname))
cdp_headers = ["select local_host",
'Remote Device',
"platform",
"capabilities",
"localInterface",
"interface",
"version",]
inv_headers = ["local_host",
"descr",
"name",
"pid",
"sn"]
ip_int_brief_headers = ["local_host",
"intf",
"ipaddr",]
ospf_headers = ["local_host","address","interface","neighbor_id","state"]
show_ver_headers = ["local_host","version","uptime","running_image","config_register"]
subnet_data_headers = ["local_host","nexthop_if","network","mask"]
for hostname in hostnames:
hostname = hostname.rstrip('\n')
cdp_data = pull_cdp_data(hostname)
inv_data = pull_inventory_data(hostname)
int_brief_data = pull_int_brief_data(hostname)
ospf_data = pull_osfp_data(hostname)
ver_data = pull_show_ver_data(hostname)
subnet_data = pull_subnet_data(hostname)
running_config = pull_raw_data_data(hostname)[0][0]
body = html_build_start_body()
body = body + "<h2>{} Data</h2>".format (hostname)
body = body + "<h2>CDP Data</h2>"
body = html_build_table(body,cdp_headers,cdp_data)
body = body + "<h2>Inventory Data</h2>"
body = html_build_table(body,inv_headers,inv_data)
body = body + "<h2>IP Interface Brief</h2>"
body = html_build_table(body,ip_int_brief_headers,int_brief_data)
body = body + "<h2>OSPF Neighborships</h2>"
body = html_build_table(body,ospf_headers,ospf_data)
body = body + "<h2>Show Version Data</h2>"
body = html_build_table(body,show_ver_headers,ver_data)
body = body + "<h2>Subnets Data</h2>"
body = html_build_table(body,subnet_data_headers,subnet_data)
body = body + "<h2>Running Config</h2>"
body = body +'<pre>'
body = body + running_config
body = body +'</pre>'
body = body + """</table></body>
</html>
"""
output_file = hostname+" report.html"
to_doc_w(output_file,body)