Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This script scans IP's and returns a status of Up or Down. How can I alter it so it doesn't scan everything twice? As it sits it scans everything and returns the "Up" addresses only and then scans all over again and only returns the "Down" status addresses.

#!/bin/bash

is_alive_ping()
{
  ping -c 1 $1 > /dev/null
  [ $? -eq 0 ] && echo $i,Up
  [ $? -eq 1 ] && echo $i,Down
}

for i in 10.1.150.{1..10}
do
is_alive_ping $i & disown
done

Bonus points if anyone can make it also return the DNS name of the IP if it has one.

Thanks in advance.

share|improve this question

4 Answers 4

up vote 1 down vote accepted

The script scans each IP just once. The scanning is happening in the background (&). The down IP's take longer time to respond, that's why they are reported later.

share|improve this answer
    
Duh. thanks. Need my coffee ;) –  LVLAaron Jan 3 '12 at 16:16
    
where is background stuff here in shell? I guess am puzzled. Its different ping is unable to get the answer from the offline nodes and is reporting them at the end after minimal default retries. –  Nikhil Mulley Jan 3 '12 at 16:21
    
@Nikhil: the function is called with & (as I also mentioned): background subshell. –  choroba Jan 3 '12 at 16:27

You're looking for nmap.

# nmap -sP 10.1.150.1-10
Starting Nmap 5.00 ( http://nmap.org ) at 2012-01-04 00:02 CET
Host foo.example.com (10.1.150.2) is up (0.00066s latency).
Host bar.example.com (10.1.150.7) is up (0.00066s latency).
Nmap done: 10 IP addresses (1 host up) scanned in 1.41 seconds
share|improve this answer

ping retries for unanswering nodes, which adds to the latency in reporting the availability of the nodes back to the console output, while the answered online nodes are reported immediately back. ping has this disadvantage that you need to execute it for every host.

Use fping, a better alternative to ping , which can do a parallel ping on the hosts, source is available at sourceforge http://fping.sourceforge.net/ . Download fping and install it under /usr/local/sbin and make it suid.

Here is a perl example, that can report the unreachable nodes at the end, while the nodes that are online are reported with their dns names, you can pass the output of this script to tools such as grep or awk again to work on the desired output.

#!/usr/bin/perl
require 'open2.pl';
use Net::IP;

$pid = &open2("OUTPUT","INPUT","/usr/local/sbin/fping -d");

@check=();

my $ip = new Net::IP ('98.137.149.56 - 98.137.149.100') || die "Unable to generate range\n";

do {
    push (@check, $ip->ip());
} while (++$ip);


foreach(@check) { print INPUT "$_\n"; }

close(INPUT);

while(<OUTPUT>) {
  print "$_";
}

close(OUTPUT);
share|improve this answer

If you want to keep the order you have to redirect the output not to stdout, but to files. After all pings are done, do a wait to wait for the last background process to finish. Then cat the result files in the "right" order.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.