|
发表于 2004-12-13 20:20:51
|
显示全部楼层
我觉得这篇文章很好http://www.linuxsir.org/bbs/showthread.php...threadid=160764
QUOTE
长城宽带认证方法 ====================================下面是auth.py,认证主程序,程序是用python写的,参考了论坛里的精华帖子,在这里表示感谢///////////auth.py/////////////#! /usr/bin/env pythonimport osimport reimport httplib, urllibtemp = os.popen('ifconfig eth0 | awk '/inet/{print $2}' | awk -F: '{print $2}'') ////获取自己的ip地址IP = temp.read()err = temp.close()m = re.search("(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{2,3})", IP)MYIP = m.group(1) ////获取自己的ip地址的标准格式ff = open('/etc/auth.conf','r',-1) ////在/etc/auth.conf里放自己的密码和id.格式为myid=xxxxxxxxxxxxmypasswd=xxxxxxxxxidpasswd = ff.readline()ff.closem = re.search("myid=(\d{12})mypasswd=(.*)$",idpasswd)MYID = m.group(1)MYPASSWD = m.group(2)path = '/cgi-bin/authcgi_login' ////构造http请求,获得认证界面conn = httplib.HTTPConnection('vip.163.com')conn.request('GET','','')r = conn.getresponse()body = r.read()conn.closer1 = re.compile(r'.*http://(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\d{2,5})/cgi-bin/authcgi_login',re.MULTILINE)m = r1.search(body) ////获取认证服务器的地址和端口SERVER=m.group(1)PORT=m.group(2)params = urllib.urlencode([('sip',SERVER),('uip',MYIP),('url','vip.163.com'),('uact','login'),('uact_status',1),('uid',MYID),('upwd',MYPASSWD),('vpn','0'),('go','用户登入'),('btype','0')])ff = open('/var/tmp/server.conf','w+',-1)HOST = 'server='+SERVER+'port='+PORTff.write(HOST)ff.flush()ff.close ////把认证服务器的地址和端口写入临时文件中给后面的logout使用cmd = 'POST '+pathheader =' HTTP/1.1'+'\x0d\x0a'+'Accept: *.*'+'\x0d\x0a'+'Content-Type: application/x-www-form-urlencoded'+'\x0d\x0a'+'User-Agent: Microsoft Internet Explorer 6.0'+'\x0d\x0a'+'HOST:'+SERVER+':'+PORTlength = len(params)control = '\x0d\x0a'+'Content-Length: '+str(length)+'\x0d\x0a'+'Cache-Control: no-cache'+'\x0d\x0a'+'Cookie: sid=20040706210751'+'\x0d\x0a\x0d\x0a'import socketsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)sock.connect((SERVER,int(PORT)))sock.send(cmd+header+control+params) ////登录认证s = sock.recv(8192)r1 = re.compile(r'.*srvip=(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})&sport=(\d{2,5})&sid=(\d{8,10})&stime=(\d{9,11})&rmtime=(.?\d{1,2})&interval=(\d{1,2}).*chkey=(\d{14})',re.MULTILINE)m = r1.search(s)SERVER = m.group(1) PORT = m.group(2) ////获得keep-alive服务器的地址和端口ff = open('/var/tmp/auth.conf','w+',-1) ff.write(params)ff.flush()ff.close ////keep-alive服务器的地址和端口写入零时文件中给keep-alive程序使用SID = m.group(3)STIME = m.group(4)RMTIME = m.group(5)INTERVAL = m.group(6)CHKEY = m.group(7)params = urllib.urlencode([('uid',MYID),('uip',MYIP),('srvip',SERVER),('sport',PORT),('sid',SID),('stime',STIME),('rmtime',RMTIME),('interval',INTERVAL),('btype','0'),('vpn','0'),('wtype','0'),('chkey',CHKEY),])header =' HTTP/1.1'+'\x0d\x0a'+'Accept: *.*'+'\x0d\x0a'+'User-Agent: Microsoft Internet Explorer 6.0'+'\x0d\x0a'+'HOST:'+SERVER+':'+PORT+'\x0d\x0a'+'Cache-Control: no-cache'+'\x0d\x0a'+'Cookie: sid=20040706210751'+'\x0d\x0a\x0d\x0a'path = '/cgi-bin/timer'cmd='GET '+path+'?'+params+headersock.send(cmd) ////发送认证成功确认包s = sock.recv(8192) sock.closeparams = urllib.urlencode([('uid',MYID),('uip',MYIP),('sid',SID),('srvip',SERVER),('sport',PORT),('stime',STIME),('rmtime',RMTIME),('interval',INTERVAL),('time_span','0'),('act','logout')])ff = open('/var/tmp/auth.conf','w+',-1)ff.write(params)ff.flush()ff.close ////把认证返回的keep-alive所需要的id,interval等写入零时文件print 'Login OK'sock.closeos.system('/etc/rc.d/init.d/UDP.py') ////运行keep-alive程序os._exit(0) ////退出认证程序
QUOTE
keep-alive程序(UDP.py) ///////////UDP.py///////////#! /usr/bin/env python## Filename UDP.pyimport ospid=os.fork() ////获取自己的pid并创建一个守护进程 if pid:ff = open('/var/tmp/authpid.conf','w+',-1)ff.write(str(pid)) ////写入进程pid ff.flush()ff.closeprint 'pid='+str(pid)os._exit(0) ////关闭自身进程 from socket import socket,AF_INET,SOCK_DGRAMff = open('/var/tmp/auth.conf','r',-1) ////读取keep-alive参数 bufread=ff.readline()ff.closeimport rer1 = re.compile(r'.*sid=(\d{8,10})&srvip=(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})&sport=(\d{2,5}).*interval=(\d{1,2})',re.MULTILINE)m = r1.search(bufread)print 'Start to Send UDP package'SID=m.group(1)HOST=m.group(2)PORT=int(m.group(3))interval=int(m.group(4))import signaldef onSignal(signum, stackframe):if signum==signal.SIGALRM:s = socket(AF_INET,SOCK_DGRAM)s.sendto(SID,(HOST,PORT)) ////如果收到alarm信号则发送keep-alive包(udp包,只是简单的一个SID) s.closeif signum==9:os._exit(0) ////如果收到信号9就退出 while 1:signal.signal(signal.SIGALRM, onSignal)signal.alarm(interval) ////定时发出alarm信号 signal.pause() ////休眠
QUOTE
logout程序(logout.py) ///////logout.py////////#! /usr/bin/env pythonimport reff = open('/var/tmp/server.conf','r',-1)HOST = ff.readline()ff.closem = re.search("server=(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})port=(\d{2,5})",HOST)SERVER = m.group(1)PORT = m.group(2) ////读取服务器地址和端口 ff = open('/var/tmp/auth.conf','r',-1)params=ff.readline()ff.closepath = '/cgi-bin/timer'cmd = 'POST '+pathheader =' HTTP/1.1'+'\x0d\x0a'+'Accept: *.*'+'\x0d\x0a'+'Content-Type: application/x-www-form-urlencoded'+'\x0d\x0a'+'User-Agent: Microsoft Internet Explorer 6.0'+'\x0d\x0a'+'HOST:'+SERVER+':'+PORTlength = len(params)control = '\x0d\x0a'+'Content-Length: '+str(length)+'\x0d\x0a'+'Cache-Control: no-cache'+'\x0d\x0a'+'Cookie: sid=20040706210751'+'\x0d\x0a\x0d\x0a' ////构造logout包 import socketsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)sock.connect((SERVER,int(PORT)))sock.send(cmd+header+control+params) ////发送logout命令 import osff = open('/var/tmp/authpid.conf','r',-1)pid = ff.readline()cmd='kill -9 '+pidos.system(cmd) ////杀掉keep-alive进程 print 'logout ok' |
|