In September this year, our school changed the campus network billing system, and made multiple networks (campus network, mobile broadband, campus Intranet) into a unified login entrance. The new billing method brings with it some unpleasant new features: automatic disconnections that can be handled irregularly.

There are a number of situations that can cause an automatic disconnection:

  1. No data access for a long time;
  2. Shutdown for a period of time to restart will be disconnected;
  3. Sometimes at night there will be a sudden disconnection;

Disconnection requires re-login, which is very unfriendly to a person with raspberry PI under the desk. In order to keep my Raspberry PI online, I plan to study the network connection mechanism of the school, find the interface for login authentication of the campus network, and periodically invoke the interface to keep the network online through scheduled tasks.

Find the interface for campus network login authentication

When connecting to the network after startup, any page opened will be automatically redirected to the network verification page with the following address:

http://10.**.**.155/eportal/index.jsp?wlanuserip=747782dfc4c71585c4738aba9ddfac74&wlanacname=4d9b42af64216a4c&ssid=&nasi p=33a23ff6f4079ffae1efa910adf8630a&snmpagentip=&mac=cea4ee74202c8e19054f5242fed8a793&t=wireless-v2&url=d93a492329ace846b 5542d1a14450dc01c67fe12a7c37ec6&apmac=&nasid=4d9b42af64216a4c&vid=80e2309e88a7305c&port=ec0e0d680caa36ef&nasportid=75961 153f3a76ba5b8d71721a48c6a36d27cfbc13a5acc9ea2aa9e8eb1cf5c88Copy the code

In order to realize automatic login, we first try to find the interface of campus network login authentication, through the browser developer tools page (press the keyboard F12 to enter), find the function call corresponding to the login button.

Then, in the source bar, search for the function’s call definition (if the js file is a full line, try using chrome’s built-in functionality to display the code pretty well).Then read the general code logic, find the Ajax-related code, and make a breakpoint where the callback function succeeds in authentication.

After that, enter your account password in the page and click login. Finally, the page will be stuck at the breakpoint. Check the Network bar to find the interface and data content of Auth.

To sum up, I got the network authentication interface and login parameters of my account.

Interface address: http://10.**.**.5/eportal/******.do? method=login
# method type: POST
# parameters:  userId=******&password=**********&service=%25E6%25A0%25A1%25E5%259B%25AD%25E5%25A4%2596%25E7%25BD%2591%25E6%259C%258D%2 5E5%258A%25A1&queryString=wlanuserip%253D44e4a1ba863c3912ce26bb6a08b619f4%2526wlanacname%253Dd6c254d78aab1b41f824e30727d 4a56a%2526ssid%253D%2526nasip%253D33a23ff6f4079ffae1efa910adf8630a%2526snmpagentip%253D%2526mac%253Dac90160b94f74a83bcc0 3f62577c799a%2526t%253Dwireless-v2%2526url%253D97bcca4b4607b74ecd5a8936b3fedd1caf8c58cf889fa299708ec8a1018a4fd68feb45fec 04791db%2526apmac%253D%2526nasid%253Dd6c254d78aab1b41f824e30727d4a56a%2526vid%253D661483fc15f609b3%2526port%253D49738f77 9d182dee%2526nasportid%253D75961153f3a76ba5cff6c0330a8d162cfde51ccd257f7b51f060e9d8c10167f0&operatorPwd=&operatorUserId= &validcode=&passwordEncrypt=false
Copy the code

I originally planned to continue analyzing the content of parameters, but found that most of the parameters were obtained from the login page parameters that automatically jumped, so I did not need to edit them by myself. After HTML decode, I think the meaning of parameters is as follows, and there are some encryption fields in it, so it is difficult to generate this parameter by myself:

UserId = * * * * * * * * * & password = * * * * * * * * * & service = off campus network service & the queryString = wlanuserip = 44 e4a1ba863c3912ce26bb6a08b619f4 &wlanacname=d6c254d78aab1b41f824e30727d4a56a &ssid= &nasip=33a23ff6f4079ffae1efa910adf8630a &snmpagentip= &mac=ac90160b94f74a83bcc03f62577c799a &t=wireless-v2 &url=97bcca4b4607b74ecd5a8936b3fedd1caf8c58cf889fa299708ec8a1018a4fd68feb45fec04791db &apmac= &nasid=d6c254d78aab1b41f824e30727d4a56a &vid=661483fc15f609b3 &port=49738f779d182dee &nasportid=75961153f3a76ba5cff6c0330a8d162cfde51ccd257f7b51f060e9d8c10167f0 &operatorPwd= &operatorUserId= &validcode= &passwordEncrypt=false
Copy the code

PS: Fortunately, the probability of entering a verification code on the campus network is very low, so I can use this parameter to log in to the campus network using the curl tool.

curl -d 'userId=********&password=*********&service=%25E6%25A0%25A1%25E5%259B%25AD%25E5%25A4%2596%25E7%25BD%2591%25E6%259C%258D% 25E5%258A%25A1&queryString=wlanuserip%253D44e4a1ba863c3912ce26bb6a08b619f4%2526wlanacname%253Dd6c254d78aab1b41f824e30727 d4a56a%2526ssid%253D%2526nasip%253D33a23ff6f4079ffae1efa910adf8630a%2526snmpagentip%253D%2526mac%253Dac90160b94f74a83bcc 03f62577c799a%2526t%253Dwireless-v2%2526url%253D97bcca4b4607b74ecd5a8936b3fedd1caf8c58cf889fa299708ec8a1018a4fd68feb45fe c04791db%2526apmac%253D%2526nasid%253Dd6c254d78aab1b41f824e30727d4a56a%2526vid%253D661483fc15f609b3%2526port%253D49738f7 79d182dee%2526nasportid%253D75961153f3a76ba5cff6c0330a8d162cfde51ccd257f7b51f060e9d8c10167f0&operatorPwd=&operatorUserId =&validcode=&passwordEncrypt=false'-X POST http://10.**.**.155/eportal/InterFace.do? method=loginCopy the code

Automatic login script

To implement automatic login, I create a script with the following logic:

  1. Check whether the network is connected.
    1. If the state is connected, end;
    2. If the status is disconnected, the system invokes the network authentication interface to ensure that it is online.

Check the network connection status

I originally planned to use curl to visit Baidu and check whether it is online according to the HTTP status code. In the actual test, it was found that the status code returned was 200 even if the line was disconnected, because the status code returned by the page automatically jumped from the campus network was 200.

I then visited Baidu while disconnected to see how it was different from being online.

<script>top.self.location.href='http://10.**.**.155/eportal/index.jsp? wlanuserip=747782dfc4c7158565aefbadb92f08f5&wlanacname=4d9b42af64216a4c&ssid=&nasip=33a23ff6f4079ffae1efa910adf8630a&snm pagentip=&mac=e928a1f5c5556f26bb004e5f1879e64b&t=wireless-v2&url=97bcca4b4607b74e02e80da18ea91792d9dd6dcc5d2aa5a3&apmac= &nasid=4d9b42af64216a4c&vid=80e2309e88a7305c&port=ec0e0d680caa36ef&nasportid=75961153f3a76ba5b8d71721a48c6a36d27cfbc13a5 acc9ea2aa9e8eb1cf5c88'</script>
Copy the code

Ah, this suddenly understand the principle of the campus network automatically jump to the login authentication page, hijacking all unauthenticated network access, return fixed HTML content, in the page to ask the browser to jump to the authentication page.

This is easy to do, save access to baidu return content, check whether there is content related to the login page, through comparison can know the current network is successfully connected, or disconnected state.

# Check network connection
function network()
{
    # timeout
    local timeout=1
    # Target site
    local target=www.baidu.com
    Get the response status code

    local content=`curl http://www.baidu.com | grep eportal/index.jsp`
    if [ ${#content} -lt 10 ]; then
        # Smooth network
        return 1
    else
        # The network is not smooth
        return 0
    fi
    return0}Copy the code

The final automatic login script

#! /bin/bash

LOG_FILE=/var/log/xxxx.log

# Check network connection
function network()
{
    # timeout
    local timeout=1
    # Target site
    local target=www.baidu.com
    Get the response status code

    local content=`curl http://www.baidu.com | grep eportal/index.jsp`
    if [ ${#content} -lt 10 ]; then
        # Smooth network
        return 1
    else
        # The network is not smooth
        return 0
    fi
    return0}# execute function
network

if [ $? -eq 0 ];then
    echo "Network is not smooth, please check the network Settings!"
    ret_value=$(curl -d "userId=****&password=****&service=%25E6%25A0%25A1%25E5%259B%25AD%25E5%25A4%2596%25E7%25BD%2591%25E6%259C%258D%25E5%258A %25A1&queryString=wlanuserip%253D747782dfc4c7158565aefbadb92f08f5%2526wlanacname%253D4d9b42af64216a4c%2526ssid%253D%2526 nasip%253D33a23ff6f4079ffae1efa910adf8630a%2526snmpagentip%253D%2526mac%253De928a1f5c5556f26bb004e5f1879e64b%2526t%253Dw ireless-v2%2526url%253Ddcc9f4c1c6174d15aa0451f13f9102fce3746944e780141b39c9ba28c4ed01fe%2526apmac%253D%2526nasid%253D4d9 b42af64216a4c%2526vid%253D80e2309e88a7305c%2526port%253Dec0e0d680caa36ef%2526nasportid%253D75961153f3a76ba5b8d71721a48c6 a36d27cfbc13a5acc9ea2aa9e8eb1cf5c88&operatorPwd=&operatorUserId=&validcode=&passwordEncrypt=false"-X POST http://10.**.**.155/eportal/InterFace.do? method=login)echo ${ret_value}
    date >> ${LOG_FILE}
    echo "try to login!" >> ${LOG_FILE}
    echo "login result: ${ret_value}" >> ${LOG_FILE}
    exit- 1fi
    echo "The Internet is clear and you can surf the Internet!"
exit 0
Copy the code

Regularly perform

Automatic login script has been written, how to make the script timing execution? Using the Crontab feature of Linux, this tool can set up a timer to execute tasks as often as the user wants. But the default editing tool was very difficult to use (vi), so I changed the default editing tool first. Run the select-editor command to select a convenient editing tool, and then crontab -e to configure scheduled tasks.

# auto connect
*/5 * * * * /home/pi/auto-connect.sh

# auto clean log
0 0 1 * * /home/pi/clean_log.sh
Copy the code

conclusion

Through the above three processes, I solved the problem of connecting my raspberry PI to the campus network. Firstly, I studied the login authentication mode of campus network and found the interface using curl tool for login authentication. Then I write a script code that automatically checks the connection status and invokes the login authentication interface. I ended up setting up a timer to automatically call the script every five minutes to keep my raspberry PI connected.

Reference

  1. The curl usage
  2. The use of the crontab