?
Solved

Shell Script for IP address

Posted on 2007-08-02
9
Medium Priority
?
3,354 Views
Last Modified: 2013-12-20
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 ?
0
Comment
Question by:kecoak
9 Comments
 
LVL 7

Expert Comment

by:ezaton
ID: 19623064
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
0
 
LVL 7

Expert Comment

by:ezaton
ID: 19623065
A fix - change in the while from -lt to -le (to make it up to and equal 50)
0
 

Author Comment

by:kecoak
ID: 19623282
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 ...
0
When ransomware hits your clients, what do you do?

MSPs: Endpoint security isn’t enough to prevent ransomware.
As the impact and severity of crypto ransomware attacks has grown, Webroot fought back, not just by building a next-gen endpoint solution capable of preventing ransomware attacks but also by being a thought leader.

 
LVL 5

Accepted Solution

by:
vikaskhoria earned 2000 total points
ID: 19623735
Hi ,
I have used the above script given by ezaton and have made some changes to make it generic as follows:

#!/bin/sh
COUNT=2
inputIp=$1
START_IP=`echo $inputIp | cut -d '-' -f 1`
BASE_IP=`echo $START_IP | cut -d '.' -f 1-3`
START=`echo $START_IP | cut -d '.' -f 4`
STOP=`echo $inputIp | cut -d '-' -f 2`
i=$START
while [ "$i" -le "$STOP" ]; do
echo "IP is $BASE_IP.$i"
ping -c $COUNT $BASE_IP.$i
let i=$i+1
done

You can run this as follows:
program 1.2.3.4-10
Passing the Ip range as parameter.
Hope this will solve your problem.

Vikas
0
 
LVL 27

Expert Comment

by:Nopius
ID: 19633273
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'.

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 19633483
>  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
0
 
LVL 7

Expert Comment

by:ezaton
ID: 19634776
let works in sh as well. At least under Linux.
0
 
LVL 7

Expert Comment

by:ezaton
ID: 19634786
Correction - ahoffmann - you are correct. My bad.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 19637172
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
0

Featured Post

Threat Trends for MSPs to Watch

See the findings.
Despite its humble beginnings, phishing has come a long way since those first crudely constructed emails. Today, phishing sites can appear and disappear in the length of a coffee break, and it takes more than a little know-how to keep your clients secure.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Make the most of your online learning experience.
In this article, the configuration steps in Zabbix to monitor devices via SNMP will be discussed with some real examples on Cisco Router/Switch, Catalyst Switch, NAS Synology device.
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question