1757081 Members
2837 Online
108858 Solutions
New Discussion юеВ

For loop

 
Zan_1
New Member

For loop

Hello!

Can anyone help me with a case how to ping x.x.x.0 to x.x.x.255 and put the ones which are alive to a text file ??

Thanks in advance...
7 REPLIES 7
Dennis Handly
Acclaimed Contributor

Re: For loop

You need a while loop:
typeset -i index=0
while [ $index -le 255 ]; do
ping xxx.$index
if [ $? -eq 0 ]; then
echo xxx.$index >> alive.file
fi
(( index=index + 1 ))
done
Zan_1
New Member

Re: For loop

hmm.. doesn't seem to recognize typeset command...

bash-3.00# ./ping.sh
./ping.sh: typeset: not found
./ping.sh: test: argument expected

Dennis Handly
Acclaimed Contributor

Re: For loop

>doesn't seem to recognize typeset command

A real shell recognizes typeset. I thought bash was ksh/sh like? Perhaps you should use the real shell in your scripts.

You can replace typeset by just:
index=0
James R. Ferguson
Acclaimed Contributor

Re: For loop

Hi:

Bash uses 'declare' instead of 'typeset'.

You probably also want to amend your ping in Dennis' script to:

# ping xxx.$index -n 1

...such that only one ping is sent for each address.

Regards!

...JRF...
Ralph Grothe
Honored Contributor

Re: For loop

Nevertheless, Bash should understand a typeset declaration

$ echo $0
-bash
$ echo ${BASH_VERSINFO[*]}
3 00 15 1 release i386-redhat-linux-gnu
$ typeset -i some_int=42
$ declare -i|grep some
declare -i some_int="42"
Madness, thy name is system administration
Hein van den Heuvel
Honored Contributor

Re: For loop

For a flexible, scriptable ping usage, You might want to consider the PERL module Net::Ping

Basic docu:

http://perldoc.perl.org/Net/Ping.html

Extended example usage in attachment to Clay's note in:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=22018


Simple example for your case:

--------------------- ping_network.pl -----
use strict;
use Net::Ping;
my $timeout = 2;
my $network = shift;
($network =~ !/\d+\.\d+\.\d+/) or die "Please provide nextwork adrress as xx.xx.xx";
my $p = Net::Ping->new('tcp');
foreach (0..255) {
my $host = "$network.$_";
my $stat = $p->ping($host,$timeout);
# printf "%s %d %s\n", $host, $stat, $stat? 'up' : 'down';
print "$host\n" if $stat;
}
$p->close();
------------------------------

# perl ping_network.pl 192.168.1 > up.txt

Hope this helps some,
Hein van den Heuvel (at gmail dot com)
HvdH Performance Consulting
Ralph Grothe
Honored Contributor

Re: For loop

For scripting ping checkers I also prefer the Net::Ping module that Hein has introduced.
One thing noteworthy about it is
that it can be employed more universal than an ordinary ping command.
For one (as Hein demonstrated), you can emit ordinary TCP packets and thus don't require root privileges or a setuid script unlike with ICMP packets.
Even better yet, it allows you to virtually ping every service by selection of the destination port.
Albeit, if you are an OO purist you probably would frown on the Net::Ping module's bad interface that it doesn't offer a proper accessor for setting the port but rather requires the user to mangle with the Net::Ping object like so:

use Net::Ping;
my $p = Net::Ping->new('tcp');
# setting to SSH's registered port
$p->{port_num} = getservbyname('ssh', 'tcp');

Madness, thy name is system administration