Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from multiprocessing import Process, Pipe
- from subprocess import Popen, PIPE, STDOUT
- import os
- import shlex
- import sys
- import logging
- import sqlite3
- import configparser
- # Logging Config settings
- logging.basicConfig(level=logging.DEBUG,
- format='[%(asctime)s] [%(levelname)-8s] [%(message)s]',
- datefmt='%a %d %b %Y] [%H:%M:%S',
- filename='tcpdump.log',
- filemode='a')
- # DEFINE CONFIG VARIABLES
- config = configparser.RawConfigParser()
- config.read('config.cfg')
- config.sections()
- # Configure Database
- # host = config.get('DataBase','host')
- # user = config.get('DataBase','user')
- # password = config.get('DataBase','password')
- # db = config.get('DataBase','db')
- class ExternalProcess(Process):
- def __init__(self, command, pipe):
- super().__init__()
- self.command = command
- self.pipe = pipe
- def run(self):
- with Popen(self.command, stdout=PIPE, stderr=STDOUT, universal_newlines=True) as process:
- for line in process.stdout:
- self.pipe.send(line)
- def tcpdump():
- # Configure network port
- networkport = ('network', 'adapter')
- logging.debug(networkport)
- # tcpdumpcommand = 'tcpdump', '-e', '-n', '-i {}', 'src', 'port 67', 'and',' net',' not',' 127.0.0.1/24'.format(networkport)
- # tcpdump -n -i eno16777736 tcp port 22 and host shinobi.noip.me
- # tcpdumpcommand = ['tcpdump','-n','-i','eno16777736','tcp','port 22','and', 'host 0300C-tiger.ecs.andrews.edu']
- # tshark = subprocess.check_output(shlex.split("""tshark -V -l -i "any" -f 'udp port 67'"""))
- # tshark = ['tshark']
- # process = subprocess.Popen(tshark,shell=True,stderr=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[1]
- tcpdump_cmd = ['sudo',
- 'tcpdump',
- '-e',
- '-n',
- '-i', 'en0',
- 'src', 'port', '67', 'and', 'net', 'not', '127.0.0.1']
- tcpdump_send_con, tcpdump_recv_con = Pipe()
- tcpdump_process = ExternalProcess(tcpdump_cmd, tcpdump_send_con)
- tcpdump_process.start()
- tshark_cmd = ['sudo',
- 'tshark',
- '-ni', 'en0',
- '-Y', 'bootp.option.type == 53',
- '-T', 'fields',
- '-e', 'frame.number',
- '-e', 'frame.time',
- '-e', 'ip.src',
- '-e', 'ip.dst',
- '-e', 'bootp.option.type',
- '-e', 'bootp.ip.client']
- tshark_send_con, tshark_recv_con = Pipe()
- tshark_process = ExternalProcess(tshark_cmd, tshark_send_con)
- tshark_process.start()
- while True:
- try:
- if tcpdump_recv_con.poll():
- print('tcpdump output:', tcpdump_recv_con.recv())
- elif tshark_recv_con.poll():
- print('tshark output:', tshark_recv_con.recv())
- except KeyboardInterrupt:
- tcpdump_process.terminate()
- tshark_process.terminate()
- sys.exit(0)
- tcpdump()
Add Comment
Please, Sign In to add comment