Check open ports every ...

Hello, i need a script (bash type maybe?..), which would check open ports on 127.0.0.1 and then compare open ports with "registered/allowed" port list and try to kill the program who uses unregistered ports. It would be great that script would be started lets say every 5 or 10 minutes.

You see i would like to have some kind of script which would allow me to control other users that they don't leave their psybnc, eggdrops and so on.

Thanks...

You could use the 'lsof' command to check what process is listening on a particular port, but it is a bit slow so you wont be able to run the script every 5 minutes (all the ports wont be scanned by then).

Provided you have netcat installed, this should run pretty quickly.

for port in {1..65536}; do
    nc -zv 127.0.0.1 $port 2>&1
done | grep open

Netstat will also tell you what programs have open listening tcp ports. On a linux system, it would go something like this:

netstat -l -t -p
1 Like