Skip to content

Commit

Permalink
Update cron scripts to use python3
Browse files Browse the repository at this point in the history
  • Loading branch information
TimEvens committed Mar 30, 2021
1 parent 4dded7f commit 121f237
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 78 deletions.
15 changes: 7 additions & 8 deletions cron_scripts/gen-whois/dbHandler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python
#!/usr/bin/env python3
"""
Copyright (c) 2018 Cisco Systems, Inc. and others. All rights reserved.
Copyright (c) 2018 Tim Evens (tim@evensweb.com). All rights reserved.
Copyright (c) 2021 Cisco Systems, Inc. and Tim Evens. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v1.0 which accompanies this distribution,
Expand Down Expand Up @@ -41,7 +40,7 @@ def connectDb(self, user, pw, host, database):

self.cursor = self.conn.cursor()

except (py.ProgrammingError), err:
except (py.ProgrammingError) as err:
print("ERROR: Connect failed: " + str(err))
raise err

Expand All @@ -65,7 +64,7 @@ def createTable(self, tableName, tableSchema, dropIfExists = True):
:return: True if the table successfully was created, false otherwise
"""
if (not self.cursor):
print "ERROR: Looks like psql is not connected, try to reconnect."
print("ERROR: Looks like psql is not connected, try to reconnect.")_
return False

try:
Expand All @@ -91,7 +90,7 @@ def createTable(self, tableName, tableSchema, dropIfExists = True):
:return: True if the table successfully was created, false otherwise
"""
if (not self.cursor):
print "ERROR: Looks like psql is not connected, try to reconnect."
print("ERROR: Looks like psql is not connected, try to reconnect.")
return False

try:
Expand All @@ -117,7 +116,7 @@ def query(self, query, queryParams=None):
:return: Returns "None" if error, otherwise array list of rows
"""
if (not self.cursor):
print "ERROR: Looks like psql is not connected, try to reconnect"
print("ERROR: Looks like psql is not connected, try to reconnect")
return None

try:
Expand Down Expand Up @@ -155,7 +154,7 @@ def queryNoResults(self, query, queryParams=None):
:return: Returns True if successful, false if not.
"""
if (not self.cursor):
print "ERROR: Looks like psql is not connected, try to reconnect"
print("ERROR: Looks like psql is not connected, try to reconnect")
return None

try:
Expand Down
38 changes: 16 additions & 22 deletions cron_scripts/gen-whois/gen_whois_asn.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#!/usr/bin/env python
#!/usr/bin/env python3
"""
Copyright (c) 2018 Cisco Systems, Inc. and others. All rights reserved.
Copyright (c) 2018 Tim Evens (tim@evensweb.com). All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v1.0 which accompanies this distribution,
and is available at http://www.eclipse.org/legal/epl-v10.html
Copyright (c) 2021 Cisco Systems, Inc. and Tim Evens. All rights reserved.
.. moduleauthor:: Tim Evens <tim@evensweb.com>
"""
Expand All @@ -16,7 +11,6 @@
from collections import OrderedDict
from time import sleep
import subprocess
import traceback
import dns.resolver

TBL_GEN_WHOIS_ASN_NAME = "info_asn"
Expand Down Expand Up @@ -94,9 +88,9 @@ def getASNList(db):
"""
# Run query and store data
rows = db.query(QUERY_AS_LIST)
print "Query for ASN List took %r seconds" % (db.last_query_time)
print("Query for ASN List took %r seconds" % (db.last_query_time))

print "total rows = %d" % len(rows)
print("total rows = %d" % len(rows))

asnList = []

Expand Down Expand Up @@ -389,7 +383,7 @@ def parseCmdArgs(argv):

# The last arg should be the command
if (len(args) <= 0):
print "ERROR: Missing the database host/IP"
print("ERROR: Missing the database host/IP")
usage(argv[0])
sys.exit(1)

Expand All @@ -400,13 +394,13 @@ def parseCmdArgs(argv):

# The last arg should be the command
if (found_req_args < REQUIRED_ARGS):
print "ERROR: Missing required args, found %d required %d" % (found_req_args, REQUIRED_ARGS)
print("ERROR: Missing required args, found %d required %d" % (found_req_args, REQUIRED_ARGS))
usage(argv[0])
sys.exit(1)

return cmd_args

except (getopt.GetoptError, TypeError), err:
except (getopt.GetoptError, TypeError) as err:
print str(err) # will print something like "option -a not recognized"
usage(argv[0])
sys.exit(2)
Expand All @@ -417,15 +411,15 @@ def usage(prog):
:param prog: Program name
"""
print ""
print "Usage: %s [OPTIONS] <database host/ip address>" % prog
print ""
print " -u, --user".ljust(30) + "Database username"
print " -p, --password".ljust(30) + "Database password"
print ""

print "OPTIONAL OPTIONS:"
print " -h, --help".ljust(30) + "Print this help menu"
print ("")
print ("Usage: %s [OPTIONS] <database host/ip address>" % prog)
print ("")
print (" -u, --user".ljust(30) + "Database username")
print (" -p, --password".ljust(30) + "Database password")
print ("")

print ("OPTIONAL OPTIONS:")
print (" -h, --help".ljust(30) + "Print this help menu")



Expand Down
47 changes: 21 additions & 26 deletions cron_scripts/gen-whois/gen_whois_route.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#!/usr/bin/env python
#!/usr/bin/env python3
"""
Copyright (c) 2018 Cisco Systems, Inc. and others. All rights reserved.
Copyright (c) 2018 Tim Evens (tim@evensweb.com). All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v1.0 which accompanies this distribution,
and is available at http://www.eclipse.org/legal/epl-v10.html
Copyright (c) 2021 Cisco Systems, Inc. and Tim Evens. All rights reserved.
.. moduleauthor:: Tim Evens <tim@evensweb.com>
"""
Expand Down Expand Up @@ -82,7 +77,7 @@ def import_rr_db_file(db, source, db_filename):
record = {'source': source}
inf = None

print "Parsing %s" % db_filename
print("Parsing %s" % db_filename)
if (db_filename.endswith(".gz")):
inf = gzip.open(db_filename, 'rb')
else:
Expand Down Expand Up @@ -116,7 +111,7 @@ def import_rr_db_file(db, source, db_filename):
record[WHOIS_ATTR_MAP[prev_attr]] += "\n" + value

except:
print "problem with continuation: (%s) '%s'\n\t\t%r" % (prev_attr, line, record)
print("problem with continuation: (%s) '%s'\n\t\t%r" % (prev_attr, line, record))
pass

elif ': ' in line:
Expand Down Expand Up @@ -226,16 +221,16 @@ def download_data_file():

for source in RR_DB_FTP:
try:
print "Downloading %s..." % source
print ("Downloading %s..." % source)
ftp = FTP(RR_DB_FTP[source]['site'])
ftp.login()
ftp.cwd(RR_DB_FTP[source]['path'])
ftp.retrbinary("RETR %s" % RR_DB_FTP[source]['filename'],
open("%s/%s" % (TMP_DIR, RR_DB_FTP[source]['filename']), 'wb').write)
ftp.quit()
print " Done downloading %s" % source
print (" Done downloading %s" % source)
except:
print "Error processing %s, skipping" % source
print ("Error processing %s, skipping" % source)
traceback.print_exc()


Expand Down Expand Up @@ -296,7 +291,7 @@ def parseCmdArgs(argv):

# The last arg should be the command
if (len(args) <= 0):
print "ERROR: Missing the database host/IP"
print ("ERROR: Missing the database host/IP")
usage(argv[0])
sys.exit(1)

Expand All @@ -306,14 +301,14 @@ def parseCmdArgs(argv):

# The last arg should be the command
if (found_req_args < REQUIRED_ARGS):
print "ERROR: Missing required args, found %d required %d" % (found_req_args, REQUIRED_ARGS)
print ("ERROR: Missing required args, found %d required %d" % (found_req_args, REQUIRED_ARGS))
usage(argv[0])
sys.exit(1)

return cmd_args

except (getopt.GetoptError, TypeError), err:
print str(err) # will print something like "option -a not recognized"
except (getopt.GetoptError, TypeError) as err:
print (str(err)) # will print something like "option -a not recognized"
usage(argv[0])
sys.exit(2)

Expand All @@ -323,16 +318,16 @@ def usage(prog):
:param prog: Program name
"""
print ""
print "Usage: %s [OPTIONS] <database host/ip address>" % prog
print ""
print " -u, --user".ljust(30) + "Database username"
print " -p, --password".ljust(30) + "Database password"
print ""

print "OPTIONAL OPTIONS:"
print " -h, --help".ljust(30) + "Print this help menu"
print " -d, --dbName".ljust(30) + "Database name, default is 'openbmp'"
print ("")
print ("Usage: %s [OPTIONS] <database host/ip address>" % prog)
print ("")
print (" -u, --user".ljust(30) + "Database username")
print (" -p, --password".ljust(30) + "Database password")
print ("")

print ("OPTIONAL OPTIONS:")
print (" -h, --help".ljust(30) + "Print this help menu")
print (" -d, --dbName".ljust(30) + "Database name, default is 'openbmp'")


def main():
Expand Down
40 changes: 18 additions & 22 deletions cron_scripts/rpki/rpki_validator.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#!/usr/bin/env python
#!/usr/bin/env python3
"""
Copyright (c) 2018 Tim Evens. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v1.0 which accompanies this distribution,
and is available at http://www.eclipse.org/legal/epl-v10.html
Copyright (c) 2021 Cisco Systems, Inc. and Tim Evens. All rights reserved.
.. moduleauthor:: Tim Evens <tim@evensweb.com>
"""
Expand Down Expand Up @@ -123,7 +119,7 @@ def parseCmdArgs(argv):

# The last arg should be the command
if (len(args) <= 0):
print "ERROR: Missing the database host/IP"
print ("ERROR: Missing the database host/IP")
usage(argv[0])
sys.exit(1)

Expand All @@ -134,14 +130,14 @@ def parseCmdArgs(argv):

# The last arg should be the command
if (found_req_args < REQUIRED_ARGS):
print "ERROR: Missing required args, found %d required %d" % (found_req_args, REQUIRED_ARGS)
print ("ERROR: Missing required args, found %d required %d" % (found_req_args, REQUIRED_ARGS))
usage(argv[0])
sys.exit(1)

return cmd_args

except (getopt.GetoptError, TypeError) as err:
print str(err) # will print something like "option -a not recognized"
print (str(err)) # will print something like "option -a not recognized"
usage(argv[0])
sys.exit(2)

Expand All @@ -151,20 +147,20 @@ def usage(prog):
:param prog: Program name
"""
print ""
print "Usage: %s [OPTIONS] <database host/ip address>" % prog
print ""
print " -u, --user".ljust(30) + "Database username"
print " -p, --password".ljust(30) + "Database password"
print " -s, --server".ljust(30) + "RPKI Validator server address"
print ""
print ("")
print ("Usage: %s [OPTIONS] <database host/ip address>" % prog)
print ("")
print (" -u, --user".ljust(30) + "Database username")
print (" -p, --password".ljust(30) + "Database password")
print (" -s, --server".ljust(30) + "RPKI Validator server address")
print ("")

print "OPTIONAL OPTIONS:"
print " -h, --help".ljust(30) + "Print this help menu"
print " -d, --dbName".ljust(30) + "Database name, default is 'openbmp'"
print ("OPTIONAL OPTIONS:")
print (" -h, --help".ljust(30) + "Print this help menu")
print (" -d, --dbName".ljust(30) + "Database name, default is 'openbmp'")

print "NOTES:"
print " RPKI validator http://server/export.json is used to populate the DB"
print ("NOTES:")
print (" RPKI validator http://server/export.json is used to populate the DB")


def main():
Expand All @@ -176,7 +172,7 @@ def main():

server = cfg['server']
load_export(db, server);
print "Loaded rpki roas"
print ("Loaded rpki roas")

# Purge old entries that didn't get updated
db.queryNoResults("DELETE FROM rpki_validator WHERE timestamp < now() - interval '1 hour'")
Expand Down

0 comments on commit 121f237

Please sign in to comment.