def main(argv): print("begin importing json to database") if not argv[0] or not os.path.isfile(argv[0]): print('need existing json file as the argument') exit(1) # load json pcap = json.load(open(argv[0])) # check if json is properly formatted if not pcap['PcapID'] or not pcap['Packets']: print('input is in unexpected format') exit(1) # variable for later use pcapid = pcap['PcapID'].split('/')[-1] packets = pcap['Packets'] pcaptime = convert_time(packets[0]['time']) # connect to db connection = init_db.connect_database() if not connection: print('database connection failed!') exit(1) # check tables init_db.create_pcap(connection) init_db.create_packet(connection) init_db.create_tag(connection) init_db.create_tagged(connection) # add predefined tags if the list is provided if "Keywords" in pcap: key_dict = pcap['Keywords'] add_predefined_tag(connection, key_dict) # add packets add_pcap(connection, pcapid, pcaptime) add_packet(connection, pcapid, packets) # close connection connection.close() os.remove(argv[0])
def main(): print("testing with dummy data (dhcp.pcap, pcap2.json") init_db.main() os.system("./pcap2db.sh ../pcaps/dhcp.pcap") connection = init_db.connect_database() add_tag(connection, 'bob', 'SRC') add_tag(connection, 'bob', 'DST') add_tagged(connection, 1, 'dhcp.pcap', 2) connection.close() json2db.main(['./pcap2.json']) print("finished adding dummy data") msg = "1. tables reset and packets from dhcp.pcap added to the packet table\n" \ "2. dummy tag (Bob) created on pin=2 packet's SRC (192.168.0.1)\n" \ "3. add packets from pcap2.json (which is just a slightly modified version of dhcp.pcap)\n" \ "all packets 192.168.0.1 are automatically tagged with Bob\n" \ "Tagged table should have 4 entries." print(msg)