cancel
Showing results for 
Search instead for 
Did you mean: 

2704n Linux Bash Script to Monitor ADSL

smurphos
Hooked
Posts: 8
Registered: 05-01-2017

2704n Linux Bash Script to Monitor ADSL

Hi,

Cobbled together this script to periodically scrape stats from my 2704n and write them to a .csv file. It's not solely my work - it included elements inspired by darsh from

https://community.plus.net/t5/Tech-Help-Software-Hardware-etc/Unlocking-the-potential-of-Sagemcom-27...

and

https://gist.github.com/potchin/44cfdf88b31af19ab7dc

Hopefully may be of use to some people.

Written and tested on a Linux Mint 18 install.

Prerequisites

curl installed - sudo apt install curl

xmllint installed - sudo apt install libxml2-utils

Create a suitable empty stats file in .csv format. If you want to create a header row the stats being pulled are:

Date, Time, DSL Uptime, Downstream Sync, Upstream Sync, Downstream Atn, Upstream Atn, Downstream Noise Margin, Upstream Noise Margin, Downsteam ES, Upstream ES, Downstream SES, Upstream SES

Edit the script to add your router's password and the path to your stats file. Save as xxxx.sh and ensure it is set to be executable in permissions

add a cronjob entry via sudo crontab -e to run the script on a regular basis

My crontab entry is is set to run every ten minutes and looks like this....

*/10 * * * * /path/to/xxxx.sh

#!/bin/bash
# scraper script for pulling ADSL data from a plusnet 2704n router
PASSWORD="***********" #replace with your routers password
IP="192.168.1.254" #default
USERNAME="admin" #default
cookiefile=$(mktemp)
tmpfile=$(mktemp)
tmpfile2=$(mktemp)
date=`date +%Y-%m-%d`
time=`date +%H:%M:%S`
stats=/path/to/statsfile.csv #path to your stats file - create this file before running the script
curl -s -c $cookiefile -o /dev/null --data-ascii "loginuser=admin&loginpasswd=${PASSWORD}" "http://${IP}/plusnetlogin.cgi" || exit 1
curl -s -b $cookiefile "http://${IP}/helpdesk.html"> $tmpfile|| exit 1
syncdown=$(cat $tmpfile | grep "var dsldownLineRate"| tr -cd '[[:digit:]]')
syncup=$(cat $tmpfile | grep "var dslupLineRate"| tr -cd '[[:digit:]]')
dslinfo=$(cat $tmpfile| grep "var dslinfo" | awk -F '"' {'print $2'})
attdown=$(echo $dslinfo | awk -F '|' {'print $2'} | awk -F '/' {'print $2'} )
attup=$(echo $dslinfo | awk -F '|' {'print $2'} | awk -F '/' {'print $1'} )
snmdown=$(echo $dslinfo | awk -F '|' {'print $1'} | awk -F '/' {'print $2'} )
snmup=$(echo $dslinfo | awk -F '|' {'print $1'} | awk -F '/' {'print $1'} )
uptime=$(cat $tmpfile | sed -n '/6 - Uptime:/{n;p;}' | awk -F'[<|>]' {'print $3'} )
curl -s -b $cookiefile "http://${IP}/statsadsl.html"> $tmpfile2 || exit 1
esdown=$(xmllint --html --xpath '//tr[position()=42]/td[position()=2]/text()' $tmpfile2)
esup=$(xmllint --html --xpath '//tr[position()=42]/td[position()=3]/text()' $tmpfile2)
sesdown=$(xmllint --html --xpath '//tr[position()=43]/td[position()=2]/text()' $tmpfile2)
sesup=$(xmllint --html --xpath '//tr[position()=43]/td[position()=3]/text()' $tmpfile2)
echo "$date,$time,$uptime,$syncdown,$syncup,$attdown,$attup,$snmdown,$snmup,$esdown,$esup,$sesdown,$sesup" >> $stats
 

 

Tags (3)
3 REPLIES
Community Veteran
Posts: 4,778
Thanks: 1,055
Fixes: 27
Registered: 16-10-2014

Re: 2704n Linux Bash Script to Monitor ADSL

Hi @smurphos - Can you post some of the output from the .CSV file please.

smurphos
Hooked
Posts: 8
Registered: 05-01-2017

Re: 2704n Linux Bash Script to Monitor ADSL

Here you go - yes my line isn't great at the moment hence my effort to keep track of what is going on....

Date Time DSL Uptime Downstream Sync Upstream Sync Downstream Atn Upstream Atn Downstream Noise Margin Upstream Noise Margin Downsteam ES Upstream ES Downstream SES Upstream SES
2017-01-21 09:00:01 0 days 00:17:05 16252 1071 26.5 12.5 5.8 3.3 53 0 10 0
2017-01-21 09:10:01 0 days 00:27:05 16252 1071 26.5 12.5 5.7 2.4 58 1 10 0
2017-01-21 09:20:01 0 days 00:37:06 16252 1071 26.5 12.5 5.3 0.3 72 1 10 0
2017-01-21 09:30:02 0 days 00:47:07 16252 1071 26.5 12.5 5.4 1.1 80 1 10 0
2017-01-21 09:40:01 0 days 00:57:07 16252 1071 26.5 12.5 5.6 3.1 99 15 10 0
2017-01-21 09:50:01 0 days 00:01:34 15840 863 26.5 12.6 6.8 8.8 139 38 23 1
2017-01-21 10:00:01 0 days 00:11:34 15840 863 26.5 12.6 6.9 9.3 141 38 23 1
2017-01-21 10:10:01 0 days 00:21:35 15840 863 26.5 12.6 6.8 9.6 152 45 25 2
2017-01-21 10:20:01 0 days 00:31:36 15840 863 26.5 12.6 6.8 9.3 157 45 28 2
Community Veteran
Posts: 4,778
Thanks: 1,055
Fixes: 27
Registered: 16-10-2014

Re: 2704n Linux Bash Script to Monitor ADSL

Thanks for that @smurphos, shame it doesn't format correctly in the page, but that's not down to you.