科学院下面联网都是使用的srun3000,实验室原来是不需要认证就能上网的,最近居然跳出认证页面了。不过,srun3000的客户端体验感觉实在是太差了,经常假死,最近更奇怪的是经常掉线,且掉线之后不能进行自动重连,这造成我服务访问我的小服务器而言简直是不能忍受的。
看了下登陆页面的JS代码,发现协议已经描述的很清楚了,password用的是16位MD5加密后的字符串,其他几个字段没发现有什么特殊意义,对于登陆/注销的返回结果也描述的很清楚。这样的话就可以写个Python脚本循环检查是否能访问外网,如果不能就尝试登陆操作,如果登陆失败就先尝试注销。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | #!/usr/bin/env python # -*- coding:utf-8 -*- import re import time import urllib import urllib2 import hashlib import datetime class Srun: def __init__(self, host, username, password, interval): self.username = username self.password = hashlib.md5(password).hexdigest()[8:-8] self.interval = interval self.host = host self.headers = { "Host": self.host, "Connection": "keep-alive", "Content-Length": "60", "Origin": "http://" + self.host, "User-Agent": "Mozilla/5.0 (Windows NT 6.1) Chrome/25.0.1364.172", "Content-Type": "application/x-www-form-urlencoded", "Accept": "*/*", "Referer": "http://" + self.host + "/index.html", "Accept-Language": "zh-CN,zh;q=0.8", "Accept-Charset": "GBK,utf-8;q=0.7,*;q=0.3" } self.parameters = { "username": self.username, "password": self.password, "drop": "0", "type": "1", "n": "100" } self.login_url = "http://%s/cgi-bin/do_login" % self.host self.logout_url = "http://%s/cgi-bin/do_logout" % self.host def __get_param_len(self): res = 0 for k,v in self.parameters.items(): res += len(k) + len(v) + 1 return res-1 def __connect(self, url, param): self.parameters["n"] = "%d" % param self.headers["Content-Length"] = "%d" % self.__get_param_len() try: data = urllib.urlencode(self.parameters) req = urllib2.Request(url, data, self.headers) res = urllib2.urlopen(req) return res.read() except Exception, e: print e return None def __login(self): data = self.__connect(self.login_url, 100) if data == "ip_exist_error": self.__log("IP still online, sleeping 30 seconds") time.sleep(30) return self.__login() pattern = re.compile(r"\s*\d+[^\S]*") res = pattern.findall(data) if res != None and len(res) > 0: return True return False def __logout(self): data = self.__connect(self.logout_url, 1) if data == "logout_ok": return True return False def __checkOnline(self): req = urllib2.Request("http://www.baidu.com/") res = urllib2.urlopen(req) if res.read().find(self.host) != -1: return False return True def __log(self, msg): _time = datetime.datetime.now().strftime("%H:%M:%S") print "[%s] %s" % (_time, msg) def run(self): while True: if self.__checkOnline() == True: self.__log("online") self.__log("sleeping %d seconds" % self.interval) time.sleep(self.interval) continue self.__log("offline, try login now...") if self.__login() == True: self.__log("login success") else: self.__log("login failure, try logout now...") self.__log("sleeping 60 seconds after logout") self.__logout() time.sleep(60) def main(): while True: try: srun = Srun("{server_ip}", "{username}", "{password}", 30) #srun = Srun("{1.1.1.1}", "{22user}", "{33pwd}", 30) srun.run() except Exception, e: print e time.sleep(10) if __name__ == "__main__": main() |
GitHub: https://github.com/Wins0n/PyUtility/blob/master/SrunLogin.py
Git强制回滚到指定版本,将commit_id之后提交的数据彻底删除:
git reset --hard <commit_id> git push origin HEAD --force |
本文地址: 程序人生 >> Srun3000自动登陆脚本
作者:代码疯子(Wins0n) 本站内容如无声明均属原创,转载请保留作者信息与原文链接,谢谢!