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
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 ]]
if [ "$ipAddressFirstOctet" -eq 10 ] //10 is first octet for our DMZ
echo "$line ($ping_response) is TCS Printer"
echo "$line ($ping_response) is CSC Printer"

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s