#! /python21/python

"""
program: smc_sucks.py
creator: Rob Chavers www.chavers.us
created: May 7, 2002

purpose: RESET my SMC 7004AWBR Barricade Router because it sucks!
         NOTE: THIS PROGRAM WILL ONLY RESET THE 750.5435 MODEL, IT WILL NOT WORK WITH THE 750.5312 MODEL.

instructions: change smc_addr and smc_pass to YOUR own values
"""

import os
import sys
import time
import httplib

smc_addr = '192.168.2.1:88'
smc_pass = 'YOURPASSWORDHERE'

url_list = ['www.smc.com', 'www.aol.com', 'www.msn.com', 'www.hotmail.com']

logfile = '/html/log/smc.txt'
logsize = 10000     # limit the log size to 10k bytes

reset_list = ['http://%s/login.htm?pws=%s&page=login' %(smc_addr, smc_pass),
              'http://%s/tools_gateway.htm?page=tools_gateway&logout=2' %smc_addr]
mesg = ''


# create a connection to the site
# and "GET" the main page
#
found = 0
for url in url_list:
    try:
        conn = httplib.HTTPConnection(url)
        conn.request("GET", "/")
        resp = conn.getresponse()
        found = 1
    except:
        mesg += 'Could not connect: %s\n' %url

    try:
        conn.close()
    except:
        pass

    if found: break


reset = 0
if not found:
    try:
        h = httplib.HTTP(smc_addr)
        for url in reset_list:
            h.putrequest('GET', url)
            h.endheaders()
            h.getreply()
            reset += 1
    except:
        pass

    h.close()

    if reset == len(reset_list):
        mesg += '\nBarricade RESET was successful\n'
    else:
        mesg += '\nCOULD NOT RESET THE SMC BARRICADE\n'

if mesg:
    # setup the mesg to be exported to the top of the
    # smc.txt log file
    #
    mesg = '\n' + time.ctime(time.time()) + '\n\n' + mesg + '\n'
    mesg += '-' * 79 + '\n'


    try:
        log_txt = open(logfile, 'r').read()
    except:
        log_txt = ''

    log_txt = mesg + log_txt
    log_txt = log_txt[:logsize]

    try:
        open(logfile, 'w').write(log_txt)
    except:
        pass
