Hacking PingPlotter, Part 2

screen shot 2014 07 11 at 5.12.43 pm

PingPlotter's Web interface

Credit: Mark Gibbs

In the previous column on hacking PingPlotter I discussed how you can use the tool’s Web interface and the curl utility to add targets to be tracked, for example, to add a single host you would enter the following on the command line:

curl ‘http://192.168.1.127:81/cmd.asp?ID=NEWTARGET \
&target=192.168.1.254'

This assumes 192.168.1.127 is the host that's running PingPlotter Pro with the Web server enabled and 192.168.1.254 is the host you want to monitor. Better still, a whole range of hosts can be added using sequences; for example:

curl ‘http://192.168.1.127:81/cmd.asp ID=NEWTARGET \
&target=192.168.[1-254].[1-254]’

But, as I pondered at the end of the previous column, how could you add only the active hosts on your network? After a bit of tinkering this is the Bash script I came up with:

#!/bin/bash
# (the previous “hash bang” line isn’t necessary under OS X)
#
# Usage: pp-prog %1 %2 %3 %4 %5
#
#       %1 - first IP address octet
#       %2 - second IP address octet
#       %3 - third IP address octet
#       %4 - start value for fourth IP address octet
#       %5 - end value for fourth IP address octet
#
# Example: > pp-prog.sh 192 168 1 1 254
#
COUNT=3    # number of pings to send to each host
DOT=“.”    # separator for IP address
#
a=$1$DOT$2$DOT$3$DOT  # build a string of the top
# three IP address octets
for (( i=$4; i<=$5; i++ )) # note 1
  do
    response=$(ping -t 1 -c $COUNT $a$i | \
grep 'received' | \
awk -F',' '{ print $2 }' | \
awk '{ print $1 }’)  # notes 2, 3, 4, 5
    if [ $response -ne 0 ];
      then
        printf "Host : $a$i is up at $(date)  \r” # note 6a
        curl 'http://192.168.1.127:81/cmd.asp?ID=NEWTARGET \
&target='$a$i &>/dev/null  #note 6b
else
        printf "Host : $a$i is down at $(date)\r”  # note 7
    fi
  done
printf "%0.s " {1..62} # note 8a
printf "\rDone.\n" # note 8b

If you’re on OS X and have curl installed (see the previous column) then you can just create a file called pp-prog.sh with the above text (modified for the address of your PingPlotter host then make it executable with the command:

chmod +x pp-prog.sh

Voila! You can now configure PingPlotter for only the active hosts on your network.

Note that if you run the script again PingPlotter will add a second entry for each host the script finds which is not what you want … so, if you have a better script or even one that can run repeatedly and add newly found hosts without duplicating those already added as targets to PingPlotter or even remove hosts that go missing let me know.

Notes:

First of all, if you're on Windows and you don't have the Bash shell see Is there a way to run Bash scripts on Windows?

Next, here are a few notes on what the script is doing:

1 2 Page
Insider Shootout: Best security tools for small business
Join the discussion
Be the first to comment on this article. Our Commenting Policies