Listing 2. store Mongo.py # Programmer: Mihalis Tsoukalos # Date: Tuesday 28 October 2014 # # Description: This Python script reads input from # tshark, parses it and stores it in a MongoDB database import sys import pymongo import re # The number of BSON documents written total = 0 # Open the MongoDB connection connMongo = pymongo.Connection('mongodb://localhost:27017') # Connect to database named LJ (Linux Journal) db = connMongo.LJ # Select the collection to save the network packet traffic = db.netdata # Read the file from stdin, line by line for line in sys.stdin: line = line.rstrip('\n') parsed = line.split("\t") total = total + 1 # Construct the "record to be inserted netpacket = { 'framenumber': parsed[0], 'sourceIP': parsed[1], 'destIP': parsed[2], 'framelength': parsed[3], 'IPlength': parsed[4] } # Store it! net_id = traffic.insert(netpacket) connMongo.close() # Present the total number of BSON documents written print "Total number of documents stored: ", total