python 自动登录CJLU校园网

python 自动登录CJLU校园网

现在很多高校学生公寓上网采用校园网认证+运营商认证的方案,每次电脑开机都需要先连校园网,再连外网,很麻烦,所以我写了个自动联网的程序,简化联网步骤。

校园网登录界面

具体思路是先在本地电脑上进行抓包,捕捉浏览器登录流量,分析校园网认证过程。然后通过程序模拟浏览器行为,提交认证表单给服务器,完成认证。下面贴出源代码:
#!/usr/bin/env python
# coding=utf8
# 自动登录CJLU ,仅供在中国计量大学使用
# @date:20161019
# @author:arckalsun@gmail.com
# 
# 命令行用法:python login.py 学号 密码
# 可以写个批处理文件,写入命令,存为开机自启动脚本
# 此程序尚未完善,不足之处请大家原谅,欢迎大家优化
#
import urllib2,urllib
import sys
import socket
import os
import socket, fcntl, struct

class CJLULogin:
    # analyze OS windows/linux
    if (os.name == "nt"):#windows
    # get local ip (windows)
        localip = socket.gethostbyname(socket.gethostname())
    else:#linux or unix
    # get local ip (unix)
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        localip = socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', 'eth0'[:15]))[20:24])
    print localip


    # check network status
    def check(self):
        url = "http://my.cjlu.edu.cn"
        response = urllib2.urlopen(url)
        geturl = response.geturl()
        if url == geturl:
            print "connected"
            return 0
        else:
            print "not connected"
            return -1


    # login
    def login(self,username,password):
        url = "https://portal2.cjlu.edu.cn:801/eportal/?c=ACSetting&a=Login&wlanuserip="+self.localip+"&wlanacip=null&wlanacname=&port=&iTermType=1&mac=000000000000&ip="+self.localip+"&redirect=null"
        request = urllib2.Request(url)
        request.add_header('Host','portal2.cjlu.edu.cn:801')
        request.add_header('Connection','keep-alive')
        request.add_header('Cache-Control','max-age=0')
        request.add_header('Origin','https://portal2.cjlu.edu.cn')
        request.add_header('Upgrade-Insecure-Requests','1')
        request.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36')
        request.add_header('Content-Type','application/x-www-form-urlencoded')
        request.add_header('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8')
        request.add_header('Referer','https://portal2.cjlu.edu.cn/a70.htm?wlanuserip=172.28.1.228&wlanacname=&me60=ethtrunk/2:2775.653')
        request.add_header('Accept-Encoding','gzip, deflate, br')
        request.add_header('Accept-Language','zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4')
        request.add_header('Cookie','wlanacname=; wlanacip=null')

        data = r"DDDDD="+username+"&upass="+password+"&R1=0&R2=&R6=0&para=00&0MKKey=123456"

        response = urllib2.urlopen(request,data)
    #print response.read()
        code = response.getcode()
        print code
        # #print page
        # print "*" * 79
        # info = response.info()
        # print info

        # print "*" * 79
        # print url

    # logout   
    def logout(self):
        print " "
        url = "https://portal2.cjlu.edu.cn/a79.htm?wlanuserip="+self.localip+"&wlanacname=&me60=ethtrunk/2:2775.653"
        request = urllib2.Request(url)
        request.add_header("Connection","Keep-Alive")
        request.add_header("User-Agent", "Post")
        request.add_header("Cache-Control", "no-cache")
        request.add_header("Pragma", "no-cache")
        request.add_header("Host", "portal2.cjlu.edu.cn")
        response = urllib2.urlopen(request)
        html = response.read()
        print html
        print len(html)

if __name__ == '__main__':
    if len(sys.argv) < 3:
        #print "Usage: " + sys.argv([0] + " username, password"
        sys.exit("Usage: python " + sys.argv[0] + " username password")
    else:
        username = sys.argv[1]
        password = sys.argv[2]

    cjlu = CJLULogin()
    if cjlu.check() == -1:
        cjlu.login(username, password)

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页