Frage

I want to create bash script that will verify by ping list of IP’s

The problem is that ping to any address take few seconds ( in case no ping answer ) in spite I defined the ping as the following:

Ping –c  1 126.78.6.23

The example above perform ping only one time – but the problem is the time , waiting few seconds until ping ended ( if no answer )

In my case this is critical because I need to check more than 150 IP’s ( usually more 90% of the IP’s are not alive )

So to check 150 IP’s I need more than 500 seconds

Please advice if there is some good idea how to perform ping quickly

  • remark my script need to run on both OS ( linux and solaris )
War es hilfreich?

Lösung

The best idea is to run ping in parallel and then save the result in a file. In this case your script will run not longer than a second.

for ip in `< list`
do
   ( ping -c1 $ip || echo ip >> not-reachable ) &
done 

Update. In Solaris -c has other meaning, so for solaris you need run ping other way:

ping $ip 57 1

(Here, 57 is the size of the packet and 1 is the number of the packets to be sent).

Ping's syntax in Solaris:

/usr/sbin/ping -s [-l | -U] [-adlLnrRv] [-A addr_family]
  [-c traffic_class] [-g gateway [ -g gateway...]]
  [-F flow_label] [-I interval] [-i interface] [-P tos]
  [-p port] [-t ttl] host [data_size] [npackets]

You can make a function that aggregates the two methods:

myping()
{
   [ `uname` = Linux ] && ping -c 1 "$i" || ping "$ip" 57 1
}
for ip in `< list`
do
   ( myping $ip || echo ip >> not-reachable ) &
done 

Another option, don't use ping directly but use ICMP module from some language. You can use for example Perl + Net::Ping module from Perl:

perl -e 'use Net::Ping; $timeout=0.5; $p=Net::Ping->new("icmp", $timeout) or die bye ; print "$host is alive \n" if $p->ping($host); $p->close;'

Andere Tipps

Does Solaris ship with coreutils OOTB these days? Then you can use timeout to specify an upper limit:

timeout 0.2s ping -c 1 www.doesnot.exist >/dev/null 2>&1

You could use hping3, which is scriptable (in Tcl).

As already stated, a simple way is to overcome the timing issue run the ping commands in parallel.

You already have the syntax for Linux (iputils) ping.

With Solaris, the proper option to send a single ping would be

ping -s 126.78.6.23 64 1

Installing nmap from sources would provide a more powerful alternative though.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top