-
Notifications
You must be signed in to change notification settings - Fork 2
/
dbWork.py
92 lines (78 loc) · 2.27 KB
/
dbWork.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
#!/usr/bin/python
import os
import sqlite3
import utils.helper as helper
import dbQueue
def db_getCursor():
conn = sqlite3.connect(dbQueue.DBFILE)
c = conn.cursor()
return c
def db_closeCursor(c):
c.close()
def db_runner(conn, query, args=None):
try:
c = conn.cursor()
if args: c.execute(query, args)
else: c.execute(query)
results = c.fetchall()
c.close()
return results
except Exception as e:
helper.printR("db_runner: " + str(e))
def db_init():
try:
os.makedirs(dbQueue.dumpDir)
os.makedirs(dbQueue.serviceDir)
os.makedirs(dbQueue.screenDir)
os.makedirs(dbQueue.dataDIR)
except OSError:
if not os.path.isdir(dbQueue.dumpDir):
raise
if not os.path.isdir(dbQueue.serviceDir):
raise
if not os.path.isdir(dbQueue.screenDir):
raise
if not os.path.isdir(dbQueue.dataDIR):
raise
try:
# set the database connectiont to autocommit w/ isolation level
conn = sqlite3.connect(dbQueue.DBFILE, check_same_thread=False)
conn.text_factory = str
conn.isolation_level = None
return conn
except Exception:
helper.printR("Could not connect to database")
helper.printR("Please run install.sh")
raise SystemExit
def db_setup(conn):
c = conn.cursor()
c.execute('DROP TABLE IF EXISTS stages')
c.execute( '''CREATE TABLE "stages" (
"stage_id" text PRIMARY KEY,
"status" text
)''')
c.execute('DROP TABLE IF EXISTS results')
c.execute( '''CREATE TABLE "results" (
"host" text,
"port" text,
"proto" text,
"serviceID" text
)''')
c.execute('DROP TABLE IF EXISTS Hosts')
c.execute( '''CREATE TABLE "Hosts" (
"host" text,
"status" text,
"ports" text
)''')
c.close()
def db_connect():
try:
# set the database connectiont to autocommit w/ isolation level
conn = sqlite3.connect(dbQueue.DBFILE, check_same_thread=False)
conn.text_factory = str
conn.isolation_level = None
return conn
except Exception:
helper.printR("Could not connect to database")
helper.printR("Please run install.sh")
raise SystemExit