Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: writing a utility to determine whether I have a working wifi connection

by RonW (Parson)
on Jul 25, 2019 at 23:18 UTC ( #11103399=note: print w/replies, xml ) Need Help??


in reply to writing a utility to determine whether I have a working wifi connection

Another option is LWP::Online. Be aware, according to the docs, it a does a GET from Google, CNN, Yahoo and Amazon.

If I were doing this, I think I'd use LWP::Simple to GET from an page on my ISP's website. In part because if I can GET a page from my ISP then I know any other access problem is their's, not mine. (Of course, if there's website you want/need to make sure you can GET from, then test that, too.)

It might also be useful to GET your modem's status page. It can tell if your connection from your house or office to your ISP is up or not.

  • Comment on Re: writing a utility to determine whether I have a working wifi connection

Replies are listed 'Best First'.
Re^2: writing a utility to determine whether I have a working wifi connection
by bliako (Prior) on Jul 26, 2019 at 13:36 UTC

    That's simple enough. And can be useful if you hit one of the "find my IP" sites to also get your DHCP-assigned IP as a bonus. Hitting google is very dangerous.

Re^2: writing a utility to determine whether I have a working wifi connection
by Aldebaran (Deacon) on Jul 29, 2019 at 19:29 UTC

    (re-ordered for thematic reasons) Thanks all for responses.

    It might also be useful to GET your modem's status page. It can tell if your connection from your house or office to your ISP is up or not.

    This simple advice came just in time for me to ask the comcast guy the address. It isn't the old 192.0.0.1 it used to be, and the page seems to have more functionality than I recall.

    Another option is LWP::Online.

    I'd like to unpack this a bit. First, I do have a working script:

    $ ./1.lwp.pl execution here execution there $ cat 1.lwp.pl #!/usr/bin/perl use 5.016; use warnings; use LWP::Online 'online'; say "execution here"; # "Is the internet working?" die "NO INTARWWEB!!!" unless online(); say "execution there"; __END__ $

    I attempted to dig through a few parts of this listing for Online.pm. I have any number of questions after having contended with the source for several days now. Regarding this snippet:

    # Set up configuration data use vars qw{%SUPPORTED @RELIABLE_HTTP}; BEGIN { # What transports do we support %SUPPORTED = map { $_ => 1 } qw{ http }; # (Relatively) reliable websites @RELIABLE_HTTP = ( # These are some initial trivial checks. # The regex are case-sensitive to at least # deal with the "couldn't get site.com case". 'http://www.msftncsi.com/ncsi.txt' => sub { $_ eq 'Mic +rosoft NCSI' }, 'http://google.com/' => sub { /About Goo +gle/ }, 'http://yahoo.com/' => sub { /Yahoo!/ + }, 'http://amazon.com/' => sub { /Amazon/ a +nd /Cart/ }, 'http://cnn.com/' => sub { /CNN/ + }, ); }

    What is happening with the subs here?

    You get about hip deep in it and need to look at source for LWP::Simple. Is this a core module?

    In this snippet from LWP::Online, how does it import a lexical variable?

    use LWP::Simple 5.805 qw{ get $ua }; use vars qw{$VERSION @ISA @EXPORT_OK}; BEGIN { $VERSION = '1.08'; # We are an Exporter require Exporter; @ISA = qw{ Exporter }; @EXPORT_OK = qw{ online offline }; # Set the useragent timeout $ua->timeout(30); }

    This is get in LWP::Simple

    sub get ($) { my $response = $ua->get(shift); return $response->decoded_content if $response->is_success; return undef; }

    How would one add his own site to @RELIABLE_HTTP?

    Thanks for your comments

      WRT your last question, @RELIABLE_HTTP is a package variable so you should be able to push (or otherwise manipulate it) after you've pulled in the module.

      use LWP::Online; push @LWP::Online::RELIABLE_HTTP, q{http://groot.example.com} => sub { + /I AM GROOT/ }; ## Call http_online however . . .

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

      What is happening with the subs here?

      They are used by the code in the sub http_online as $check, where first $_ is set to the content fetched from the URL, and then the sub is called, and its return value used as a boolean. Although the subs you see here could be done by precompiled regexes (qr//), a sub allows for much more flexibility, a simple example being the sub { /Amazon/ and /Cart/ } case.

      LWP::Simple. Is this a core module?

      No, as you can find out with the corelist command:

      $ corelist LWP::Simple ... LWP::Simple was not in CORE (or so I think)

      However, the module's distribution, libwww-perl, is quite often used by scripts, so that e.g. Strawberry Perl includes it, and it's fairly common to find it already installed alongside Perl on many *NIX systems.

      In this snippet from LWP::Online, how does it import a lexical variable?

      Just to nitpick, it's not a lexical variable, it's a package variable. They can be exported/imported just like subs, e.g. Exporter supports this.

Re^2: writing a utility to determine whether I have a working wifi connection
by karlgoethebier (Abbot) on Jul 26, 2019 at 08:58 UTC

    You can change @RELIABLE_HTTP in the source code. Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11103399]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2021-01-21 11:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?