bwifi/bwifi.sh

76 lines
2.9 KiB
Bash
Raw Normal View History

2022-10-03 05:19:38 +00:00
#!/usr/bin/env bash
2022-12-02 03:37:38 +00:00
# Change the following variable to your username and password for school network authorization.
2022-10-03 05:19:38 +00:00
username=1970111222
password=123123321
2022-12-02 03:37:38 +00:00
# Get authorizing location from 302 response header.
# Since sometimes the school network don't hijack DNS lookup query, and it may take such a long time
# during DNS lookup. So we use an IP address to bypass the DNS lookup. Any fixed IP address providing
# HTTP service can take place of the "119.29.29.29".
2022-10-03 05:19:38 +00:00
echo 'sending a http request...'
res_headers=$(curl --request GET --head --no-progress-meter http://119.29.29.29/)
echo 'response received'
2022-12-02 03:37:38 +00:00
# If authorizing is requested, school network will respond any HTTP (not HTTPS) request with an 302
# response and its "Server" field is "DrcomServer"
2022-10-03 05:19:38 +00:00
if ! grep "Server: DrcomServer" <<< $res_headers ; then
2022-12-02 03:37:38 +00:00
echo "didn't receive a response from DrcomServer, you may already be online."
2022-10-03 05:19:38 +00:00
exit 1
fi
auth_host=$(sed -n -E 's/Location: (.*)/\1/p' <<< $res_headers | tr -d '[:space:]')
auth_host="${auth_host}/a70.htm"
echo auth host: $auth_host
# known auth host: 10.1.206.13
# - 10.1.206.13
# - 192.168.211.3
# for wireless, which will be redirected to this location, a "@bistu" is required,
if [ "$auth_host" == *"10.1.206.13"* ] ; then
username="${username}@bistu"
declare -a cookie_opt
declare -a header_opt
cookie_opt+=("--cookie" "program=beijing-information")
cookie_opt+=("--cookie" "md5_login2=$username|$password")
header_opt+=("--header" "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8")
header_opt+=("--header" "Accept-Language: en-US,zh-CN;q=0.5")
header_opt+=("--header" "Accept-Encoding: gzip, deflate")
header_opt+=("--header" "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/110.0")
2022-10-03 05:19:38 +00:00
fi
2022-12-02 03:42:18 +00:00
# Note that the encoding of response is gb2312, so we need to transcode
# before comparing the title to judge whether we succeed to login.
2022-10-03 05:19:38 +00:00
title=$(curl --no-progress-meter --include \
--data-urlencode "DDDDD=$username" \
--data-urlencode "upass=$password" \
--data-urlencode "R1=0" \
--data-urlencode "R2=" \
--data-urlencode "R3=0" \
--data-urlencode "R6=0" \
--data-urlencode "para=00" \
--data-urlencode "0MKKey=123456" \
--data-urlencode "buttonClicked=" \
--data-urlencode "redirect_url=" \
--data-urlencode "err_flag=" \
--data-urlencode "username=" \
--data-urlencode "password=" \
--data-urlencode "user=" \
--data-urlencode "cmd=" \
--data-urlencode "Login=" \
"${cookie_opt[@]}" \
"${header_opt[@]}" \
2022-10-03 05:19:38 +00:00
$auth_host | iconv --from-code=gb2312 --to-code=utf-8 | sed -n -E 's#<title>(.*)</title>#\1#p')
echo auth result page title: $title
if [ "$title" == "认证成功页" ] ; then
echo "auth result: success"
else
echo "auth result: failed"
fi