You are here: Home > Publications > RIPE Labs > Shane Kerr > SQLite2pickle

SQLite2pickle

Shane Kerr — 12 May 2015
SQLite2pickle.py

Python Source icon SQLite2pickle.py — Python Source, 1 KB (1086 bytes)

File contents

import sys
import cPickle

# location of pysqlite2 install
sys.path.append('/home/shane/lib/python')

from pysqlite2 import dbapi2 as sqlite


# dictionary of name servers, each entry is a dictionary of IP addresses
ns_dict = { }

# read our 'job' list and convert to a list of name servers and IP addresses
conn = sqlite.connect('zcheck.db')
cur = conn.cursor()
cur.execute('SELECT nameserver, ip, last_result FROM job')
for row in cur:
    (ns, ip, result) = row
    ns = ns.upper()
    if not ns in ns_dict:
        ns_dict[ns] = { }
    if not (ip is None):
        ns_dict[ns][ip] = ns_dict[ns].get(ip, 0) | int(result)

# working IP addresses per server
ns_ip = { }

# get portion of IP addresses that is good for each name server
ns_list = ns_dict.keys()
for ns in ns_list:
    num_ip = 0
    num_ip_good = 0
    for ip in ns_dict[ns].keys():
        num_ip = num_ip + 1
        if ns_dict[ns][ip]:
            num_ip_good = num_ip_good + 1
    ns_ip[ns] = (num_ip, num_ip_good)

# output our resulting mapping
outfile = file('ScanPcap.pickle', 'w')
cPickle.dump(ns_ip, outfile)