Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by Aldebaran (Curate)
on Jul 25, 2019 at 21:03 UTC ( [id://11103395]=perlquestion: print w/replies, xml ) Need Help??

Aldebaran has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

We've got Comcast (an american internet service provider) showing up today as I got bumped off twice today for no reason I can ascertain. The service had been spotty, so they recommended getting a new router, which we did. I don't want this "spottiness" to take over my wifi situation and look to the tools I have. Right now, these are mostly bash commands or scripts:

$ cat .bash_aliases alias pt='perltidy -i=2 -utf8 -b ' alias cx='chmod +x ' alias bounce='sudo service network-manager restart ' alias shh="nmcli networking off" alias wake="nmcli networking on" $ shh $ wake $ shh $ wake $

The script I use to show me what's happening with the system journal is:

#!/bin/bash timename=$(date +"%m-%d-%Y_%H-%M-%S") _out_fn=$timename.txt echo "Time is $timename " >> $_out_fn nmcli network off >> $_out_fn nmcli network on >> $_out_fn env | sort >> $_out_fn sleep 40s echo ---journal control--- >> $_out_fn journalctl --since "1 minute ago" >> $_out_fn exit #*******************end**************************

My platform is

$ cat /etc/os-release NAME="Ubuntu" VERSION="18.04.2 LTS (Bionic Beaver)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 18.04.2 LTS"

, but I seek a solution that can port to strawberry perl (windows) if possible.

I would like to develop a perl-based tool that can discern whether I have a wifi connection that gets me to the internet. There must be any number of ways to do this.

Does one send out a request to some host name, which might be fine, until that host name doesn't want to answer what amounts to a ping?

Part of this software tool that I want to have would also tell me when the wireless goes out in a log. For this machine, I think I would grep events in journalctl. Might it be able to run as a daemon?It might retry the connection every minute or so.

I hope that we can work up a few solutions to compare and contrast. Thanks for your comment.

Replies are listed 'Best First'.
Re: writing a utility to determine whether I have a working wifi connection
by RonW (Parson) on Jul 25, 2019 at 23:18 UTC

    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.

      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-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.

      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

Re: writing a utility to determine whether I have a working wifi connection
by holli (Abbot) on Jul 25, 2019 at 21:33 UTC

      Interesting. I would not have thought to use Win32::API on a Linux system (unless it running under MS-WSL).

Re: writing a utility to determine whether I have a working wifi connection
by Discipulus (Canon) on Jul 26, 2019 at 08:23 UTC
    Hello Aldebaran,

    if you r program have to be portable you can have a bounch of modules handling this: X::Y::ConnectionInfo responsible to load X::Y::ConnectionInfo::Linux or X::Y::ConnectionInfo::Win32 based on the OS you are running on.

    See File::Spec (useful to write portable programs) and its structure.

    Under win32 you can consider to just parse the output of:

    netsh interface show interface

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      I ended up having so much trouble with the machine that I started this thread with that I went out and bought another. I was able to test whether I had wifi using perl, as kind of a first useful thing to do with a new windows machine. I'm not done looking at this issue, but I can now test it from another platform as well.

      C:\Users\tblaz\Documents\meditations>cd C:\Users\tblaz\Documents\meditations C:\Users\tblaz\Documents\meditations>perl 1.lwp.pl execution here execution there C:\Users\tblaz\Documents\meditations>type 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__ C:\Users\tblaz\Documents\meditations>

      This seems to confirm the following.

      C:\Users\tblaz\Documents\meditations>netsh interface show interface Admin State State Type Interface Name ---------------------------------------------------------------------- +--- Enabled Connected Dedicated Wi-Fi C:\Users\tblaz\Documents\meditations>
      if you r program have to be portable you can have a bounch of modules handling this: X::Y::ConnectionInfo responsible to load X::Y::ConnectionInfo::Linux or X::Y::ConnectionInfo::Win32 based on the OS you are running on.

      Can you say a few more words about how to create this namespace?

      Again, thx all for comments,

        "Can you say a few more words about how to create this namespace?"

        I believe that Discipulus was using that namespace as an example, but either way, here's how.

        Using Module::Starter. I've left in all of the commands so you can see what's happening:

        spek@scelia ~/scratch $ module-starter --module=X::Y::ConnectInfo::Win +32 --author="Your Name" --email=your@email.com --license=perl Added to MANIFEST: Changes Added to MANIFEST: ignore.txt Added to MANIFEST: lib/X/Y/ConnectInfo/Win32.pm Added to MANIFEST: Makefile.PL Added to MANIFEST: MANIFEST Added to MANIFEST: README Added to MANIFEST: t/00-load.t Added to MANIFEST: t/manifest.t Added to MANIFEST: t/pod-coverage.t Added to MANIFEST: t/pod.t Added to MANIFEST: xt/boilerplate.t Created starter directories and files spek@scelia ~/scratch $ cd X-Y-ConnectInfo-Win32/ spek@scelia ~/scratch/X-Y-ConnectInfo-Win32 $ ll lib/X/Y/ConnectInfo/ total 20 drwxr-xr-x 2 spek spek 4096 Aug 9 13:57 ./ drwxr-xr-x 3 spek spek 4096 Aug 9 13:57 ../ -rw-r--r-- 1 spek spek 1989 Aug 9 13:57 Win32.pm

        By hand is the same. Create any directory to hold the distribution which is your top(root)-level directory (I typically name mine lower-case with dashes, eg: top-container-thing), which contains all of your distribution information and directory hierarchy. To have X::Y::ConnectInfo::Win32, we'd then create a lib directory (after going into your top-level dir), then an X directory in that, a Y directory within X, another directory... you get the picture. The magic happens at the Win32.pm file. That's what gets registered in @INC. That's how a module is brought to life.

        All that also means, that if you create, then drop a Ubuntu.pm file within the lib/X/Y/ConnectInfo directory, it'll be registered by the system as X::Y::ConnectInfo::Ubuntu module, ready for use. It'll be seen the same on the CPAN.

        One last note... when we say "module", we mean the actual code rendered out of the .pm file. We refer to the entire package of all the stuff shown above as the module's "distribution". We install distributions from the CPAN, and a distribution can include one, or several modules.

Re: writing a utility to determine whether I have a working wifi connection
by Anonymous Monk on Jul 27, 2019 at 12:49 UTC

    I tend to use Net::Ping for this sort of thing. The CPAN Testers results say it's highly portable. Of course, you have to choose your ping protocol, and Net::Ping does not support absolutely everything. I tend to use icmp, which requires privilege, at least under a non-Windows system.

      Net::Ping ... I tend to use icmp, which requires privilege, at least under a non-Windows system.

      An alternative that doesn't require root is Net::Ping::External.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11103395]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 15:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found