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

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:

PING www.google.com(2607:f8b0:400a:803::2004) 56 data bytes --- www.google.com ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 36.669/36.669/36.669/0.000 ms PING perlmonks.org (66.39.54.27) 56(84) bytes of data. --- perlmonks.org ping statistics --- 3 packets transmitted, 0 received, 100% packet loss, time 2035ms PING microsoft.com (40.112.72.205) 56(84) bytes of data. --- microsoft.com ping statistics --- 3 packets transmitted, 0 received, 100% packet loss, time 2031ms
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:

$ cat 4.ping3.pl #!/usr/bin/perl -w use 5.011; use Path::Tiny; use POSIX qw(strftime); # initialization that must precede main data structure # User: enter a subdirectory you would like to create # enter a subdirectory of this^^^ for output my $ts = "template_stuff"; my $output = "translations"; ## turning things to Path::Tiny my $abs = path(__FILE__)->absolute; my $path1 = Path::Tiny->cwd; my $path2 = path( $path1, $ts ); say "abs is $abs"; say "path1 is $path1"; say "path2 is $path2"; print "This script will build the above path2. Proceed? (y|n)"; my $prompt = <STDIN>; chomp $prompt; die unless ( $prompt eq "y" ); my $template_file = "1.monk.tmpl"; my $abs_to_template = path( $path2, $template_file )->touchpath; my $string1 = '<{$symbol}></{$symbol}>'; my $return5 = $abs_to_template->spew_utf8($string1); say "return5 is $return5"; # script params my %vars = ( monk_tags => path( $path2, $template_file ), translations => path( $path2, $output ), book => 'monastery tags ', ); my $rvars = \%vars; my $return1 = write_monk_tags($rvars); say "return1 is $return1"; my $munge = strftime( "%d-%m-%Y-%H-%M-%S", localtime ); $munge .= ".monk.txt"; # use Path::Tiny to create and write to a text in relevant directory my $save_file = path( $vars{$output}, $munge )->touchpath; my $return2 = $save_file->spew_utf8($return1); say "return2 is $return2"; ## ping a few sites and add it to this log; append output # keep time my $start = time; my $return3 = ping_sites($save_file); say "return3 is $return3"; say time - $start, "seconds elapsed during pinging"; say "created file $save_file"; system("gedit $save_file &"); sub ping_sites { use 5.011; use Path::Tiny; use Net::Ping; my $outfile = shift; my $pinger = Net::Ping->new( 'tcp', 3 ); for my $dest ( 'www.google.com', 'www.translate.yandex.ru', 'www.bing.com', 'www.yahoo.com' ) { $pinger->ping($dest) or print "### $dest is unreachable\n"; } $pinger->close; return "done with pinging"; } sub write_monk_tags { use warnings; use 5.011; use Text::Template; my $rvars = shift; my %vars = %$rvars; my $body = $vars{"monk_tags"}; my $template = Text::Template->new( ENCODING => 'utf8', SOURCE => "$body", ) or die "Couldn't construct template: $!"; my $return = "$vars{\"book\"}\n"; # User: change these quoted values for different order or tags my @buchstaben = qw/i p c readmore b/; for my $i (@buchstaben) { $vars{"symbol"} = $i; print "How many $i tag pairs would you like?: "; my $prompt = <STDIN>; chomp $prompt; if ( $prompt lt 1 ) { $prompt = 0; } while ( $prompt gt 0 ) { my $result = $template->fill_in( HASH => \%vars ); $return = $return . $result; --$prompt; } } return $return; } __END__ $
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,

Replies are listed 'Best First'.
Re^8: using online translation engines with perl (ping)
by hippo (Archbishop) on Nov 29, 2018 at 10:13 UTC
    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.

        You might want to give Net::Ping::External a try. From its docs:

        ... this module will probably provide more accurate results than Net::Ping will. Why? ICMP ping is the most reliable way to tell whether a remote host is alive. However, Net::Ping cannot use an ICMP ping unless you are running your script with privileged (AKA "root") access. The system's "ping" command uses ICMP and does not usually require privileged access.