2014-09-20 17:19:56 +08:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
|
|
# wlan.sh -- login/logout for ZJUWLAN
|
2014-09-20 15:39:28 +08:00
|
|
|
#
|
2014-10-02 13:25:28 +08:00
|
|
|
# Requirements: curl
|
2014-09-20 18:20:13 +08:00
|
|
|
#
|
2014-09-20 15:39:28 +08:00
|
|
|
# Copyright (C) 2014 Zhang Hai <Dreaming.in.Code.ZH@Gmail.com>
|
|
|
|
|
# Copyright (C) 2014 Zeno Zeng <zenoofzeng@gmail.com>
|
2017-03-24 21:49:17 +08:00
|
|
|
# Copyright (C) 2017 Wu Fan <wfwf1997@gmail.com>
|
2014-09-20 15:39:28 +08:00
|
|
|
#
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful, but
|
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
# General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program. If not, see
|
|
|
|
|
# <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
2014-09-28 21:39:15 +08:00
|
|
|
BASEDIR=$(dirname $0)
|
|
|
|
|
|
2019-08-30 23:18:52 +08:00
|
|
|
DISABLE_CHECK=${BASEDIR}/.disable-wlan
|
|
|
|
|
|
2014-09-28 21:39:15 +08:00
|
|
|
USER="${BASEDIR}/user.sh"
|
|
|
|
|
|
|
|
|
|
USERNAME=$($USER get)
|
|
|
|
|
PASSWORD=$($USER getpwd $USERNAME)
|
|
|
|
|
|
2019-08-30 23:18:52 +08:00
|
|
|
if [[ -f "${DISABLE_CHECK}" ]]; then
|
|
|
|
|
echo WLAN function has been disabled.;
|
|
|
|
|
exit 1;
|
|
|
|
|
fi
|
|
|
|
|
|
2014-09-20 15:39:28 +08:00
|
|
|
logout() {
|
|
|
|
|
USERNAME=$1
|
|
|
|
|
PASSWORD=$2
|
|
|
|
|
|
|
|
|
|
echo "Logout: ${USERNAME}"
|
2017-03-24 21:49:17 +08:00
|
|
|
RESPONSE=$(curl "https://net.zju.edu.cn/include/auth_action.php" -H "Content-Type: application/x-www-form-urlencoded" -d "action=logout&username=${USERNAME}&password=${PASSWORD}&ajax=1" -s)
|
2014-09-20 18:13:40 +08:00
|
|
|
|
2017-03-24 21:49:17 +08:00
|
|
|
if [[ $? -eq 60 ]]; then
|
|
|
|
|
CHOOSE=''
|
|
|
|
|
read -p "There's an issue with ZJUNET's CA certificates, do you want to do it anyway? [yes/NO] " CHOOSE
|
|
|
|
|
if [[ $CHOOSE == "YES" ]] || [[ $CHOOSE == "yes" ]]; then
|
|
|
|
|
RESPONSE=$(curl "https://net.zju.edu.cn/include/auth_action.php" -H "Content-Type: application/x-www-form-urlencoded" -d "action=logout&username=${USERNAME}&password=${PASSWORD}&ajax=1" -s -k)
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
|
2014-09-20 18:13:40 +08:00
|
|
|
case "${RESPONSE}" in
|
|
|
|
|
*ok*)
|
|
|
|
|
echo "Logout: success."
|
2014-09-23 16:31:12 +08:00
|
|
|
;;
|
2017-03-24 21:49:17 +08:00
|
|
|
*已断开*)
|
2014-09-23 16:31:12 +08:00
|
|
|
echo "Logout: success."
|
2014-09-20 18:13:40 +08:00
|
|
|
;;
|
|
|
|
|
*)
|
2017-03-24 21:49:17 +08:00
|
|
|
echo "Logout: unsuccess."
|
|
|
|
|
echo "${RESPONSE}"
|
2014-09-20 18:13:40 +08:00
|
|
|
exit 1;
|
|
|
|
|
;;
|
|
|
|
|
esac
|
2014-09-20 15:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
login() {
|
|
|
|
|
USERNAME=$1
|
|
|
|
|
PASSWORD=$2
|
|
|
|
|
|
2017-03-24 21:49:17 +08:00
|
|
|
STATUS=$(curl http://g.cn/generate_204 -I -s | grep HTTP | awk {'print $2'})
|
|
|
|
|
if [[ STATUS -eq 204 ]]; then
|
|
|
|
|
echo "You have already logged in."
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
2014-09-20 15:39:28 +08:00
|
|
|
|
|
|
|
|
echo "Login: ${USERNAME}"
|
2017-03-24 21:49:17 +08:00
|
|
|
RESPONSE=$(curl "https://net.zju.edu.cn/include/auth_action.php" -H "Content-Type: application/x-www-form-urlencoded" -d "action=login&username=${USERNAME}&password=${PASSWORD}&ac_id=3&user_ip=&nas_ip=&user_mac=&save_me=1&ajax=1" -s)
|
|
|
|
|
|
|
|
|
|
if [[ $? -eq 60 ]]; then
|
|
|
|
|
CHOOSE=''
|
|
|
|
|
read -p "There's an issue with ZJUNET's CA certificates, do you want to do it anyway? [yes/NO] " CHOOSE
|
|
|
|
|
if [[ $CHOOSE == "YES" ]] || [[ $CHOOSE == "yes" ]]; then
|
|
|
|
|
RESPONSE=$(curl "https://net.zju.edu.cn/include/auth_action.php" -H "Content-Type: application/x-www-form-urlencoded" -d "action=login&username=${USERNAME}&password=${PASSWORD}&ac_id=3&user_ip=&nas_ip=&user_mac=&save_me=1&ajax=1" -s -k)
|
|
|
|
|
fi
|
|
|
|
|
fi
|
2014-09-20 18:13:40 +08:00
|
|
|
|
|
|
|
|
case "${RESPONSE}" in
|
|
|
|
|
*help.html*)
|
|
|
|
|
echo "Login: success."
|
|
|
|
|
;;
|
|
|
|
|
*login_ok*)
|
|
|
|
|
echo "Login: success."
|
|
|
|
|
;;
|
2017-03-24 21:49:17 +08:00
|
|
|
*E2532*)
|
|
|
|
|
echo "Login: failed. Please retry after 10s." >&2
|
|
|
|
|
echo "Login: ${RESPONSE}" >&2
|
|
|
|
|
exit 1
|
|
|
|
|
;;
|
2014-09-20 18:13:40 +08:00
|
|
|
*)
|
2017-03-24 21:49:17 +08:00
|
|
|
echo "Login: failed." >&2
|
2019-08-30 23:18:52 +08:00
|
|
|
if [[ -z "${RESPONSE}" ]]; then
|
|
|
|
|
echo "Login: (Empty response)" >&2
|
|
|
|
|
else
|
|
|
|
|
echo "Login: ${RESPONSE}" >&2
|
|
|
|
|
fi
|
2014-09-20 18:13:40 +08:00
|
|
|
exit 1
|
|
|
|
|
;;
|
|
|
|
|
esac
|
2014-09-20 15:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
2014-09-20 17:19:56 +08:00
|
|
|
|
2014-09-19 23:36:35 +08:00
|
|
|
case "$1" in
|
2019-08-30 23:18:52 +08:00
|
|
|
d|-d|disconnect)
|
2014-09-28 21:39:15 +08:00
|
|
|
logout $USERNAME $PASSWORD
|
2014-09-19 23:36:35 +08:00
|
|
|
;;
|
2019-08-30 23:18:52 +08:00
|
|
|
|
|
|
|
|
""|c|-c|connect)
|
|
|
|
|
login $USERNAME $PASSWORD
|
2014-09-19 23:36:35 +08:00
|
|
|
;;
|
2019-08-30 23:18:52 +08:00
|
|
|
|
|
|
|
|
disable)
|
|
|
|
|
"${BASEDIR}/sudo.sh" touch "${DISABLE_CHECK}"
|
|
|
|
|
exit 0
|
|
|
|
|
;;
|
|
|
|
|
|
2014-09-20 18:13:40 +08:00
|
|
|
*)
|
2019-08-30 23:18:52 +08:00
|
|
|
echo Invalid subcommand \"$1\" for \`zjunet wlan\`. Run \`zjunet usage\` for help.
|
2014-09-20 18:13:40 +08:00
|
|
|
;;
|
2014-09-19 23:36:35 +08:00
|
|
|
esac
|
2014-10-15 16:35:07 +08:00
|
|
|
|
|
|
|
|
"${BASEDIR}/sudo.sh" "${BASEDIR}/route.sh"
|
|
|
|
|
|