Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Using Net::Ping for broadcast address

by birdbrane (Chaplain)
on Mar 30, 2001 at 21:48 UTC ( [id://68441]=perlquestion: print w/replies, xml ) Need Help??

birdbrane has asked for the wisdom of the Perl Monks concerning the following question:

Greets,

I am trying to right a series of scripts to map out all of our *nix hosts on our intranet, in order to ease administration. One part of that script requires that I ping each address. Problem is, if the subnet doesn't exist, I don't want to ping 255 addresses only to find nothing.

I wrote a perl script to ping the broadcast address (using Net::Ping) but it comes back that the address doesn't exist (even though a command line ping does).

Am I doing something incorrectly here, or does Net::Ping not work for pinging broadcast addresses? I have tried w/ and w/o ICMP, again, to no avail.

For information purposes, here is the script:

#! /usr/local/bin/perl -w use strict; use Net::Ping; my $IPBlk; my $FirstOctets = "10.0"; my $ThirdOctet; for ($ThirdOctet = 1; $ThirdOctet <= 25; $ThirdOctet++) { $IPBlk = "$FirstOctets." . "$ThirdOctet"; my $sbnt = "$IPBlk" . ".0"; my $p = Net::Ping->new(); if ($p->ping("$sbnt") ) { print "host exists\n"; } else { print "Host does not exist\n"; } print "$?\n"; }

Many thanks in advance,

Joe

Replies are listed 'Best First'.
Re: Using Net::Ping for broadcast address
by THRAK (Monk) on Mar 30, 2001 at 22:37 UTC
    A couple of thoughts...

    If you have no idea which "$ThirdOctet's the servers are on, then I don't see where you would have any choice but to cycle through them all. Either give it a list of them directly (see below) or you if you can parse it from /etc/hosts, build it dynamically.
    #! /usr/local/bin/perl -w use strict; use Net::Ping; my $IPBlk; my $FirstOctets = "10.0"; my @ValidOctets = (1, 5, 7); foreach my $ThirdOctet (@ValidOctets) { my $sbnt = "$FirstOctets." . "$ThirdOctet" . ".0"; print "Pinging: $sbnt\n"; my $p = Net::Ping->new(); if ($p->ping("$sbnt") ) { print "host exists\n"; } else { print "Host does not exist\n"; } print "$?\n"; }
    If you can parse /etc/hosts it might pay to put a distinguishing comment on the lines that are servers (### SERVER ###). You could then parse the file for these entries and build a list of known servers to ping via their direct address. Don't know it this is exactly what you are looking to accomplish, but you might want to try that approach instead. Might take some work to fix up your /etc/hosts file, but it would be a one-timer.

    -THRAK
    www.polarlava.com
      Hey THRAK,

      Thanks for the reply. Actually, I don't necessarily know which subnet ($ThirdOctet) the systems are on. Part of this script is to "auto-discover". By replacing Net::Ping w/ `/usr/sbin/ping `$sbnt`, I can scan the broadcast address, just look for the word /alive/ and it hums happily along.

      I have a working script, but I am just a little bummed that I can't get perl's ping to do job. In other words: why won't perl's ping allow/recognize a broadcast address ping response?

      Joe

        Joe, Are you running this as yourself or root? ICPM only works as root. Read the "Description" section here. I haven't used Net::Ping much, but I did play around with it a bit while trying to build a monitor for a bunch of distributed check printers.

        -THRAK
        www.polarlava.com
Re: Using Net::Ping for broadcast address (nmap -sP a.b.c.d/mask)
by ybiC (Prior) on Mar 30, 2001 at 23:19 UTC
    Sounds like a system call to nmap might do what you want.   (code) MAC n' IP cheese shows how I did something somewhat similar using nmap and Net::Ping.

    http://www.insecure.org/nmap/

        cheers,
        Don
        bumbling toward Perl Adept
        (it's pronounced "why-bick")

      THRAK,

      Yes, I am running the script as root, but trying an icmp (or tcp) hasn't worked. The docs don't seem to be helpful in that respect, but then again, I am sure most people don't use ping for this type of thing (but they should). :)

      Don,

      I went on to command line and tried the nmap command you recommended, but in order to get it to recognize that the subnet as a whole was up, I had to get a response back from all hosts. I am just interested in getting a single line back, (or even a '0' return code) upon success. I think that I am going to have to stick with a ping system call, as it doesn't seem that Net::Ping will ping a .0 address.

      Thanks for the suggestions and help,

      Joe

Re: Using Net::Ping for broadcast address
by knight (Friar) on Mar 31, 2001 at 04:09 UTC
    Try it with .255 for the last octet, not .0. Although modern IP stacks are supposed to treat an all-zero host portion as a broadcast address, there are still enough stacks around that only treat all-ones as broadcast that it's a little more reliable/portable to use that.

    Another possibility: Do you have a 255.255.255.0 netmask on the interface(s) in question? It would have to be for a last octet of all-ones or all-zeros to be treated as broadcast.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://68441]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-29 08:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found