-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuild_the_temp_database.py
More file actions
163 lines (137 loc) · 4.68 KB
/
build_the_temp_database.py
File metadata and controls
163 lines (137 loc) · 4.68 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
import os
from getpass import getpass
import re
from pprint import pprint
from db_work import *
import time
#This builds a set of tables from a dictionariy
#Next is a list of list of directories, "table" is the table name
#columnts is a list [0] is the feild name [1] is the feild type
#tables_columns = [{'table': "devices_subnets",
#'columns':[['device_id','TEXT'],['interface','TEXT'],['ip','TEXT'],['subnet_mask','TEXT']]},
#
#{'table': "devices_ipv4_routing",
#'columns':[['device_id','TEXT'],['OSPF_config','TEXT'],['BGP_config','TEXT'],['route_map','TEXT'],['prefix_list','TEXT'],['redist_connected','TEXT']]},
#]
#Next we call the function below
#build_db(db_name,tables_columns)
#Regular expressoin to find IP addresses
def get_ip (input):
return(re.findall(r'(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)', input))
def read_doc (file_name):
doc = []
for line in open(file_name, 'r').readlines():
doc.append(line)
return doc
def read_in_ips():
ips_doc = "IPs.txt"
ips = []
for line in read_doc (ips_doc):
temp_ips = get_ip (line)
for temp_ip in temp_ips:
ips.append(temp_ip)
return ips
#main function that ties everything together
def build_to_do_database(table_dics):
db_name = table_dics['tmp_db']["db_name"]
#Table Structure
#If you make changes here to sure and make changes in explore the network table_dics
tables_columns = [
{
'table': 'to_do_ips',
'columns':[['ip','text']],
'primary_key':'ip'},
{
'table': 'done',
'columns':[['ip','text']]},
{'table': 'issues',
'columns':[['ip','text']],
'primary_key':'ip'},
]
#Pull in IPs from the IPs document
ips = read_in_ips()
#Build the DB from the structure above
#build_db(db_name,tables_columns)
#Put the IPs into the to_do_ips table
build_table_commands = [
"DROP TABLE IF EXISTS to_do_ips",
"DROP TABLE IF EXISTS done",
"DROP TABLE IF EXISTS issues",
"CREATE TABLE to_do_ips(ip varchar(39) unique);",
"CREATE TABLE done(ip varchar(39) unique);",
"CREATE TABLE issues(ip varchar(39) unique);",
]
conn = MySQLdb.connect(host=db_location,port = 3306, user= db_user, passwd=db_password,db=temp_db)
cur = conn.cursor()
for build_table_command in build_table_commands:
cur.execute(build_table_command)
conn.commit()
cur.close
conn.close
put_ips_in_todo_db(table_dics,ips)
#Name of Database that will be created
db_name = 'temp.db'
def pull_all_table_data(conn,cur,table):
command = 'select * from {};'.format(table)
returned_data = cur.execute(command)
return returned_data.fetchall()
def pull_ip(tmp_db_dic):
conn = MySQLdb.connect(db_location,db_user, db_password,temp_db)
cur = conn.cursor()
output = []
remove_from_table = tmp_db_dic['to_do_table']
add_to_table = tmp_db_dic['done_table']
good_ip = False
while good_ip == False:
command = "SELECT ip FROM {} order by rand() limit 1;".format(remove_from_table)
cur.execute(command)
ip_to_do = cur.fetchall()
if (len(ip_to_do)) == 0:
return "Done"
if len(ip_to_do[0][0]) == 0:
print (ip_to_do, 'build temp db line 107 when this is nothing the program has reached the end of the DB and crashes, that means it is done')
ip = ip_to_do[0][0]
command = "select ip from {} where (ip = '{}');".format (add_to_table,ip)
cur.execute(command)
done_ips = cur.fetchall()
if len(done_ips) == 0:
good_ip = True
command = "DELETE FROM {} where (ip ='{}');".format(remove_from_table,ip)
cur.execute(command)
conn.commit()
time.sleep(.1)
cur.close
conn.close
insert_into_done(ip,tmp_db_dic )
return ip
def insert_ip_into_issues(db_name,ip):
conn = MySQLdb.connect(db_location,db_user, db_password,temp_db)
cur = conn.cursor()
issues_table = 'issues'
command = "INSERT INTO {} ('ip') VALUES('{}')".format(issues_table,ip)
try:
returned_data = cur.execute(command)
except Exception as e:
if "UNIQUE constraint failed" not in str(e):
if 'Duplicate entry' not in str(e):
print ('build_temp line 135',str(e))
command = 'select * from {};'.format(issues_table)
returned_data = cur.execute(command)
conn.commit()
cur.close
conn.close
def insert_into_done(ip,tmp_db_dic):
db_name = tmp_db_dic['db_name']
add_to_table = tmp_db_dic['done_table']
conn = MySQLdb.connect(db_location,db_user, db_password,temp_db)
cur = conn.cursor()
command = "INSERT INTO {} (ip) VALUES('{}');".format(add_to_table,ip)
try:
cur.execute(command)
except Exception as e:
if "UNIQUE constraint failed" not in str(e):
if 'Duplicate entry' not in str(e):
print ('build_temp line 155',str(e))
conn.commit()
cur.close
conn.close