zoukankan      html  css  js  c++  java
  • 东大校园网一键登录

      东大校园网很不好用,我在宿舍连着网。当我出去自习的时候,也想用网,就连不上。
           因为东大校园网只允许一个人使用,这使得想合伙共用一个账号的人就没法整了。学校这么做多半是为了多挣点钱,这对于一个月只用2,3G的人是一种剥削。既然制度不对,为什么不改呢?
           于是只能先断开一下,在重新登陆校园网了。可是坑爹的问题又出现了,有三个按钮:连接,断开连接,断开全部连接。
            我点了“断开网络”,然后再“连接网络”,它还告诉我:当前连接数大于一。
            实际上,只有一个管用:应该点“断开全部连接”,再点“连接网络”。
            那么就得分析一下为啥会有这种逗比的情况:编这个页面的人多半是同情贫苦大众的,他以为学校肯定会开明到允许多个学生共用一个账号(然而他高估了学校),这样一来,中间那个按钮就起作用了,那个按钮就是为多个用户共用一个账号而生的。但是最终那个按钮没能派上用场,反倒成了累赘,总是误导人,不信可以统计一下,有多少人在一分钟之内先点了“断开连接”,然后又点了“断开全部连接”,这就说明又多少次点中间按钮是不管用的,是不符合人类需求的。学校既然狠心一人一个账号来剥削群众,为啥就不删除哪一个按钮呢?
            连上了网,一下子还开了一个小窗口,为啥不把这个窗口所显示的信息跟页面合并呢。这就使得我被迫多移动一次鼠标去关它。
            实际上,这个功能是后来添加的。http://jifei.neu.edu.cn/stats/dashboard?sid=12815848134566654这个链接里面的参数sid是随机生成的,发送到服务器上,服务器完成查询之后,将流量信息保存到一个hash表中,键是这个随机数,值是流量使用情况。所以这个是实现是有可能出bug的,就是可能撞车,张三和李四同时登陆校园网,可能张三看到的流量信息是李四的。
    如果用linux登陆校园网,可以用下面这个脚本,把用户名和密码改一下就可以了。
    #!/bin/bash
    #use 'ping' to check whether you are connected to NEU
    ping -c2 'www.neu.edu.cn' || { echo "You should connect to NEU firstly" ;exit 0 ; }
    user='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    password='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    temp=`mktemp`
    #disconnect all the connections
    curl -d "uid=$user&password=$password&range=2&timeout=1&operation=disconnectall" "http://ipgw.neu.edu.cn/ipgw/ipgw.ipgw" -o $temp 
    iconv -f "gb2312" -t "utf-8"  $temp  -o $temp
    cat $temp | grep '<td>.*</td>'
    #if ask connect,connect it
    if test $# -eq 0;then
        curl -d "uid=$user&password=$password&range=2&timeout=1&operation=connect" "http://ipgw.neu.edu.cn/ipgw/ipgw.ipgw" -o $temp
        iconv -f "gb2312" -t "utf-8"  $temp  -o $temp
        cat $temp | grep '<td>.*</td>'
    fi
    #output how much energy did you use .
    curl -d "uid=$user&password=$password" "http://jifei.neu.edu.cn/stats/dashboard?sid=0" -o $temp 
    curl "http://jifei.neu.edu.cn/stats/dashboard?sid=0" -o $temp  
    res=`cat $temp | grep '<li>.*MB</li>'`
    res=${res//[<li>,</li>]/}
    echo "$res"


    还可以用一个网页打开,原理是用js向服务器post表单,自动登陆。
    <html>
    <script>
        var name='stu_20124003';
        var password="xxxxxxxxxxxxxxxxxxxxxxxxxxx";
        var user="uid="+name+"&password="+password;
        var data=user+"&range=2&timeout=1&";
        var x="http://ipgw.neu.edu.cn/ipgw/ipgw.ipgw";
        var y= "http://jifei.neu.edu.cn/stats/dashboard?sid=0";
        function post(url,data){
            var q=new XMLHttpRequest();
            q.open("post",url);
            q.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            q.send(data);
        } 
           post(x,encodeURI(data+"operation=disconnectall"));
           post(x,encodeURI(data+"operation=connect"));
           post(y,encodeURI(user));
        location=y;
    </script>
    </ht

    ===============java版==================

    import java.io.OutputStream;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.Scanner;

    public class URLConnectionPost {
     static void post(String url, String data) {
      try {
       URLConnection connection = new URL(url).openConnection();
       connection.setDoOutput(true);
       connection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
       OutputStream cout = connection.getOutputStream();
       cout.write(data.getBytes());
       cout.close();
       connection.getInputStream();
      } catch (Exception e) {
       e.printStackTrace();
      }
     }

     public static void main(String[] args) {
      String name = "stu_20124003", password = "xxxxxxxxxxxxx";
      String user = "uid=" + name + "&password=" + password, data = user + "&range=2&timeout=1&";
      String x = "http://ipgw.neu.edu.cn/ipgw/ipgw.ipgw", y = "http://jifei.neu.edu.cn/stats/dashboard?sid=0";
      post(x, data + "operation=disconnectall");
      post(x, data + "operation=connect");
      post(y, user);
      try {
       Scanner cin = new Scanner(new URL(y).openConnection().getInputStream());
       while(cin.hasNext()){
        System.out.println(cin.nextLine());
       }
       cin.close();
      } catch (Exception e) {
       e.printStackTrace();
      }
     }
    }


    在连接校园网之前程序会点击“断开全部连接”,然后再点击“连接”。所以只要连着校园网,一旦打开这个网页就肯定登陆了。
    ======================
    4.20日更新
    现在,东大校园网改版了,确实是变好了不少。首先,那个显示流量的弹框没了,变成了ajax请求;其次,手机端变得漂亮多了.
    java大法好
    public class Main {
        static String tos(InputStream cin) {
            Scanner in = new Scanner(cin);
            String ans = "";
            while (in.hasNext()) {
                ans += in.nextLine() + "
    ";
            }
            in.close();
            return ans;
        }
        static InputStream post(String url, String data) {
            try {
                URLConnection connection = new URL(url).openConnection();
                connection.setDoOutput(true);
                connection.setRequestProperty("content-type",
                        "application/x-www-form-urlencoded");
                OutputStream cout = connection.getOutputStream();
                cout.write(data.getBytes());
                cout.close();
                return connection.getInputStream();
            } catch (Exception e) {
                System.out.println("没连校园网");
                // e.printStackTrace();
            }
            return null;
        }
    
        public static void main(String[] args) {
            String name = "stu_20124003", password = "xxxxxxxxx";
            String user = "username=" + name + "&password=" + password;
            String base = "http://ipgw.neu.edu.cn:803/";
            String logout = base + "include/auth_action.php";
            String login = base + "srun_portal_pc.php?ac_id=1&";
            post(logout, user + "&action=logout&ajax=1");
            post(login, user + "&action=login&ac_id=1");
            InputStream cin = post(logout, "action=get_online_info");
            String s[] = tos(cin).split(",");
            System.out.println("已用流量:"
                    + new DecimalFormat("#,###").format(Long.parseLong(s[0])));
            long time = Long.parseLong(s[1]);
            System.out.println("登录时间:" + String.format("%02d:%02d:%02d", time / 3600,
                    (time / 60) % 60, time % 60));
            System.out.println("账户余额:" + s[2]);
            System.out.println("IP地址:" + s[5]);
        }
    }

     顺道学学python

    from urllib.request import Request, urlopen
    def post(url, data, doCallback):
        req = Request(url)
        fd = urlopen(req, data.encode(encoding='utf_8'))
        if(doCallback):
            return fd.read().decode("utf_8")
    name = "stu_20124003"
    password = "xxxxxxxx" 
    user = "username=" + name + "&password=" + password
    base = "http://ipgw.neu.edu.cn:803/";
    logout = base + "include/auth_action.php";
    login = base + "srun_portal_pc.php?ac_id=1&";
    post(logout, user + "&action=logout&ajax=1", False)
    post(login, user + "&action=login&ac_id=1", False);
    ans = post(logout, "action=get_online_info", True)
    value = ans.split(sep=',')
    def formatFlux(s):
        ans = ""
        danwei = "B,K,M,G".split(",")
        i=len(danwei)-1
        while i>=0:
            if i*3<=len(s):
                ans+=s[max(len(s)-i*3-3,0):len(s)-i*3]+danwei[i]+' '
            i=i-1
        return ans
    print("已用流量:" + formatFlux(value[0]))
    t = int(value[1])
    print("登录时间:%02d:%02d:%02d" % (t / 3600, (t / 60) % 60, t % 60))
    print ("账户余额:" + value[2])
    print("IP地址:" + value[5])

     javascript也好用,不过是setHeader几下罢了。

    <html>
    <h1 id='haha'></h1>
    <script>
        var name = 'stu_20124003';
        var password = "xxxxxxxxx";
        var user = "username=" + name + "&password=" + password;
        function post(url, data, doCallback) {
            var q = new XMLHttpRequest();
            q.open("post", url);
            q.setRequestHeader('Referer', 'http://ipgw.neu.edu.cn:803');
            q.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
     q.setRequestHeader("Origin","http://ipgw.neu.edu.cn:804");
    if (doCallback) { q.onreadystatechange = function(q) { if (q.readyState == 4 && q.status == 200) { document.getElementById('haha').innerHtml = q.responseText; } }; } q.send(data); } var base = "http://ipgw.neu.edu.cn:803/"; var logout = base + "include/auth_action.php"; var login = base + "srun_portal_pc.php?ac_id=1&"; post(logout, user + "&action=logout&ajax=1", false); post(login, user + "&action=login&ac_id=1", false); post(logout, "action=get_online_info", true); </script> </html>

     林教主发现,断开连接时还需要post用户名,无需post正确的密码,后台没有对密码进行检测是否正确.于是,林教主把全校人的校园网断开了一遍.

    现在我来北航了,北航校园网1G只要5毛钱,允许多人同时在线,节假日免费,每月初赠送1G,登录页面还算漂亮.总而言之,北航校园网比东北大学建设的好多了.

    public class Main {
        static boolean post(String url, String data) throws IOException {
            URLConnection connection = new URL(url).openConnection();
            connection.setDoOutput(true);
            OutputStream cout = connection.getOutputStream();
            cout.write(data.getBytes());
            cout.close();
            StringBuilder builder = new StringBuilder();
            Scanner cin = new Scanner(connection.getInputStream());
            while (cin.hasNext()) {
                builder.append(cin.nextLine());
            }
            cin.close();
            return builder.toString().contains("login_ok");
        }
        public static void main(String[] args) throws IOException {
            String username = "xxxxx", password = "xxxxx";
            while (false == post(
                    "https://gw.buaa.edu.cn:802/include/auth_action.php",
                    "action=login&username=" + username + "&password={B}"
                            + Base64.getEncoder()
                                    .encodeToString(password.getBytes())
                            + "&save_me=1&ajax=1&ac_id=4"));
        }
    }

    用python的requests库实现起来更简单

    import requests
    import base64
    
    username = "xxxx"
    password = "xxxx"
    
    def go(action="login"):
        resp = requests.post("https://gw.buaa.edu.cn:802/include/auth_action.php", data={
            "action": action,
            "username": username,
            "password": "{B}" + base64.encodebytes(password.encode("utf8")).decode("utf8"),
            "save_me": 0,
            "ajax": 1,
            "ac_id": 22,
        })
        resp.encoding = "utf8"
        print(resp.text)
    
    go("login")

    手机端登录时,需要打开浏览器输入用户名密码,十分麻烦,所以编了一个android app,方便一键登录.本程序能自动打开wifi,链接校园网,然后点一下登录并退出就可以了.把用户名和密码存储在SharedPreference里面,因为android经验很少,特此记录.

    Main.java:本程序唯一的Activity

    public class Main extends Activity implements View.OnClickListener {
        Button loginButton, exitButton;
        EditText username, password;
        TextView info;
        Handler handler = new Handler();
        SharedPreferences sharedPreferences;
        Semaphore semaphore = new Semaphore(1);
        Runnable runnable = new Runnable() {
            long lastLoginTime = 0;
    
            @Override
            public void run() {
                try {
                    semaphore.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (System.currentTimeMillis() - lastLoginTime < 3000) {
                    return;
                } else {
                    lastLoginTime = System.currentTimeMillis();
                    login(username.getText().toString(), password.getText().toString());
                }
                semaphore.release();
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            loginButton = (Button) findViewById(R.id.login);
            exitButton = (Button) findViewById(R.id.exit);
            username = (EditText) findViewById(R.id.username);
            password = (EditText) findViewById(R.id.password);
            info = (TextView) findViewById(R.id.info);
            loginButton.setOnClickListener(this);
            exitButton.setOnClickListener(this);
            init();
            username.setText(sharedPreferences.getString("username", ""));
            password.setText(sharedPreferences.getString("password", ""));
        }
    
        void init() {
            WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
            wifiManager.setWifiEnabled(true);
            WifiConfiguration config = new WifiConfiguration();
            config.SSID = ""BUAA-WiFi"";
            int netId = wifiManager.addNetwork(config);
            wifiManager.enableNetwork(netId, true);
            sharedPreferences = this.getSharedPreferences("wifi", 0);
        }
    
        @Override
        public void onClick(View v) {
            if (v == exitButton) {
                finish();
                System.exit(0);
            }
            if (username.getText().length() == 0 || password.getText().length() == 0) {
                Toast.makeText(this, "please input username&password", Toast.LENGTH_LONG).show();
                return;
            }
            if (semaphore.availablePermits() == 1) {
                new Thread(runnable).start();
            }
        }
    
        boolean post(String url, String data) throws IOException {
            URLConnection connection = new URL(url).openConnection();
            connection.setDoOutput(true);
            OutputStream cout = connection.getOutputStream();
            cout.write(data.getBytes());
            cout.close();
            StringBuilder builder = new StringBuilder();
            Scanner cin = new Scanner(connection.getInputStream());
            while (cin.hasNext()) {
                builder.append(cin.nextLine());
            }
            cin.close();
            return builder.toString().contains("login_ok");
        }
    
        void login(String username, String password) {
            try {
                int cnt = 2;
                while (cnt-- > 0 && !post(
                        "https://gw.buaa.edu.cn:802/include/auth_action.php",
                        "action=login&username=" + username + "&password={B}"
                                + Base64.encodeToString(password.getBytes(), Base64.DEFAULT)
                                + "&save_me=1&ajax=1&ac_id=4"))
                    Thread.sleep(1000);
                if (cnt > 0) {
                    sharedPreferences.edit().putString("username", Main.this.username.getText().toString()).putString("password", Main.this.password.getText().toString()).commit();
                    exitButton.callOnClick();
                } else {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(Main.this, "login failed", Toast.LENGTH_LONG).show();
                        }
                    });
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    本程序布局文件:唯一的layout

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <EditText
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="username" />
    
        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="password"
            android:inputType="textPassword" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <Button
                android:id="@+id/exit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="exit" />
    
            <Button
                android:id="@+id/login"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="login&amp;exit" />
    
        </LinearLayout>
    
        <TextView
            android:id="@+id/info"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

     用网络请求的方式登录校园网有时无法成功,这是可以模拟浏览器,这样登录成功率百分之百,但是运行速度有些慢.下面代码用到HtmlUnit库.创建maven项目并添加HtmlUnit的依赖即可.

    public class App {
        public static void main(String[] args) throws Exception {
            WebClient client = new WebClient(BrowserVersion.CHROME);
            HtmlPage page = client.getPage(
                    "https://gw.buaa.edu.cn:802/beihanglogin.php?ac_id=22&url=http://gw.buaa.edu.cn:802/beihangview.php");
            HtmlTextInput loginname = (HtmlTextInput) page.getElementById("loginname");
            HtmlPasswordInput password = (HtmlPasswordInput) page.getElementById("password");
            HtmlSubmitInput button = (HtmlSubmitInput) page.getElementById("button");
            loginname.setValueAttribute("xxxxx");
            password.setValueAttribute("xxxxx");
            button.click();  
            client.close();
        }
    }

    带配置文件的登录,第一次登录时会提示输入用户名密码并保存到家目录下。

    import base64
    
    import requests
    import warnings
    import time
    import os
    import json
    warnings.filterwarnings("ignore")
    
    
    def login(username, password):
        print(username,password)
        resp = requests.post("https://gw.buaa.edu.cn:803/include/auth_action.php",
                             data={
                                 "action": "login",
                                 "username": username.lower(),
                                 "password": "{B}" + base64.encodebytes(password.encode("utf8")).decode("utf8"),
                                 "save_me": 0,
                                 "ajax": 1,
                                 "ac_id": 22
                             }, verify=0)
        resp.encoding = "utf8" 
        if "login_ok" in resp.text:
            return True
        else:
            return False
    
    
    filename = os.path.join(os.path.expanduser("~"), '.school-net.json')
    if os.path.exists(filename):
        config = json.load(open(filename,encoding='utf8'))
        resp = login(config['username'], config['password'])
    else:
        while True:
            username = input("please input username : ").strip()
            if not username:
                continue
            password = input("please input password : ").strip()
            if not password:
                continue 
            if login(username, password):
                break
            else:
                print("登录失败,可能是用户名密码错误")
        config = dict(username=username, password=password)
        json.dump(config, open(filename,mode="w"))
        resp = True
    
    if resp:
        print("login ok")
    else:
        print("login failed")
    time.sleep(2)
    

      

  • 相关阅读:
    Visual Studio 2005 Starter Kits
    怎样去做才是朋友?
    C#读写日志文本文件
    [文摘20080707]马云追加投资20亿 淘宝首定10年超沃尔玛目标
    [转]在WinForm应用程序中实现自动升级
    [转]官方Flash CS3简体中文帮助文档下载,AS3.0简体中文帮助文档下载
    [引]MySQL INNODB类型表的外键关联设置
    [转]winfrom让弹出的MessageBox在指定时间内自动销毁
    生活开心一笑 之 "我家半"与"QQ病毒"
    [English20080721]疯狂英语365句
  • 原文地址:https://www.cnblogs.com/weiyinfu/p/5124106.html
Copyright © 2011-2022 走看看