Year of Python (YOP) – Week Fifty

14Dec15

Hello Reader!

This week we’re going back and revisiting my Windows DNS script from a couple of weeks ago.  What I had planned to do when I first wrote the script is add the ability to write it all to a SQLite database.  After that I’m going to make a script that will let me search for data within the database.

So the main difference between this script and the previous version is one section:

if os.path.isfile(args.output_file):
    print "{} exists...".format(args.output_file)
else:
    db_connect = sqlite3.connect(args.output_file)
    dns_db = db_connect.cursor()
    # create table
    dns_db.execute("CREATE TABLE dns_record (ID INTEGER PRIMARY KEY AUTOINCREMENT, dns_date text, dns_time text, dns_am_pm text,dns_thread text, dns_context text, dns_ipi text, dns_udp_tcp text, dns_send_recv text, dns_ip text, dns_xid text, dns_query text,dns_opcode text, dns_flagsh text, dns_flagsc text, dns_response text, dns_ques_t text, dns_ques_n text);")

with open(args.backup_file, "r") as new_file:
    for line in new_file:
        try:
            dns_record = dns_record_parse(line)
            dns_db.execute("insert into dns_record values(NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",(dns_record[0], dns_record[1], dns_record[2], dns_record[3], dns_record[4], dns_record[5], dns_record[6],dns_record[7], dns_record[8], dns_record[9], dns_record[10], dns_record[11], dns_record[12], str(dns_record[13]),dns_record[14], dns_record[15], dns_record[16]))
            db_connect.commit()
        except Exception, e:
            print "{}: {}".format(str(e), dns_record)
            continue

What we’re doing here is checking to see if the SQLite database exists, if it doesn’t we’ll set up the table to store the data.

Once that part is done, we read in the DNS log file and insert it into the database.  If we run into any issues we print out an error.

Until next week!

https://github.com/CdtDelta/YOP

Advertisements


No Responses Yet to “Year of Python (YOP) – Week Fifty”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: