Sunday, October 28, 2012

Hosts Online (Working on 12.10)

So out of the whopping four scripts I have written, this was still the biggest pain in the arse to fix for 12.10, although that was expected because it was the biggest pain in the arse to write for 7.04. The good news is with 12.10 it is farrrr less complicated. It still works the same as it did 5 years ago, however. The script "hostsonline" is run in the background (ideally started at startup,) and it will place a file named ".hostsonline" inside of your home directory every 30 seconds. This file will contain a list of all the hosts connected to your LAN. (To reiterate why I did it this way instead of just outputting the script directly to conky, nmap (the program the script is built off of) takes a while to "resolve." IF I loaded nmap directly into conky, it would jam it up and slow down the other modules. So this way all conky has to do is read a file that is updated every 30 seconds, and it will instantly update in conky. This also helps conky not return an incomplete list if conky updates while nmap is running.

Anyway, without further ado, here she is.

//Start of Script

#!/bin/sh
#
# Script by Jason Gotti (email removed for spambots)
# Written and tested on Ubuntu 12.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 "wlan0" to reflect your network interface. (Check in "ifconfig")
# You also need to install nmap, apcalc, and dog for this script to function correctly..

rawlist=$(nmap -sP 192.168.1.0/24 | grep 'Nmap scan report' | sed 's/[^0-9.]*//g')
while :
do
rm ~/.rawlist
rm ~/.hostsonline
echo $rawlist >> ~/.rawlist
tr ' ' '\n' < ~/.rawlist >> ~/.hostsonline
sleep 30
done


// End of Script

This should be called in conky like this:

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


And it will output like:

Hosts Online:
192.168.1.2
192.168.1.8
192.168.1.12

No comments: