Commit 21169c76 authored by Jason Frisvold's avatar Jason Frisvold
Browse files

- Python 2 to 3 compatibility

- NOTE : Some dependencies (MySQLdb, netaddr) may not support Python3
parent 6334743d
......@@ -20,7 +20,11 @@
# Import libraries
import sys
import getopt
import ConfigParser
# Try/Except to handle Python 3 renaming of configparser
try:
import configparser
except ImportError:
import ConfigParser as configparser
import time
import logging
from datetime import datetime
......@@ -56,8 +60,8 @@ def main(argv):
"dhvVs:c:",
["debug", "help", "verbose", "config",
"version", "license"])
except getopt.GetoptError, err:
print str(err)
except getopt.GetoptError as err:
print(str(err))
usage()
sys.exit(2)
......@@ -71,7 +75,7 @@ def main(argv):
# TODO : This should be validated
configfile = o
elif o in ("-V", "--version"):
print '{0} {1}'.format(programname, version)
print('{0} {1}'.format(programname, version))
sys.exit()
elif o in ("-h", "--help"):
usage()
......@@ -101,14 +105,14 @@ def main(argv):
user = cfg['dbuser'],
passwd = cfg['dbpass'],
db = cfg['dbname'])
except MySQLdb.Error, e:
except MySQLdb.Error as e:
logger.exception("MySQL Error [{0}]: {1}".format(e.args[0],
e.args[1]))
sys.exit()
# Get the list of servers to access
serverlist = get_servers(dbhandle)
# Loop through the list
if (len(serverlist) > 0):
for server in serverlist:
......@@ -127,11 +131,11 @@ def main(argv):
if (len(configlist) > 0):
push_configs(server_conn, configlist,
server['config_dir'])
# Get results from server
get_results(dbhandle, server_conn, server['output_dir'],
str(server['id']))
server_conn.close()
# Process results, update database, store results
......@@ -146,7 +150,7 @@ def main(argv):
def loadconfig():
logger.info('Loading server configuration')
config = ConfigParser.SafeConfigParser()
config = configparser.SafeConfigParser()
config.read(defaultconfigfile)
......@@ -167,7 +171,7 @@ def loadconfig():
cfg['work_dir'] = config.get('skynet', 'work_dir')
cfg['gpg_binary'] = config.get('skynet', 'gpg_binary')
for k, v in cfg.items():
for k, v in list(cfg.items()):
logger.debug('{0} = {1}'.format(k, v))
def get_servers(dbhandle):
......@@ -184,13 +188,13 @@ def get_servers(dbhandle):
c.disabled = 0))'''
try:
cur = dbhandle.cursor()
cur = dbhandle.cursor()
cur.execute(sql)
except MySQLdb.Error, e:
except MySQLdb.Error as e:
logger.exception("MySQL Error [{0}]: {1}".format(e.args[0], e.args[1]))
serverlist = []
rows = cur.fetchall()
if (len(rows) > 0):
for row in rows:
......@@ -219,7 +223,7 @@ def connect_server(server_ip, username, ssh_key):
transport = paramiko.Transport((server_ip))
transport.start_client()
transport.auth_publickey(username, server_key)
sftp = paramiko.SFTPClient.from_transport(transport)
except:
e = sys.exc_info()[0]
......@@ -241,9 +245,9 @@ def build_configs(dbhandle, server_id):
c.last_contacted <= s.last_modified)'''
try:
cur = dbhandle.cursor()
cur = dbhandle.cursor()
cur.execute(sql, [str(server_id)])
except MySQLdb.Error, e:
except MySQLdb.Error as e:
logger.exception("MySQL Error [{0}]: {1}".format(e.args[0], e.args[1]))
configlist = []
......@@ -294,7 +298,7 @@ def get_results(dbhandle, server_conn, output_dir, server_id):
try:
file_list = server_conn.listdir(output_dir)
except IOError, e:
except IOError as e:
logger.info('IO Error retrieving file list - [{0}] - {1}'
.format(e.args[0], e.args[1]))
file_list = []
......@@ -310,12 +314,12 @@ def get_results(dbhandle, server_conn, output_dir, server_id):
logger.info('Updating last_contacted for server {0}'.format(server_id))
sql = '''UPDATE cloud SET last_contacted = NOW() WHERE id = %s'''
try:
cur = dbhandle.cursor()
cur.execute(sql, [server_id])
dbhandle.commit()
except MySQLdb.Error, e:
except MySQLdb.Error as e:
logger.exception("MySQL Error [{0}]: {1}".format(e.args[0], e.args[1]))
def process_results(dbhandle):
......@@ -348,7 +352,7 @@ def process_results(dbhandle):
cur = dbhandle.cursor()
cur.execute(sql, [timer_id])
dbhandle.commit()
except MySQLdb.Error, e:
except MySQLdb.Error as e:
logger.exception("MySQL Error [{0}]: {1}"
.format(e.args[0], e.args[1]))
......@@ -361,7 +365,7 @@ def process_results(dbhandle):
else:
cloud_id = 0
spawn_id = 0
target_id = 0
target_id = 0
# Pull stats from results
......@@ -401,7 +405,7 @@ def process_results(dbhandle):
cur.execute(sql, [cloud_id, spawn_id, target_id, version, starttime,
endtime, runtime, status, portcount, nmapfile])
dbhandle.commit()
except MySQLdb.Error, e:
except MySQLdb.Error as e:
logger.exception("MySQL Error [{0}]: {1}"
.format(e.args[0], e.args[1]))
......@@ -422,9 +426,9 @@ def process_results(dbhandle):
# Usage
###
def usage():
print ('Usage: ' + sys.argv[0] + ' [OPTION]... ');
print '{0}'.format(programname)
print
print(('Usage: ' + sys.argv[0] + ' [OPTION]... '));
print('{0}'.format(programname))
print()
print ('Mandatory arguments to long options are mandatory for short ' \
'options too.')
print (' -c <file> configuration file')
......@@ -452,4 +456,4 @@ try:
if __name__ == "__main__":
main(sys.argv[1:])
except KeyboardInterrupt:
sys.exit()
\ No newline at end of file
sys.exit()
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