Introduction
In my last post, I showed how to get some stats from the gateway using wget and passing the username / password in plaintext from the command line.Here I will show how to do this in a slightly more secure manner, and determine whether the Internet connection is up or not.
Setup
This has been developed using the firmware version NF18ACV.NC.AU-R6B016.EN, and my Raspberry Pi running Raspbian GNU/Linux 9 (stretch).Note that it will not likely work with the newer "NC2" firmware, but in a future post I will show how getting stats using the gateway's CLI might work.
More secure method to send password with wget
Create .netrc file in home directory, if it doesn't already exist, and add the following line:machine 192.168.20.1 login admin password password
Where admin is the username, and password is the password.
You also need to make that file not readable by anyone else:
pi@rpi3:~$ chmod 600 ~/.netrc
Now we don't need to add the credentials to the URL:
pi@rpi3:~$ wget -q -O - http://192.168.20.1/info.html | \ > sed -n "/Aggregate Line Rate/{n;s/\(^.*<td>\)\(.*\)\(<\/td>.*$\)/\2/;p}" 10721 34317
Get connection status
Let's get the info page again, but this time just look at all the JavaScript variables that are defined:pi@rpi3:~$ wget -q -O - http://192.168.20.1/info.html | grep ^var var buildAtm = '1'; var buildPtm = '1'; var vdslp = '17a'; var buildMoca = '0'; var voice = 'SIP'; var VdslIncluded = '1'; var DslBondingEnabled = '0'; var vartractype0 = 'PTM' ; var vartractype1 = 'Inactive' ; var dfltGw = '123.246.123.1|VDSL'; var gwandtype; var buildSntp = '1'; var enblSntp = '1'; var proto = 'MER';
We see there the dfltGw var has the default gateway for the connection, a pipe character, and then the string VDSL.
For comparison, if I run this again with the phone cable disconnected from the box, the string is:
var dfltGw = ' ';
So, to if we get the string VDSL (this is in my situation), we can conclude the Internet connection is up.
To format that output more nicely, we can add few lines of JavaScript to the above:
gwandtype = dfltGw.split('|'); var MyConnType = gwandtype[1]; console.log(MyConnType);
This "split"s the field we are interested in into an array, we then get the second element of that array ([1]), and write that value to stdout (console.log).
To interpret all that JavaScript, we just run it all through NodeJS (node command), you may need to install it first.
Conclusion
Putting the above together, create a shell script getstat.sh:status=` ( wget -q -O - http://192.168.20.1/info.html | grep ^var echo "gwandtype = dfltGw.split('|');" echo "var MyConnType = gwandtype[1];" echo "console.log(MyConnType);" ) | node` echo $status
And run it:
pi@rpi3:~$ ./getstat.sh VDSL
No comments:
Post a Comment