Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
#! /usr/bin/perl -Tw use strict; use vars qw/ $opt_b $opt_n $opt_q $opt_r $opt_s $opt_t /; use constant TIMEOUT => 1; use Getopt::Std; use Socket; use Net::Ping; getopts( 'bnrqst:' ); $opt_r = 1 if $opt_n or $opt_s; $opt_q = 1 if $opt_b; $opt_t ||= TIMEOUT; die "This script must be installed setuid root.\n" unless $> == 0; my $first_taint = shift or die "No first IP address given (e.g. 172.17 +.1.1)\n"; my $last_taint = shift or die "No last IP address given (e.g. 127)\n" +; my ($net,$first) = ($first_taint =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3})\.( +\d{1,3})$/ ); die "Couldn't determine the network part in $first_taint.\n" unless $n +et; die "Couldn't determine the host part in $first_taint.\n" unless $firs +t; my ($last) = ($last_taint =~ /^(\d{1,3})$/ ); die "Couldn't determine the host part in $last_taint.\n" unless $last; ($last, $first) = ($first, $last) if $last < $first; my $p = Net::Ping->new( 'icmp' ); foreach my $host ( $first..$last ) { my $ip = "$net.$host"; my $hostname = do { if( $opt_r and my $resolve = gethostbyaddr( inet_aton($ip), AF +_INET )) { $resolve =~ s/\..*$// if $opt_s; $resolve .= " ($ip)" if $opt_n; $resolve; } else { $ip; } }; if( $p->ping($ip, $opt_t) ) { print $hostname; print ' is reachable.' unless $opt_b; print "\n"; } elsif( ! $opt_q ) { print "$hostname is NOT reachable.\n"; } } $p->close(); =head1 NAME pinger - ping a range of hosts =head1 SYNOPSIS B<pinger> [B<-bnqrs>] [B<-t> timeout] host last =head1 DESCRIPTION Ping a range of hosts using ICMP. =head1 OPTIONS =over 5 =item B<-b> Brief. Do not print the "is reachable/is NOT reachable" text. Probably needs B<-q> as well to be of any use to a program downstream. =item B<-n> Numeric. Print the numeric IP address as well as the resolved host nam +e. (Sets B<-r>). =item B<-q> Quiet. Only report hosts that are pinged successfully. =item B<-r> Resolve. Resolve the numeric address to a FQDN host (implies -q unless + -n). =item B<-s> Short. Like B<-r>, but resolves the numeric address to a simple host n +ame (stopping at the first dot). =item B<-t timeout> Timeout. Set the time out to t seconds (defaults to 1). A low timeout +value is used as this script is destined to be used on internal networks where it is normal +to have sub-second response times. The value may be easily edited in the source to a high +er value if required. =back =head1 EXAMPLES C<pinger 10> Pings the machines from to Sample output loo +ks like: is reachable. is NOT reachable. is reachable. is reachable. is NOT reachable. is reachable. is NOT reachable. is reachable. is reachable. is reachable. C<pinger -r 6> Pings the machines from to and resolves the ho +stnames. In the following example no A record for exists, so the numeric IP address +is used instead. is reachable. is NOT reachable. is reachable. is reachable. is NOT reachable. is reachable. C<pinger -n 6> Like the above, but also includes the numeric IP address alongside the + resolved host name. ( is reachable. ( is NOT reachable. is reachable. ( is reachable. ( is NOT reachable. ( is reachable. C<pinger -sq 6> This time, only display the local hostnames without the domain, and on +ly report hosts that are pinged successfully. profane is reachable. is reachable. godolphin is reachable. porpentine is reachable. C<pinger -bq 6> Report the IP addresses of all hosts that are pinged successfully. =head1 BUGS Due to the way Net::Ping works, and TCP/IP in general, this script must be installed setuid root. In light of this constraint, the script runs in tainted mode. To install the script, run the following command: C<chmod 4555 pinger> The way the behaviour is controlled by the command-line switches is unnecessarily obfuscated and should be cleaned up. This is a by-produc +t of the script's evolution. By and large, however, they tend to do The Right Thing. =head1 SEE ALSO L<Net::Ping> =head1 COPYRIGHT Copyright 1999-2001 David Landgren. This script is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 AUTHOR David "grinder" Landgren eval {join chr(64) => qw[landgren]} =cut

In reply to pinger - ping a range of hosts by grinder

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [choroba]: Good morning, Monks!
    [Corion]: And a good daypart to you as well, Lady_Aleena !
    [robby_dobby]: Hey monkeys, do you ever get the realization that you're all waking up to chaos? I suppose not :P
    [Lady_Aleena]: Good morning Corion, I hope you have a wonderful day as well. 8)
    [Corion]: Yesterday I encountered an interesting data structure problem. I have a remote program that emits events, and my client listens for these events with one-shot callbacks, that is, I register the callback and if the event gets generated that callback ...
    [Lady_Aleena]: robby_dobby, every day. Chaos is my life with few controls.
    [Corion]: ... gets called once. The data structure for that is just a hash of arrays, mapping the event type to a queue of registered one-shots, and the first one-shot from the queue gets removed and called.
    [Corion]: But now I want to register a one-shot for two events, of which only one will arrive, so my data structure doesn't work anymore...
    [Lady_Aleena]: Corion, ouchy.
    [Corion]: (maybe I should write this up as a SoPW) - currently, the "most efficient" data structure I come up with is a single array which I scan for the first fitting one-shot. Not efficient but I don't expect more than five outstanding one-shots anyway

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (9)
    As of 2017-05-29 07:52 GMT
    Find Nodes?
      Voting Booth?