ipc_ngrep.py

"""Sniff the network with ngrep.
   This example needs ngrep, a small nifty command line 
   network sniffer. I would assume ngrep is unix only.
   Also, ngrep needs root permissions therefore
   this example needs to be run with root permissions.
   by stephan)
"""
from slut import *
from twisted.internet.protocol import ProcessProtocol


class Atlantis(World):
    def onSetup(self):
        self.name = "Sniffing with ngrep."
        TagText(self, 'texttag', 12, 'courier').setText('waiting for data ...')

        reactor.spawnProcess(Ngrep(10), "ngrep",
            ["ngrep", "-q", "-d", "any", "title|TITLE", "port", "80"], {})
        #on some system you might have to specify the full path to ngrep
        #the following was reported to work on macs
        #reactor.spawnProcess(pp, "/opt/local/bin/ngrep", 
        #       ["ngrep","-d", "en1", "port", "80"], {})


class Ngrep(ProcessProtocol):
    def __init__(self, verses):
        self.verses = verses
        self.data = ""
    def connectionMade(self):
        print "connectionMade!"
    def outReceived(self, data):
        print "outReceived!", data
        if data[0] != '#':
            glo.world.tags['texttag'].setText(data[:20] + '[...]')
    def errReceived(self, data):
        print "errReceived! with %d bytes!" % len(data)
    def inConnectionLost(self):
        print "inConnectionLost! stdin is closed! (we probably did it)"
    def outConnectionLost(self):
        print "outConnectionLost! The child closed their stdout!"
    def errConnectionLost(self):
        print "errConnectionLost! The child closed their stderr."
    def processEnded(self, status_object):
        print "processEnded, status %d" % status_object.value.exitCode
        print "quitting"



atlantis = Atlantis()
atlantis.run()
Initiated by Stephan Hechenberger
Thanks to CADRE's 103