Commit 0d406490 authored by Jason Frisvold's avatar Jason Frisvold
Browse files

- Change to Formatter style strings

- Separate SQL into a variable before execution
parent 718ec0c2
......@@ -88,7 +88,7 @@ def main(argv):
while True:
loopstarttime = datetime.now()
logger.info('Loop starts at ' + str(loopstarttime))
logger.info('Loop starts at {0}'.format(str(loopstarttime)))
timingdb = open_timing_database()
......@@ -120,7 +120,7 @@ def loadconfig():
config.read(configfile)
except:
e = sys.exc_info()[0]
logger.exception('Unable to load config file = ' + e)
logger.exception('Unable to load config file : {0}'.format(e))
global cfg
......@@ -159,7 +159,7 @@ def process_config(timingdb, filelist):
logger.info('Process Config')
for file in filelist:
logger.debug('Processing file - ' + file)
logger.debug('Processing file - {0}'.format(file))
incoming = open(os.path.join(cfg['configdir'], file), 'r')
lines = incoming.readlines()
incoming.close()
......@@ -172,7 +172,7 @@ def process_config(timingdb, filelist):
except:
e = sys.exc_info()[0]
logger.exception("Error creating timing object : %s", e)
logger.exception('Error creating timing object : {0}'.format(e))
def check_timing(loopstarttime, timingdb):
logger.info('Check Timing')
......@@ -182,12 +182,13 @@ def check_timing(loopstarttime, timingdb):
try:
timingcursor = timingdb.cursor()
timingcursor.execute('''SELECT t.server_id, td.override_flag FROM timers
AS t, timer_details AS td WHERE (t.minute = ? OR
t.minute = "*") AND (t.hour = ? OR t.hour = "*")
AND (t.day = ? OR t.day = "*") AND (t.month = ? OR
t.month = "*") AND t.server_id = td.server_id''',
(loopstarttime.minute, loopstarttime.hour,
sql = '''SELECT t.server_id, td.override_flag FROM timers AS t,
timer_details AS td WHERE (t.minute = ? OR t.minute = "*") AND
(t.hour = ? OR t.hour = "*") AND (t.day = ? OR t.day = "*")
AND (t.month = ? OR t.month = "*") AND t.server_id =
td.server_id'''
timingcursor.execute(sql, (loopstarttime.minute, loopstarttime.hour,
loopstarttime.day, loopstarttime.month))
rows = timingcursor.fetchall()
......@@ -198,7 +199,7 @@ def check_timing(loopstarttime, timingdb):
except:
e = sys.exc_info()[0]
logger.exception("Error checking timing : %s", e)
logger.exception('Error checking timing : {0}'.format(e))
return spawnlist
......@@ -208,44 +209,48 @@ def check_complete(spawnlist, timingdb):
try:
timingcursor = timingdb.cursor()
timingcursor.execute('SELECT server_id, start_time, pid FROM spawned')
sql = 'SELECT server_id, start_time, pid FROM spawned'
timingcursor.execute(sql)
rows = timingcursor.fetchall()
except:
e = sys.exc_info()[0]
logger.exception("Error getting spawned process data : %s", e)
logger.exception('Error getting spawned process data : {0}'.format(e))
if (len(rows) > 0):
for row in rows:
logger.debug('Checking id ' + str(row[0]) + ' started at ' +
str(row[1]))
logger.debug('Checking id {0} started at {1}'.format(str(row[0]),
str(row[1])))
if pid_exists(row[2]):
logger.debug('PID ' + str(row[2]) + ' Still running')
logger.debug('PID {0} Still running'.format(str(row[2])))
try:
# If server_id should be spawned, isn't allowed to override,
# and is still running, then remove it from the spawnlist
# and set overrun = 1
spawnlist.remove((row[0],0))
logger.debug('ID ' + str(row[0]) + ' not allowed to '
'override, removed from spawn list')
logger.debug('''ID {0} not allowed to override, removed from
spawn list'''.format(str(row[0])))
timingcursor.execute('''UPDATE spawned SET overtime = 1
WHERE server_id = ? AND pid = ?''',
(row[0], row[2],))
sql = '''UPDATE spawned SET overtime = 1 WHERE server_id =
? AND pid = ?'''
timingcursor.execute(sql, (row[0], row[2],))
except:
pass
else:
logger.debug('PID ' + str(row[2]) + ' Completed')
logger.debug('PID {0} Completed'.format(str(row[2])))
end_time = int(time.mktime(time.gmtime()))
timingcursor.execute('''DELETE FROM spawned WHERE server_id = ?
AND pid = ?''', (row[0], row[2]))
timingcursor.execute('''INSERT INTO spawn_log (server_id,
start_time, end_time) VALUES (?, ?, ?)''',
(row[0], row[1], end_time))
sql = '''DELETE FROM spawned WHERE server_id = ? AND pid = ?'''
timingcursor.execute(sql, (row[0], row[2]))
sql = '''INSERT INTO spawn_log (server_id, start_time, end_time)
VALUES (?, ?, ?)'''
timingcursor.execute(sql, (row[0], row[1], end_time))
timingdb.commit()
......@@ -256,7 +261,7 @@ def spawn_process(spawnlist):
for (timer, override) in spawnlist:
start_time = int(time.mktime(time.gmtime()))
logger.debug('Spawning process for timer - ' + str(timer))
logger.debug('Spawning process for timer : {0}'.format(str(timer)))
# First fork, if pid is zero, then it's the child
try:
......@@ -265,8 +270,8 @@ def spawn_process(spawnlist):
# The parent process should jump to the next loop iteration
continue
except OSError, e:
sys.stderr.write("fork #1 failed: (%d) %s\n" %
(e.errno, e.strerror))
sys.stderr.write('fork #1 failed: ({0}) {1}'.format(e.errno,
e.strerror))
sys.exit(1)
# Set the parameters for the child
......@@ -281,8 +286,8 @@ def spawn_process(spawnlist):
if pid > 0:
sys.exit(0)
except OSError, e:
sys.stderr.write("fork #2 failed: (%d) %s\n" %
(e.errno, e.strerror))
sys.stderr.write('fork #2 failed: ({0}) {1}'.format(e.errno,
e.strerror))
sys.exit(1)
# Set the streams for the process
......@@ -308,12 +313,13 @@ def spawn_process(spawnlist):
except:
e = sys.exc_info()[0]
logger.exception("Error creating cursor : %s", e)
logger.exception('Error creating cursor : {0}'.format(e))
logger.debug('Timer number ' + str(timer))
logger.debug('Timer number {0}'.format(str(timer)))
timingcursor.execute('''SELECT ip_range, nmap_options FROM timer_details
WHERE server_id = ?''', (timer,))
sql = '''SELECT ip_range, nmap_options FROM timer_details WHERE
server_id = ?'''
timingcursor.execute(sql, (timer,))
nmap_options = timingcursor.fetchone()
......@@ -322,20 +328,19 @@ def spawn_process(spawnlist):
optionlist = shlex.split(cfg['nmap_binary'] + ' -oX ' + tmpfile + ' ' +
nmap_options[1] + ' ' + nmap_options[0])
logger.debug('Will spawn with options ' + str(optionlist))
logger.debug('Will spawn with options {0}'.format(str(optionlist)))
spawned = Popen(optionlist)
logger.debug('Spawned - ' + str(spawned.pid))
logger.debug('Spawned - {0}'.format(str(spawned.pid)))
timingcursor.execute('''INSERT INTO spawned (server_id, start_time, pid,
overtime) VALUES (?, ?, ?, 0)''', (timer,
start_time,
spawned.pid))
sql = '''INSERT INTO spawned (server_id, start_time, pid, overtime)
VALUES (?, ?, ?, 0)'''
timingcursor.execute(sql, (timer, start_time, spawned.pid))
timingdb.commit()
spawned.wait()
logger.debug('Timer ' + str(timer) + ' completed.')
logger.debug('Timer {0} completed.'.format(str(timer)))
nmap_filename = cfg['output_dir'] + 'nmap_xml.' + str(timer) + '.' + \
str(start_time) + '.xml.gpg'
......@@ -343,12 +348,11 @@ def spawn_process(spawnlist):
cmd = (cfg['gpg_binary'], '--batch', '--yes', '--output', nmap_filename,
'--encrypt', '--recipient', cfg['gpg_key'], tmpfile)
logger.debug('Encrypting ' + tmpfile + ' via GPG (' +
str(cmd) + ')')
logger.debug('Encrypting {0} via GPG ({1})'.format(tmpfile, str(cmd)))
Popen(cmd).wait()
logger.debug('Deleting temporary file - ' + tmpfile)
logger.debug('Deleting temporary file - {0}'.format(tmpfile))
os.remove(tmpfile)
......@@ -364,38 +368,43 @@ def open_timing_database():
timingcursor = timingdb.cursor()
# Create the tables, if they don't exist already
timingcursor.execute('''CREATE TABLE IF NOT EXISTS timers
sql = '''CREATE TABLE IF NOT EXISTS timers
( server_id INT,
minute TEXT,
hour TEXT,
day TEXT,
month TEXT
)''')
)'''
timingcursor.execute(sql)
timingcursor.execute('''CREATE TABLE IF NOT EXISTS timer_details
sql = '''CREATE TABLE IF NOT EXISTS timer_details
( server_id INT,
override_flag BOOLEAN,
ip_range TEXT,
nmap_options TEXT
)''')
)'''
timingcursor.execute(sql)
timingcursor.execute('''CREATE TABLE IF NOT EXISTS spawned
sql = '''CREATE TABLE IF NOT EXISTS spawned
( server_id INT,
start_time INT,
pid INT,
overtime BOOLEAN
)''')
)'''
timingcursor.execute(sql)
timingcursor.execute('''CREATE TABLE IF NOT EXISTS spawn_log
sql = '''CREATE TABLE IF NOT EXISTS spawn_log
(server_id INT,
start_time INT,
end_time INT,
status INT
)''')
)'''
timingcursor.execute(sql)
except:
e = sys.exc_info()[0]
logger.exception("Error accessing/creating sqlite database : %s", e)
logger.exception('Error accessing/creating sqlite database : {0}'
.format(e))
sys.exit()
return(timingdb)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment