in reply to Re^3: using online translation engines with perl
in thread using online translation engines with perl

Let me just ask: do people ping anymore, or is that an old-fashioned way to see if one has internet connectivity?

I still use ping for this as it gives more useful diagnostics than many other methods and it doesn't fail due to certificate failures, etc.

For example, I pinged perlmonks and got no answer, and had my terminal tied up.

You need to know how to use the tool of your choice. If you just run something like you have as

my $trial = system("ping www.google.com");

how do you expect it ever to finish? Using some of the options restricts the action to a finite process:

$ ping -nc1 perlmonks.org PING perlmonks.org (209.197.123.153) 56(84) bytes of data. 64 bytes from 209.197.123.153: icmp_seq=1 ttl=57 time=90.1 ms --- perlmonks.org ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 90.195/90.195/90.195/0.000 ms $

Response received, data displayed, no tying up the terminal forever. Alternatively, see Net::Ping.

Replies are listed 'Best First'.
Re^5: using online translation engines with perl (ping)
by Aldebaran (Curate) on Nov 26, 2018 at 07:34 UTC

    I was unable to replicate your results.

    $ ping -ncl perlmonks.org ping: bad number of packets to transmit. $ ping -ncl www.perlmonks.org ping: bad number of packets to transmit. $

    I found the c option to be relevant to what I'm shooting for now. I don't mean to be dismissive about the others, but I hardly understood them. In man ping, these are desribed as follows:

    -n Numeric output only. No attempt will be made to lookup +symbolic names for host addresses. -c count Stop after sending count ECHO_REQUEST packets. With +deadline option, ping waits for count ECHO_REPLY packets, until t +he time‐ out expires. -l preload If preload is specified, ping sends that many packets n +ot wait‐ ing for reply. Only the super-user may select preload m +ore than 3.

    I went with different options to see how it would dovetail with forked processes. I'll have more about ping at the end of this post, but I wanted to post the functions that would house it first, with the logic driven by an initial sftp->mkdir method call. First, I ssh to my site and put a plain file where I know the script is going to want to ask for a new directory:

    (uiserver):u61210220:~/pmimage$ pwd >1.pitty2 (uiserver):u61210220:~/pmimage$ cat 1.pitty2 /kunden/homepages/9/d349337426/htdocs/pmimage (uiserver):u61210220:~/pmimage$

    Then I run my normal html template with the new functions. Output then source:

    Put file to server(y/n)?: y server dir is perlmonks parameter array is perlmonks Net::SFTP::Foreign=HASH(0x55825b47bcc8) C +ouldn't create remote directory: Failure error is Couldn't create remote directory: Failure
    sub createDir { use 5.011; use Net::SFTP::Foreign; my ( $dirName, $sftp ) = @_; my $success = $sftp->mkdir($dirName) or handleDirCreateError( @_, $sftp->error ); return $success; } sub handleDirCreateError { use 5.011; use Net::SFTP::Foreign; use Net::SFTP::Foreign::Constants qw(:error); my ( $dirName, $sftp, $error ) = @_; say "parameter array is @_"; say "error is $error"; return $error; }

    The error's description is less than complete.

    I'm trying to get the functionality described in https://metacpan.org/pod/Net::SFTP::Foreign::Constants but seem to be missing something, as my error message looks a lot different than the ones in that module. One hopes to catch salva's eye, but it's improbable this deep in my own shit.

    Q1) How do I employ the constants in that module for this case?

    I've folded together elementary forking and pinging, and I really don't get what I've unearthed. This is the source:

    $ cat 6.fork.pl #!/usr/bin/perl -w use 5.011; my $start = time; if (fork() == 0) { # arm the alarm clock alarm(10); # create a child process that sleeps system("ping www.google.com >5.fork.txt"); exit(0); } say "relevant processes with pstree command:"; system("pstree -Apal $$"); while((my $pid = wait()) != -1) { say "$pid terminated" } system("cat 5.fork.txt"); say time-$start, " ? <-ten seconds elapsed from forked process" __END__ $

    This was the first trial:

    This was as I expected it. The icmp_seq= went from 1 to 10. I expected the same output if run again and was wrong:

    The icmp_seq= values go up to 152, and there is twice as much output. The third trial had these values all over the map:

    Q2) What does icmp_seq=608 mean in this context?

    Q3) With 30 seconds of pinging google, how much data was sent? How much of this type of activity can a person do without it being interpreted as an attack?

    What really threw me for a loop is when I walked away from my laptop, came back later to it and find:

    $ ping: sendmsg: Network is unreachable ping: sendmsg: Network is unreachable ping: sendmsg: Network is unreachable ...

    They descended like ghosts, long after the programs were supposedly done executing, and twice.

    After a couple of days of making mistakes, this seems like a good ping command to test whether one has internet connectivity:

    $ ping -c 3 www.google.com PING www.google.com(sea15s12-in-x04.1e100.net (2607:f8b0:400a:809::200 +4)) 56 data bytes 64 bytes from sea15s12-in-x04.1e100.net (2607:f8b0:400a:809::2004): ic +mp_seq=1 ttl=54 time=17.0 ms 64 bytes from sea15s12-in-x04.1e100.net (2607:f8b0:400a:809::2004): ic +mp_seq=2 ttl=54 time=21.1 ms 64 bytes from sea15s12-in-x04.1e100.net (2607:f8b0:400a:809::2004): ic +mp_seq=3 ttl=54 time=19.0 ms --- www.google.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2002ms rtt min/avg/max/mdev = 17.096/19.092/21.126/1.649 ms

    Q4) What could you regex for to decide that this were a success?

    Finally, I didn't want to post before taking a whirl with hippo's suggestion of Net::Ping. Here's the source:

    #!/usr/bin/perl -w use 5.011; use Net::Ping; my $start = time; if ( fork() == 0 ) { # arm the alarm clock alarm(5); my @sites = qw/www.google.com www.merrillpjensen.com www.perlmonks.o +rg/; for my $host (@sites) { my $p = Net::Ping->new(); print "$host is alive.\n" if $p->ping($host); $p->close(); } exit(0); } say "relevant processes with pstree command:"; system("pstree -Apal $$"); while ( ( my $pid = wait() ) != -1 ) { say "$pid terminated"; } #system("cat 7.fork.txt"); say time - $start, " ? seconds elapsed from forked process" __END__

    The unimpressive output is:

    relevant processes with pstree command: 7.fork.pl,27579 -w ./7.fork.pl |-7.fork.pl,27580 -w ./7.fork.pl `-pstree,27581 -Apal 27579 27580 terminated 5 ? seconds elapsed from forked process

    The intent is to loop over a few sites and see if pinging is successful. I wonder what makes a site pingable like google, and how we might handle a ping sent to our own sites.

    Anyways, long post, arrrggh...

    Thank you for your comments,

      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.

        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,

      I'll reply in detail later but wanted to clear up this one point now in case it helps.

      I was unable to replicate your results.
      $ ping -ncl perlmonks.org

      That's because you've transcribed it instead of copy-and-paste. I haven't used the -l option, I've passed a 1 (the digit one) to the -c option. Of course "l" is a bad number of packets to transmit because it isn't a number. HTH.