Thursday, February 7, 2008

Hosts Online

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Okay. This one was probably the trickiest out of all of them so far. I had a lot of problems with variables not updating correctly, so I kind of went "re-define" crazy for awhile. As such, there were places where variables were defined more then once for no real reason. I think I got all of them, but who knows. I'll clean it up later when I put all the scripts in the download section. (More on that later.) (Just copy and paste the scripts from the Scripts page, ya lazy bastards!)

Anyway, this script makes use of the powerful nmap tool (available in the debian repos,) specifically nmap with the -sP option, which pings the network in an attempt to get a response from all hosts online. The output is quite lengthy, but with the help of awk and frustrating scripting, I was able to get the script to trim the output down to a simple list of all IP addresses which are online on the network.

The second problem I ran into was figuring out how to get it to update. Running the script directly from conky proved to slow it down too much, as it had to re-run nmap every four seconds, which took a good 15 seconds. So this delayed conky in general from refreshing until nmap was finished. So then I tried to have the script output the list to a seperate file, and have conky read that file. The only problem with that was sometimes conky would read the file while it was being written, which would return partial results.

The solution came in the form of having the script write the list to a temporary file, and only after the program was completely done running would the contents of the temp file (the hosts online list) be copied to a permanent file, which conky would read from. By doing it this way, conky would never read from an incomplete file. With all that said, it's time for the script. This script sould run without any modifications, as I made all the variables as dynamic as possible.
// Start of Script

#!/bin/sh
#
# Script by Jason Gotti (email removed for spambots)
# Written and tested on Ubuntu 7.10.
#
# Developed for use with conky, but also can be used
# for other information related purposes. Essentially
# this script will create a file in your home directory
# called ".hostsonline," which will update every thirty
# seconds, and will contain a list of all IP Addresses
# currently assigned on your current network.
#
# Initial variables. Change "ath0" to reflect your network interface. (Check in "ifconfig")
# You also need to install nmap, apcalc, and dog for this script to function correctly..

addressrange=$(ifconfig ath0 | grep "inet addr" | awk -F: {' print $2 '} | awk {' print $1 '} | awk -F. '{ print $1,".",$2,".",$3,".",0,"/24" }' | tr -d " ")
numberofhosts=$(nmap -sP $addressrange | grep Host | wc -l)
currenthost=1
host=$(nmap -sP $addressrange | grep Host | dog -l $currenthost | awk '{ print $2 }')
repeat=1
while [ $repeat = 1 ]
currenthost=1
host=$(nmap -sP $addressrange | grep Host | dog -l $currenthost | awk '{ print $2 }')
do
while [ $currenthost -le $numberofhosts ]
do
echo $host >> ~/.hostsonlinetmp
currenthost=$(calc $currenthost+1)
host=$(nmap -$addressrange | grep Host | dog -l $currenthost | awk '{ print $2 }')
done
sed '/^$/d' ~/.hostsonlinetmp > ~/.hostsonline
rm ~/.hostsonlinetmp
sleep 30
done


// End of Script

After this file is run, you should have a file in your home directory named ".hostsonline." From conky we can run "cat ~/.hostsonline" to display its contents. This looks like the following:

${color #ffcb48}Hosts Online:
${color #FFFFFF}${execi 5 cat ~/.hostsonline}


And it outputs like:

Hosts Online:
xxx.xxx.xxx.xxx
xxx.xxx.xxx.xxx
xxx.xxx.xxx.xxx


(Of course, the actual IP addresses would be displayed.)

To run this script, you'll need nmap, dog, and apcalc installed. All of these can be obtained from the debian repos.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFHrKhzTiCACtcRvugRAhHoAKCDWI5BKqOy0F7ld1SnJ21LW9LRYgCgkB03
W2o3AXqaAPuyrbq67HKOXjk=
=kt+3
-----END PGP SIGNATURE-----

No comments: