Link to home
Start Free TrialLog in
Avatar of kecoak
kecoak

asked on

Shell Script for IP address

Program like ping/traceroute/nslookup in Linux/Unix only accept single ip address. So for instance If I am want to know whether 10 hosts are up or not, i need to type ping <ip address> 10 times, one by one. The other option is to use other program or software that can accept multiple ip address.

I am thinking to create a shell script that can take multiple ip address and translate it into the right format.
Say for instance
150.101.1.1-50 should be translated into 150.101.1.1 to 150.101.1.50
Could you please give me a guidance or sample script so that I could modify and incorporate the shell script ?
Avatar of ezaton
ezaton

First - you can ask ping to ping to broadcast address, like ping -p 192.168.0.0
nmap could do a similar trick.
Second - the sript you want:
#!/bin/sh
COUNT=2 (how many pings?)
BASE_IP=150.101.1
START=1
STOP=50
i=$START
while [ "$i" -lt "$STOP" ]; do
echo "IP is $BASE_IP.$i"
ping -c $COUNT $BASE_IP.$i
let i=$i+1
done
A fix - change in the while from -lt to -le (to make it up to and equal 50)
Avatar of kecoak

ASKER

Hi ezaton,

Thanks but what I really wanted is something general not specific IP.
So I am looking for a solution so that whenever I enter the ip address, the program its self could understand what IP address they should look into.
For instance program 192.168.1.2-5 means 192.168.1.2 ... 192.168.1.5, 192-200.30.30.30 means ...
ASKER CERTIFIED SOLUTION
Avatar of vikaskhoria
vikaskhoria
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Arty K
kecoak, hi.
Why not to use 'nmap' instead of 'ping'?
It understands IP ranges and it can perform ICMP pings as well, like "nmap -PE --max-retries 3 -oG - 192.168.1-2.1-100'.

>  let i=$i+1
is no sh syntax (probaly ksh or bash:), you need to write
  i=`expr $i '+' 1`

if you use bash already, the script could be simplified to something like

seq -f "ping -c 1 150.1.1.%g" 50 |sh
let works in sh as well. At least under Linux.
Correction - ahoffmann - you are correct. My bad.
don't worry, it's the stupid bash which often mimicks to be a sh, which is wrong :-/
even it can be tweaked to operate in sh compatibility mode