www.farid-hajji.net banner

Farid Hajji

Perl: Einführung, Anwendungen, Referenz (2/e) [Support-Site]

Farid Hajji: Perl - Einführung, Anwendungen, Referenz
2., aktualisierte und erweiterte Auflage
Addison-Wesley Longman, ISBN 3-8273-1535-2

Ergänzung

ping.pl
#!/usr/local/bin/perl -w
# ping.pl -- Entfernte Hosts mit TCP, UDP oder ICMP an'ping(1)'en.

use Getopt::Std;
use vars qw ($opt_T $opt_U $opt_I $opt_P $opt_t $opt_h);
my $prog = $0; $prog =~ s,.*/,,;
getopts("TUIPt:h") or usage();
my $timeout = $opt_t || 3;

use Net::Ping;
my ($p_tcp, $p_udp, $p_icmp) = ( Net::Ping->new("tcp", $timeout),
                     Net::Ping->new("udp", $timeout),
                     defined $opt_I ?
                       Net::Ping->new("icmp", $timeout) : undef );

foreach my $host (@ARGV) {
    print "$host:\t[TCP]\t", $p_tcp->ping($host) ? "ok" : "not ok", "\n"
    if defined $opt_T;

    print "$host:\t[UDP]\t", $p_udp->ping($host) ? "ok" : "not ok", "\n"
    if defined $opt_U;

    print "$host:\t[ICMP]\t", $p_icmp->ping($host) ? "ok" : "not ok", "\n"
    if defined $opt_I;

    print "$host:\t[ping]\t", ping_ping($host, $timeout), "\n"
    if defined $opt_P;
}

sub ping_ping {
    my $host    = shift;
    my $timeout = shift;

    if ($^O eq 'solaris') {
    # System V kompatibles ping(1) ohne -s Option.
    return system("ping $host $timeout >/dev/null 2>/dev/null") ?
        "not ok" : "ok";
    } elsif ($^O eq 'freebsd') {
    # BSD kompatibles ping(1), wobei nur 1 Paket gesendet wird.
    # ACHTUNG: $timeout wird hier leider nicht beruecksichtigt!
    return system("ping -c 1 $host >/dev/null 2>/dev/null") ?
        "not ok" : "ok";
    } else {
    return "Operating system $^O not supported";
    }
}

sub usage {
    print STDERR <<"EOUSAGE";
Usage: $prog [-T | -U | -I | -P] [-t timeout] remotehost1 [remotehost2 ...]
       $prog -h

    -T          Is the echo/tcp service up and running?
    -U          Is the echo/udp service up and running?
    -I          Does the host respond to ICMP Echo-Requests?
                [This is the default, and what ping(1) does].
                -I requires the program to be run as 'root'.
    -P          Call the external ping(1) Program.
    -t timeout  Wait at most timeout seconds for a reply.
    -h          Displays this help screen.
EOUSAGE

    exit 1;
}
   

[Prev] [Up] [Relevant Chapter] [Next]

[Alte Quelle]


Last modified: $Date: 2006/05/18 12:55:48 $
FH. Search :: Sitemap :: Disclaimer :: Copyright :: Privacy
FreeBSD Logo