092013

User Online Monitor Script Andrew Mallett | Updated September 2013

Using the ping command, this script will check if a system is on-line. You can ping either the hostname or IP Address directly. The frequency that the script runs is set by running it via cron.

The script, philping.sh..

#/bin/sh
#Andys Ping Alerter Script

/bin/ping -c1 phil > /dev/null

status=`nmap -sP phil | grep host | cut -c 26`
if [ $status = 0 ]
then
echo /dev/null
else
echo `date +%H:%M.%S` "Phil is on line.." >> /sc/phillog/philup
echo `date +%H:%M.%S` "Phil is on line.." >> /dev/pts/0
echo `date +%H:%M.%S` "Phil is on line.." >> /var/www/philup.txt
fi

The code firstly pings the hostname of the target. If the host is unknown to your system, place an IP/Hostname entry in the /etc/hosts file. The ping command is output to /dev/null as we don't require the results echoed to anywhere at this stage.

The second line declares a variable called status by invoking nmap with the -sP options to perform a ping scan (host discovery), then print out the available hosts that responded to the scan. The grep command then pulls out only the line containing the word host (lower case). Finally the cut command grabs character 26 from this line, which is either a '1' (as in '1 host up') or a '0' (host not found).

The third line tests the new $status variable using an if/else statement. If $status=0 then the script echoes bugger-all, jumps to fi and finishes.

However if $status=1 the script elses to echo a message in three places:

The first echo takes the current time and the 'Phil is on line' message and appends it to the day's record, /sc/phillog/philup. This record file is renamed to the day's date and archived daily.

The second echo does the same thing but sends it to the screen of my Linux server, /dev/pts/0

The third echo appends the same message to a file which is in the home path of my Apache Server. This means if I want to be remotely nosey, I can see Phil's online status for the day by checking the relevant server online, thus: http://www.shell-scripts.com/philup.txt

Unix-Linux Custom Alert Scripts

 

Note that as usual, full paths to every file are included as the script runs every 5 minutes from good old crontab..

*/5 * * * * /sc/scripts/philping.sh > /dev/null

Dear old Phil eh, I've got tabs on the poor bastard whenever he logs onto my network. I could use this to monitor his smartphone too but I'm not that paranoid. Did I mention I also own the DHCP Server? - mwuha-ha-ha-hah..!

Back

Comments (0)

Name
E-mail (Will not appear online)
Homepage
Title
Comment
;-) :-) :-D :-( :-o :-O B-) :oops: :-[] :-P
To prevent automated Bots form spamming, please enter the text you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
»
This comment form is powered by GentleSource Comment Script. It can be included in PHP or HTML files and allows visitors to leave comments on the website.

PHP Scripts   Disposable Email   Temporary Email