import logging
import logging.handlers
+import os
import pcapy
import struct
+import subprocess
import sys
import time
class Queue(Thread):
- heartbeat = 5.0
+ heartbeat = 1.0
maxitems = 100
def __init__(self, log):
self.join()
+class EventException(Exception):
+ pass
+
class Event(object):
def __init__(self, interface):
self.cappie = interface.cappie
self.interface = interface
+ self.log = interface.log
def __str__(self):
return self.__class__.__name__
raise NotImplementedError
+class EventTimeout(EventException):
+ pass
+
+
+class EventShell(Event):
+ heartbeat = 0.1
+ timeout = 10
+
+ def __init__(self, interface, script):
+ Event.__init__(self, interface)
+
+ self.script = script
+
+ def run(self):
+ args = " ".join([self.script, self.interface.dev])
+
+ start = time.time()
+ self.log.debug("Running: %s" % args)
+
+ p = subprocess.Popen(args,
+ close_fds=True,
+ shell=True,
+ stdin=open("/dev/null", "r"),
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT)
+
+ while p.poll() is None:
+ time.sleep(self.heartbeat)
+ if (time.time() - start) > self.timeout:
+ try:
+ os.killpg(p.pid, 9)
+ except OSError:
+ pass
+ raise EventTimeout, "Script took too long to return"
+
+ for line in p.stdout.read().splitlines():
+ if not line: continue
+ self.log.debug(" %s" % line)
+
+ self.cappie.log.debug("Child process returned with exit code: %s" % p.returncode)
+ return p.returncode
+
+
class Cappie(object):
def __init__(self):
self.__interfaces = []