ITeye博客

restart router

liseor · 2011-05-06 16:02 · python · 编程语言 · Windows · XML · Python · CGI · Web · Markdown

#! /usr/bin/env python
# Script to restart daily my wireless router
# by Helio Loureiro
 
import httplib
import base64
import urllib
import time
from time import ctime
 
user = "admin"
passw = "password"
host = "192.168.1.1"
 
def GetAuth(user = "user", passw = "passwd"):
        auth = user + ":" + passw
        return base64.encodestring(auth)[:-1]
def restartRouter():
    headers = { 
            "Host" : host , 
            "User-Agent" : "Mozilla/5.0  (Python)",
            "Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" , 
            "Accept-Language" : "pt-br,pt;q=0.8,en-us;q=0.5,en;q=0.3",
            "Accept-Encoding" : "gzip,deflate", 
            "Accept-Charset" : "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
            "Referer" : "http://192.168.0.1/RST_st_poe.htm" ,
            "Authorization" : "Basic " +  GetAuth(user, passw),
            "Content-Type" : "application/x-www-form-urlencoded"
            }
     
    post_params_dis = {
            "ConMethod" : "Disconnect" 
            }
    
    post_params_con = {
            "ConMethod" : "%20%20Connect%20%20" 
            }
     
    params_dis = urllib.urlencode(post_params_dis)
    params_con = urllib.urlencode(post_params_con)
    web = httplib.HTTPConnection(host)
    web.request("POST", "/st_poe.cgi", params_dis, headers)
    response = web.getresponse()
     
    if (response.status == 200):
        print "[" + ctime() + "]  router stop successfully"
        time.sleep(2)
        web.request("POST", "/st_poe.cgi", params_con, headers)        
        response = web.getresponse()         
        if (response.status == 200):
            print "[" + ctime() + "]  router start successfully"
        else:
            print "[" + ctime() + "] Restart FAILED"
    else:
        print "[" + ctime() + "] Restart FAILED"
    
    #data = response.read()
    web.close()
#!/usr/bin/pytthon
#-*-coding:utf-8 -*-
#http://192.168.0.1/RST_st_poe.htm
import urllib2,cookielib,base64
upas = base64.b64encode('admin:password')
ip = '192.168.0.1'
url= "http://"+ip+"/RST_st_poe.htm"
print url
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar())) 
urllib2.install_opener(opener)
print '准备重启%s' %ip
print '伪照人工登录信息'
opener.addheaders = [('User-agent','Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1)'),('Accept-Encoding','gzip, deflate'),('Authorization',' Basic '+upas)] 
print '打开网页'

req = urllib2.Request(url)
print '请求重启信息'
print req
u = urllib2.urlopen(req) 
print u.read()
print '%s重启完成'%ip

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。