Skip to content

Instantly share code, notes, and snippets.

@Donearm
Created May 18, 2011 13:21
Show Gist options
  • Save Donearm/978555 to your computer and use it in GitHub Desktop.
Save Donearm/978555 to your computer and use it in GitHub Desktop.
Reboot, connect, disconnect and get DSL and Wifi info from a Netgear DG834GT router
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
""" Reboot, connect, disconnect and gets info about DSL and WiFi status from a Netgear DG834GT router"""
###############################################################################
# Copyright (c) 2010-2011, Gianluca Fiore
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
###############################################################################
__author__ = "Gianluca Fiore"
__license__ = "GPLv3"
__version__ = "0.9"
__date__ = "25122010"
__email__ = "forod.g@gmail.com"
__status__ = "stable"
import sys
import re
import telnetlib
import urllib2
import time
import Tkinter
from socket import setdefaulttimeout
DEBUGURL = 'http://192.168.0.1/setup.cgi?todo=debug'
REBOOTURL = 'http://192.168.0.1/setup.cgi?next_file=diag.htm&todo=reboot'
#DISCONNECTURL = 'http://192.168.0.1/setup.cgi?todo=disconnect&this_file=st_poe.htm&next_file=st_poe.htm'
DISCONNECTURL = 'http://192.168.0.1/setup.cgi?todo=disconnect'
#CONNECTURL = 'http://192.168.0.1/setup.cgi?todo=connect&this_file=st_poe.htm&next_file=st_poe.htm'
CONNECTURL = 'http://192.168.0.1/setup.cgi?todo=connect'
STATTBLURL = 'http://192.168.0.1/setup.cgi?next_file=stattbl.htm'
LOGOUTURL = 'http://192.168.0.1/setup.cgi?todo=logout'
INTERVALURL = 'http://192.168.0.1/setup.cgi?next_file=interval.htm'
STATUSURL = 'http://192.168.0.1/setup.cgi?next_file=s_status.htm'
# already the base64 encoded string, just to avoid shoulder surfing
BASE64STRING = 'XXXXXXXXXXX'
class RebooterWindow(Tkinter.Frame):
"""Main Tkinter window"""
def __init__(self, master=None):
Tkinter.Frame.__init__(self, master)
self.pack()
self.router_auth()
self.createWidgets()
def createWidgets(self):
# statusbar
self.sbarv = Tkinter.StringVar()
self.STATUSBAR = Tkinter.Label(self)
self.STATUSBAR["text"] = ''
self.STATUSBAR["relief"] = 'sunken'
self.STATUSBAR["anchor"] = 'sw'
self.STATUSBAR["cursor"] = 'xterm'
self.STATUSBAR["height"] = '2'
# self.STATUSBAR["width"] = '60'
self.STATUSBAR["textvariable"] = self.sbarv
self.STATUSBAR.pack({"side": "bottom"})
# reboot button
self.REBOOT = Tkinter.Button(self)
self.REBOOT["text"] = "Reboot"
# self.REBOOT["command"] = self.telnet_reboot
self.REBOOT["command"] = self.reboot
self.REBOOT.pack({"side": "left"})
# connect button
# this nearly always times out but it still works (at the
# very least the second time)
self.CONNECT = Tkinter.Button(self)
self.CONNECT["text"] = "Connect"
self.CONNECT["command"] = self.connect
self.CONNECT.pack({"side": "left"})
# disconnect button
self.DISCONNECT = Tkinter.Button(self)
self.DISCONNECT["text"] = "Disconnect"
self.DISCONNECT["command"] = self.disconnect
self.DISCONNECT.pack({"side": "left"})
# adslinfo button
self.ADSLINFO = Tkinter.Button(self)
self.ADSLINFO["text"] = "Adsl Info"
self.ADSLINFO["command"] = self.telnet_adslinfo
self.ADSLINFO.pack({"side": "left"})
# wifi_info button
self.WIFIINFO = Tkinter.Button(self)
self.WIFIINFO["text"] = "Wifi Info"
self.WIFIINFO["command"] = self.wifi_info
self.WIFIINFO.pack({"side": "left"})
# close button
self.CLOSE = Tkinter.Button(self)
self.CLOSE["text"] = "Close"
self.CLOSE["fg"] = "black"
self.CLOSE["command"] = self.quit
self.CLOSE.pack({"side": "left"})
def req_handler(self, req):
try:
result = urllib2.urlopen(req)
return result
except urllib2.HTTPError as e:
if e.code == 401:
self.router_auth()
else:
self.sbarv.set(e.code)
print(e.code)
sys.exit(1)
except urllib2.URLError as e:
self.sbarv.set(e.reason)
print(e.reason)
sys.exit(1)
def router_auth(self):
# Enable debug first with a http request using Basic Authorization
request = urllib2.Request(DEBUGURL)
request.add_header("Authorization", "Basic %s" % BASE64STRING)
result = self.req_handler(request)
def wifi_info(self):
tn = telnetlib.Telnet('192.168.0.1')
tn.write("iwconfig\n")
tn.write("exit\n")
output = tn.read_all()
for line in output.split("\n"):
if re.search("Link Quality", line):
self.sbarv.set(line)
tn.close()
def telnet_adslinfo(self):
tn = telnetlib.Telnet('192.168.0.1')
tn.write("adslctl info\n")
tn.write("exit\n")
output = tn.read_all()
for L in output.split("\n"):
if re.search('Channel', L):
self.sbarv.set(L)
tn.close()
def disconnect(self):
setdefaulttimeout(8)
request = urllib2.Request(DISCONNECTURL)
request.add_header("Authorization", "Basic %s" % BASE64STRING)
result = self.req_handler(request)
if result:
self.sbarv.set("Disconnected")
def connect(self):
request = urllib2.Request(CONNECTURL)
request.add_header("Authorization", "Basic %s" % BASE64STRING)
result = self.req_handler(request)
if result:
self.sbarv.set("Connected")
def reboot(self):
request = urllib2.Request(REBOOTURL)
request.add_header("Authorization", "Basic %s" % BASE64STRING)
result = self.req_handler(request)
if result:
self.sbarv.set("Please wait for the LEDs to be on before using the connection again")
def telnet_reboot(self):
# rebooting with telnet, somewhat more reliable than http
tn = telnetlib.Telnet('192.168.0.1')
tn.write("reboot\n")
tn.write("exit\n")
output = tn.read_all()
for L in output.split("\n"):
if re.search('reboot', L):
self.sbarv.set(L)
time.sleep(1)
self.sbarv.set("Please wait for the LEDs to be on before using the connection again")
tn.close()
def main():
root = Tkinter.Tk()
root.title("Netgear Dg834GT Rebooter")
win = RebooterWindow(master=root)
win.mainloop()
root.destroy()
return 0
if __name__ == '__main__':
status = main()
sys.exit(status)
@tboneticklez
Copy link

Urllib

@tboneticklez
Copy link

Urllib

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment