Bash Script To Check If A Device Is In A Specific Network | Cutting First Octet of IP Address


Had to configure a Print Server few week back. While the configurations was easy, its testing was way too cumbersome. Thanks to this trick that did the job 🙂

Now that the configuration was correct, my task was to configure many (62 to be precise) printers on my Print Server. A few printers were in a different network. So, the task was to first ping them & determine their location. Doing it manually would have been a pain (pinging 62 devices) 😦 So I chose to write a script to help me achieve it 😉

Below is that script.

while read line
do
ping_response=`ping -c1 $line|grep PING|cut -d"(" -f2|cut -d")" -f1 2>/dev/null`
ipAddressFirstOctet=`echo "$ping_response"|cut -d"." -f1`
re='^[0-9]+$' //Any output other than ping response. Happens when ping doesn't find that host.
if [[ $ipAddressFirstOctet =~ $re ]]
then
if [ "$ipAddressFirstOctet" -eq 10 ] //10 is first octet for our DMZ
then
echo "$line ($ping_response) is TCS Printer"
else
echo "$line ($ping_response) is CSC Printer"
fi
fi
done<~shashank/printer_list
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s