views:

93

answers:

1

On linux (ubuntu), is it possible to execute a script upon any incoming network connection?

During long periods inactivity on my home server, I plan on stopping the software raid, parking the data disks and restarting the raid array upon encountering an incoming network connection.

I've been researching this problem and haven't come across an appropriate solution yet, any help is appreciated.

Neil

Note: I'm aware it's unnessecary to stop/start the array and there are downsides (disk wear & tear...). However I'm set in my ways, please do not post advising me not to stop/start the disks.

Edit (1):

Thought I'd share my opinion and hopefully get some feedback on it.

I think I'd need to use xinetd binding a catcher script that catches incoming connection data on all ports, launches my launch script and passes the incoming connection data to the relevant service for that port. Only issue is I don't know what data I would need to catch or how to in python.

Edit (2): I've hacked together a solution using the python library pypcap http://code.google.com/p/pypcap/ . For future reference, here is the code.

import pcap
...

pc = pcap.pcap(name)
    pc.setfilter(' '.join(args))
 pc.setnonblock()

 try:
        for ts, pkt in pc:
   # subprocess.call([filename, args])
   print 'readying disks...'
   break
 except OSError, ValueError:
  print 'Error: invalid arguements passed to subprocess'
    except KeyboardInterrupt:
        print 'Error: keyboard interupt'
+1  A: 

Take a look at netfilter/iptables. You may be able to write code to use the userspace API libraries to detect incoming packet events.

Jim Garrison
thanks for the suggestion. I had a look at the libraries but decided to use make use of libpcap or more specifically pycap http://code.google.com/p/pypcap/