in reply to Re^5: using online translation engines with perl (ping)
in thread using online translation engines with perl

It's not clear to me why you are using fork here but that certainly seems to be the root of your troubles. The doc for alarm says:

If you want to use "alarm" to time out a system call you need to use an "eval"/"die" pair.

... but you haven't done that in 6.fork.pl and therefore the ping processes are not reaped which is presumably why you see high icmp_seq counts on subsequent runs. Why use alarm at all instead of ping's built-in timeout?

Here are 2 examples with neither fork nor alarm. First, using system to execute the ping command:

#!/usr/bin/env perl use strict; use warnings; for my $dest ('www.google.com', 'www.perlmonks.org', 'microsoft.com', +'foo.bar') { system ("ping -nqc1 -w 3 -W 3 $dest > /tmp/ping.log") and print "### $dest is unreachable\n"; }

and now the same with Net::Ping:

#!/usr/bin/env perl use strict; use warnings; use Net::Ping; my $pinger = Net::Ping->new ('icmp', 3); for my $dest ('www.google.com', 'www.perlmonks.org', 'microsoft.com', +'foo.bar') { $pinger->ping ($dest) or print "### $dest is unreachable\n"; } $pinger->close;

Note that in both cases, microsoft.com is unreachable because it doesn't respond to pings and foo.bar is unreachable because it doesn't exist. The magic number 3 is the timeout to enforce in each case. As per the documentation the second script must be run as root to use ICMP pings.

Replies are listed 'Best First'.
Re^7: using online translation engines with perl (ping)
by Aldebaran (Curate) on Nov 29, 2018 at 00:50 UTC
    First, using system to execute the ping command:

    This first routine works quite well.

    ### www.perlmonks.org is unreachable ### microsoft.com is unreachable ping: foo.bar: Name or service not known ### foo.bar is unreachable return3 is done with pinging 7seconds elapsed during pinging created file /home/bob/Documents/meditations/template_stuff/translatio +ns/28-11-2018-08-18-41.monk.txt $

    The details are well-aligned and tell a story, one that is mercifully-short by machine standards:

    and now the same with Net::Ping:

    I did have to become superuser to execute this. Even so,

    $ ./4.ping3.pl ... Error utime () on '/home/bob/Documents/meditations/template_stuff/1.mo +nk.tmpl': Permission denied at ./4.ping3.pl line 26. $ sudo perl 4.ping3.pl ... ### www.google.com is unreachable ### www.translate.yandex.ru is unreachable ### www.bing.com is unreachable ### www.yahoo.com is unreachable return3 is done with pinging 13seconds elapsed during pinging

    I reach no one with either icmp nor tcp. Meanwhile, my wifi signal remains strong for every other thing I'm doing. Again, I've placed it in my little template for having monastery tags:

    If you want to use "alarm" to time out a system call you need to use an "eval"/"die" pair.

    Now that I see how I would have to shoe-horn it in, this doesn't seem like a viable option for legible, lexical, and portable perl. I'll drop this rock.

    Why use alarm at all instead of ping's built-in timeout?

    This seems to be a better option. I'll continue testing. That I can't reach google with Net::Ping is a head-scratcher. Thanks for your comments,

      That I can't reach google with Net::Ping is a head-scratcher.

      Indeed it is. I'm unable to reproduce that. Perhaps this pared-down test would be a starting point?

      use strict; use warnings; use Net::Ping; use Test::More tests => 2; my $dest = 'www.google.com'; my $pinger = Net::Ping->new ('icmp', 3); my $res; $res = system ("ping -nqc1 -w 3 -W 3 $dest > /tmp/ping.log"); is ($res, 0E0, "System ping to $dest"); $res = $pinger->ping ($dest); ok ($res, "Net::Ping to $dest"); $pinger->close;

      Running this I see the expected output.

      $ sudo prove -v googleping.t googleping.t .. 1..2 ok 1 - System ping to www.google.com ok 2 - Net::Ping to www.google.com ok All tests successful. Files=1, Tests=2, 1 wallclock secs ( 0.05 usr 0.01 sys + 0.05 cusr + 0.01 csys = 0.12 CPU) Result: PASS

      If you can run this and pass one test but not the other, I suggest firing up wireshark and see what's actually happening on the network. Good luck.

        Perhaps this pared-down test would be a starting point?

        I can't get anything from Net::Ping, likely because I understand less than half of it. To my way of the thinking, it is "actually" the system that pings, and the perl module that wraps this call. (Is that wrong?) If I can do that with a straight-up system call, then that's what they are for.

        I hadn't used prove before, so I was glad to see how this goes:

        $ sudo prove -v 5.ping3.pl [sudo] password for bob: 5.ping3.pl .. 1..2 ok 1 - System ping to www.google.com not ok 2 - Net::Ping to www.google.com # Failed test 'Net::Ping to www.google.com' # at 5.ping3.pl line 14. # Looks like you failed 1 test of 2. Dubious, test returned 1 (wstat 256, 0x100) Failed 1/2 subtests Test Summary Report ------------------- 5.ping3.pl (Wstat: 256 Tests: 2 Failed: 1) Failed test: 2 Non-zero exit status: 1 Files=1, Tests=2, 11 wallclock secs ( 0.04 usr 0.02 sys + 0.14 cusr + 0.03 csys = 0.23 CPU) Result: FAIL $ cat 5.ping3.pl #!/usr/bin/perl -w use 5.011; use Net::Ping; use Test::More tests => 2; my $dest = 'www.google.com'; my $pinger = Net::Ping->new ('icmp', 10); my $res; $res = system ("ping -nqc1 -w 3 -W 3 $dest > /tmp/ping.log"); is ($res, 0E0, "System ping to $dest"); $res = $pinger->ping ($dest); ok ($res, "Net::Ping to $dest"); $pinger->close; $ ping -nqc1 -w 3 -W 3 www.google.com PING www.google.com(2607:f8b0:400a:800::2004) 56 data bytes --- www.google.com ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 24.338/24.338/24.338/0.000 ms $

        So the problem exists somewhere between my chair and the operating system. But I've got a version that works, so I can just use that. Why play bad logic games? Between the following readmore tags are output, then source, including a template that provides monastery tags, and the output of the system command appended in that same, unique, time-based file:

        I'm happy with this result from this thread. Thank you for your comments.

        If you can run this and pass one test but not the other, I suggest firing up wireshark and see what's actually happening on the network.
        sudo add-apt-repository ppa:wireshark-dev/stable sudo apt-get update sudo apt-get install wireshark sudo wireshark

        Eye-popping software. Thx.