Last active
December 14, 2015 06:19
-
-
Save vivekagr/5041303 to your computer and use it in GitHub Desktop.
Gathers and prints IP Address, WAN Status, SNR Margin, attenuation and transfer rate from ITI DNA-A211-I modem.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import re | |
from time import sleep | |
import requests | |
from requests.auth import HTTPBasicAuth | |
from bs4 import BeautifulSoup | |
from termcolor import colored | |
MAIN_PAGE_URL = "http://192.168.1.1/info.html" | |
WAN_INFO_URL = "http://192.168.1.1/wancfg.cmd?action=view" | |
ADSL_INFO_URL = "http://192.168.1.1/statsadsl.html" | |
STATS_PAGE_URL = "http://192.168.1.1/statswan.cmd" | |
CREDENTIALS = ('admin', 'password') | |
try: | |
wan_info = BeautifulSoup(requests.get(WAN_INFO_URL, auth=HTTPBasicAuth(*CREDENTIALS)).text) | |
adsl_info = BeautifulSoup(requests.get(ADSL_INFO_URL, auth=HTTPBasicAuth(*CREDENTIALS)).text) | |
main_page = requests.get(MAIN_PAGE_URL, auth=HTTPBasicAuth(*CREDENTIALS)).text | |
except requests.exceptions.ConnectionError: | |
print colored("Unable to connect to the router. Please check network connectivity.\n", 'red') | |
sys.exit() | |
ip_address = wan_info.find_all('tr')[1].find_all('td')[-1].text.strip() | |
wan_status = wan_info.find_all('tr')[1].find_all('td')[-2].text.strip() | |
snr_margin_down = adsl_info.find_all('tr')[6].find_all('td')[1].text.strip() | |
snr_margin_up = adsl_info.find_all('tr')[6].find_all('td')[2].text.strip() | |
attenuation_down = adsl_info.find_all('tr')[7].find_all('td')[1].text.strip() | |
attenuation_up = adsl_info.find_all('tr')[7].find_all('td')[2].text.strip() | |
rate_down = adsl_info.find_all('tr')[10].find_all('td')[1].text.strip() | |
rate_up = adsl_info.find_all('tr')[10].find_all('td')[2].text.strip() | |
gateway = re.findall("var dfltGw = '(.*)'", main_page)[0] | |
def main(): | |
if len(sys.argv) == 1: | |
print "\n" + "=" * 40 | |
print colored("Router Information", 'cyan') | |
print "=" * 40 | |
print "IP Address:", ip_address | |
print "Gateway:", gateway | |
if wan_status == "Up": | |
print "WAN Status:", colored(wan_status, 'green') | |
else: | |
print "WAN Status:", colored(wan_status, 'red', attrs=['bold', 'blink']) | |
print "SNR Margin: %s/%s" % (snr_margin_down, snr_margin_up) | |
print "Attenuation: %s/%s" % (attenuation_down, attenuation_up) | |
print "Rate: %s/%s" % (rate_down, rate_up) | |
print "=" * 40 + "\n" | |
elif len(sys.argv) == 2: | |
if sys.argv[1] == "reboot": | |
requests.get("http://192.168.1.1/rebootinfo.cgi") | |
print colored("Rebooting...", 'green') | |
elif sys.argv[1] in ["usage", "speed"]: | |
previous = 0 | |
while True: | |
try: | |
wan_info = BeautifulSoup(requests.get(STATS_PAGE_URL, auth=HTTPBasicAuth(*CREDENTIALS)).text) | |
except requests.exceptions.ConnectionError: | |
print("Unable to connect to the router. Please check network connectivity.") | |
sys.exit() | |
received = int(wan_info.find_all('tr')[2].find_all('td')[4].text) | |
if previous and received: | |
print str(round((received - previous)/1024.0, 2)) + ' kBps' | |
previous = received | |
sleep(1) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment