Search

Bash script to search for working machines in a network PDF Print E-mail
User Rating: / 1
PoorBest 
Programming - Bash
Tuesday, 29 June 2010 16:15

Below I give a small script that will help you to discover the machines that are plugged on to the network. It uses a small command called ping to discover the machines connected to the network. It will work only for the networks with subnet mask 255.255.255.0

 

 #!/bin/bash

> validip

> invalidip

echo "enter the first ip address"

read i

echo "enter the last ip address of the same subnet of mask 24 bit"

read j

 

k=`echo $i |awk -F . '{print$1"."$2"." $3"."}'`

m=`echo $i |awk -F . '{print$4}'`

n=`echo $j |awk -F . '{print$4}'`

while [ $m -le $n  ]

do

 

ping -c 1 $k$m > /dev/null

if [ $? -eq 0  ]

 

then

echo "$k$m" >> validip

 

else

echo "$k$m" >> invalidip

 

fi

 

m=`expr $m + 1`

 

done

 

Save this bash script to a file name checkip.sh. Then run the script from the terminal by typing 

 

 

sh  checkip.sh

 

 

Then it will prompt for the starting and ending ip addresses between which you want to discover the machines. The file validip will contain the list of pingable machines and file invalidip contains the list of ip addresses that arent used. Please remember to provide the smaller ip address as the first ip address or else you can try modifying the script so that it can accept the ip address in any order. Also the ping response should be enabled on all machines in the network.



Add this page to your favorite Social Bookmarking websites
 
Last Updated on Sunday, 04 July 2010 17:59
 

Add comment


Security code
Refresh

We have 77 guests online