Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have ports open on a Mac for specific services. Is there a way to have the OS report through e-mail (Smtp) once a connection is established to one of the ports of interest?

share|improve this question
up vote 0 down vote accepted

The simplest solution - to use crontab. Create a file similar to the following:

#!/bin/sh
t=1197
while [ $t -gt 0 ]; do
ports="$(netstat -n | grep "ESTABLISHED" | awk '{print $4}' | grep -E '.*\.(80|5500|and other interested ports)$')"
if [ "$ports" ];
then t=0; echo $ports | mail -s "subject" your_mail@mail.com
fi
sleep 3
t=$((t-3))
done

Save it as 123.sh and make it executable: chmod +x /path/to/123.sh. Create in your user directory .crontab file:

*/20 * * * * /path/to/123.sh

Checking will be executed every 3 seconds, but the difference in time between the notifications will not be less than 20 minutes, so as not to make it into a spam. You can change these numbers to other, what right for you. And finally - you need to specify the crontab daemon to file: crontab ~/.crontab

share|improve this answer
    
Thanks, perhaps I will try to accomplish this with CronniX – orlando lopez May 10 '12 at 22:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .