├── README.md ├── auto-whu.conf ├── auto-whu.conf.sample ├── auto-whu.service ├── auto-whu.sh └── auto-whu.timer /README.md: -------------------------------------------------------------------------------- 1 | # auto-whu-standard 2 | Auto WHU for standard linux distribution i.e. Arch Linux, Ubuntu, etc. With systemd in mind, this version is much more concise than [the openwrt version](https://github.com/7Ji/auto-whu-openwrt). 3 | 为标准Linux发行版(如Arch Linux,Ubuntu,Debian等)准备的武大校园网自动登录管理程序,依托于systemd进行后台管理和自动启动以及日志记录,如果计划于在OpenWrt上使用,请使用依赖于SysVinit、和procd等的[OpenWrt版本](https://github.com/7Ji/auto-whu-openwrt) 4 | 5 | 如何使用? 6 | -- 7 | ### 方法一:使用systemd进行后台管理,设置开机登录,并定时进行在线认证 8 | 0、确保你使用的发行版的init为systemd,你可以用以下命令确定 9 | ```` 10 | systemctl --version 11 | ```` 12 | 1、确保curl、sed、grep和bash已安装且grep支持-oP参数,curl支持-d参数,(部分系统可能只内置了不支持这些参数的busybox版,如果发现不支持,那么你应当安装对应的完整版)你可以用以下命令确定 13 | ```` 14 | curl --help 15 | grep --help 16 | sed --version 17 | bash --version 18 | ```` 19 | 2、下载auto-whu.sh并将之放置到/usr/sbin/下,并为之增加执行权限,(亦可以手动复制文本并创建此文件) 20 | ```` 21 | wget https://raw.githubusercontent.com/7Ji/auto-whu-standard/main/auto-whu.sh -O /usr/sbin/auto-whu.sh 22 | chmod +x /usr/sbin/auto-whu.sh 23 | ```` 24 | 3、下载auto-whu.service与auto-whu.timer,将之放到/etc/systemd/system/下(亦可以手动复制文本并创建此文件) 25 | ```` 26 | wget https://raw.githubusercontent.com/7Ji/auto-whu-standard/main/auto-whu.service -O /etc/systemd/system/auto-whu.service 27 | wget https://raw.githubusercontent.com/7Ji/auto-whu-standard/main/auto-whu.timer -O /etc/systemd/system/auto-whu.timer 28 | ```` 29 | 4、下载auto-whu.conf,将之放到/etc/下(亦可以手动复制文本并创建此文件): 30 | ```` 31 | wget https://raw.githubusercontent.com/7Ji/auto-whu-standard/main/auto-whu.conf -O /etc/auto-whu.conf 32 | ```` 33 | 5、参照auto-whu.conf.sample,对auto-whu.conf进行修改 34 | ```` 35 | vi /etc/auto-whu.conf 36 | ```` 37 | 6、若要设置开机自动验证,并定时检测在线情况,请启动auto-whu.timer的开机启动,你可以修改auto-whu.timer中的``OnUnitActiveSec=1min``项来修改检测间隔。如果不需要定时检测,则应改为启动auto-whu.service。 38 | ```` 39 | systemctl enable --now auto-whu.timer 40 | 或 41 | systemctl enable --now auto-whu.service 42 | ```` 43 | **你不可以同时启用auto-whu.timer和auto-whu.service** 44 | ### 方法二:手动调用auto-whu.sh来进行网络认证 45 | 46 | *除非你计划将auto-whu.sh放置入你自己的开机脚本中,或是每次进行手动验证,或是使用crontab进行定时管理,否则你不应该手动调用此脚本。如果你的Linux发行版使用的init程序为systemd,你应当使用方法一,而不是启用crontab再手动将auto-whu.sh添加到crontab中* 47 | 48 | 0、确保curl、sed、grep和bash已安装且grep支持-oP参数,curl支持-d参数,(部分系统可能只内置了不支持这些参数的busybox版,如果发现不支持,那么你应当安装对应的完整版)你可以用以下命令确定 49 | ```` 50 | curl --help 51 | grep --help 52 | sed --version 53 | bash --version 54 | ```` 55 | 1、下载auto-whu.sh并将之放置到/usr/sbin/下,并为之增加执行权限,(亦可以手动复制文本并创建此文件) 56 | ```` 57 | wget https://raw.githubusercontent.com/7Ji/auto-whu-standard/main/auto-whu.sh -O /usr/sbin/auto-whu.sh 58 | chmod +x /usr/sbin/auto-whu.sh 59 | ```` 60 | 2、使用命令调用auto-whu,你可以使用-h参数来查看帮助信息,auto-whu接受传入以下参数: 61 | ``-u [username]`` 声明登录用户名,应为13位数字 62 | ``-p [password]`` 声明密码,不应为空字段 63 | ``-n [network]`` 声明登陆网络类型,0-3的整数,0为教育网(默认),1为电信,2为联通,3为移动 64 | ``-m [network_manual]`` 手动声明网络名称,会覆盖``-n``参数,例如教育网在此处为``-m Internet``,除非后期网络情况有变,或你计划把auto-whu使用在非武大校园网的环境中,否则不应该使用此参数 65 | ``-c [config file]`` 配置文件路径,将会从中读取用户名、密码、网络类型、手动网络名称、验证URL、是否检测systemd、各变量合法性等,这些选项将会被命令行提供的参数覆盖(例如,``-u``会覆盖配置文件中的``USERNAME``项) 66 | ``-a [authorization URL]`` eportal的验证URL,只推荐非武大校园网环境的用户声明此项。如果你自行抓包发现武大校园网的验证方法有变动,你应当fork本repo后修改并提出pull request。 67 | ``-f`` 开启前台模式,将会禁用systemd检测 68 | ``-s`` 跳过参数合法性检查,包括禁用13位数字用户名检查,非空密码检查,0-3整数网络编号检查 69 | ``-h`` 打印帮助文本 70 | 71 | 例如,一位用户名为 *2017300000000* 的用户,他的密码是 *123456* ,希望登录 *电信* 网络,他应该使用下面这条命令: 72 | ``/usr/sbin/auto-whu.sh -u 2017300000000 -p 123456 -n 1 -f`` 或 ``/usr/sbin/auto-whu.sh -u 2017300000000 -p 123456 -m dianixn -f`` 73 | *(``-f``可以省略)* -------------------------------------------------------------------------------- /auto-whu.conf: -------------------------------------------------------------------------------- 1 | # This is the configuration file of auto-whu, by default this file should be placed as /etc/auto-whu.conf 2 | 3 | # If auto-whu should ignore the systemd check, set to 1 if you just want to use it manually or implement it into a non-systemd environment (This is strongly not recommanded unless you are using OpenWrt, in that case, you should use auto-whu-openwrt instead) 4 | IGNORE_SYSTEMD='0' 5 | # If auto-whu should ignore the sanity check for username and network, unless you are using auto-whu in a non-whu environment, you should never set it to 1 6 | IGNORE_SANITY='0' 7 | # Username, should be a number of 13 digits, set IGNORE_SANITY to 1 if you want to use non-standard username. 8 | USERNAME='' 9 | # Password, must be specified, set IGNORE_SANITY to 1 if you want to use empty password(experimental) 10 | PASSWORD='' 11 | # Network, 0=China CERNET(default), 1=China Telcom, 2=China Unicom, 3=China Mobile, 12 | NETWORK='0' 13 | # If you are not using auto-whu in WHU environment, or just for experiment, you may want to specify the network name manually, this overwrites the NETWORK setting above. The numbers 0-3 above each converts to Internet,dianxin,liantong and yidong here in WHU as of 2021. 14 | NETWORK_MANUAL='' 15 | # Authorization URL,by default it's set to 'http://172.19.1.9:8080/eportal/InterFace.do?method=login' if empty, DO NOT EDIT THIS UNLESS YOU KNOW WHAT YOU ARE DOING 16 | URL='' -------------------------------------------------------------------------------- /auto-whu.conf.sample: -------------------------------------------------------------------------------- 1 | # This is the configuration file of auto-whu, by default this file should be placed as /etc/auto-whu.conf 2 | 3 | # If auto-whu should ignore the systemd check, set to 1 if you just want to use it manually or implement it into a non-systemd environment (This is strongly not recommanded unless you are using OpenWrt, in that case, you should use auto-whu-openwrt instead) 4 | IGNORE_SYSTEMD='0' 5 | # If auto-whu should ignore the sanity check for username and network, unless you are using auto-whu in a non-whu environment, you should never set it to 1 6 | IGNORE_SANITY='0' 7 | # Username, should be a number of 13 digits, set IGNORE_SANITY to 1 if you want to use non-standard username. 8 | USERNAME='2017302010000' 9 | # Password, must be specified, set IGNORE_SANITY to 1 if you want to use empty password(experimental) 10 | PASSWORD='123456' 11 | # Network, 0=China CERNET(default), 1=China Telcom, 2=China Unicom, 3=China Mobile, 12 | NETWORK='1' 13 | # If you are not using auto-whu in WHU environment, or just for experiment, you may want to specify the network name manually, this overwrites the NETWORK setting above. The numbers 0-3 above each converts to Internet,dianxin,liantong and yidong here in WHU as of 2021. 14 | NETWORK_MANUAL='' 15 | # Authorization URL,by default it's set to 'http://172.19.1.9:8080/eportal/InterFace.do?method=login' if empty, DO NOT EDIT THIS UNLESS YOU KNOW WHAT YOU ARE DOING 16 | URL='' -------------------------------------------------------------------------------- /auto-whu.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Automatically login to WHU network 3 | After=network-online.target 4 | Wants=network-online.target 5 | 6 | [Service] 7 | #It's strongly recommended to create a seperate user to run auto-whu 8 | #User=auto-whu 9 | ExecStart=/usr/sbin/auto-whu.sh -c /etc/auto-whu.conf 10 | 11 | [Install] 12 | WantedBy=multi-user.target -------------------------------------------------------------------------------- /auto-whu.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Online check 3 | check_online() { 4 | ping -w1 -W1 -c 1 baidu.com 1>/dev/null 2>&1 5 | [[ $? = 0 ]] && echo "Network is already up" && return 0 6 | return 1 7 | } 8 | # Check online and immediately exit if is running by systemd 9 | check_online && [[ $? = 0 ]] && [[ ! -z "$INVOCATION_ID" ]] && exit 10 | echo "Warning: running auto-whu when already online is dangerous, you may get your account banned for too many login requests. Use systemd and the bundled service and timer file to manage auto-whu instead. Check the repo for more info: https://github.com/7Ji/auto-whu-standard" 11 | # Help message 12 | help () { 13 | echo "Usage: $0 -u [username] -p [password] -n [network] -m [manual network] -u [url] -c [config file] -f -s -h" 14 | echo " -u username, should be a number of 13 digits" 15 | echo " -p password, any value not empty" 16 | echo " -n network, single-digit number from 0 to 3, 0 for CERNET, 1 for China Telcom, 2 for China Unicom, 3 for China Mobile" 17 | echo " -m a manually specified network name, replace the -n option" 18 | echo " -c config file, path to the configuration file" 19 | echo " -a eportal authorization URL, DO NOT SET IT unless you totally understand it" 20 | echo " -f foreground mode, ignore the systemd check" 21 | echo " -s skip check for sanity for username, password and network" 22 | echo " -h print this message" 23 | echo " *notice that all other arguments will overwrite the value provided by the config file" 24 | } 25 | # Check arguments 26 | [[ $# = 0 ]] && help && exit 27 | while [[ $# -ge 1 ]]; do 28 | if [[ "$1" = '-u' ]]; then 29 | ARG_USERNAME="$2" 30 | shift 31 | elif [[ "$1" = '-p' ]]; then 32 | ARG_PASSWORD="$2" 33 | shift 34 | elif [[ "$1" = '-n' ]]; then 35 | ARG_NETWORK="$2" 36 | shift 37 | elif [[ "$1" = '-m' ]]; then 38 | ARG_NETWORK_MANUAL="$2" 39 | shift 40 | elif [[ "$1" = '-a' ]]; then 41 | ARG_URL="$2" 42 | shift 43 | elif [[ "$1" = '-c' ]]; then 44 | ARG_CONFIG="$2" 45 | shift 46 | elif [[ "$1" = '-f' ]]; then 47 | ARG_IGNORE_SYSTEMD='1' 48 | elif [[ "$1" = '-s' ]]; then 49 | ARG_IGNORE_SANITY='1' 50 | elif [[ "$1" = '-h' ]]; then 51 | help && exit 52 | fi 53 | shift 54 | done 55 | # Check and read configuration file if neccessary 56 | if [[ ! -z "$ARG_CONFIG" ]]; then 57 | [[ ! -f "$ARG_CONFIG" ]] && echo "ERROR: The configuration file '$ARG_CONFIG' you've provided does not exist." 58 | [[ ! -r "$ARG_CONFIG" ]] && echo "ERROR: Not allowed to read the configuration file '$ARG_CONFIG', check your permission" 59 | source "$ARG_CONFIG" 60 | fi 61 | [[ ! -z "$ARG_USERNAME" ]] && USERNAME=$ARG_USERNAME 62 | [[ ! -z "$ARG_PASSWORD" ]] && PASSWORD=$ARG_PASSWORD 63 | [[ ! -z "$ARG_NETWORK" ]] && NETWORK=$ARG_NETWORK 64 | [[ ! -z "$ARG_NETWORK_MANUAL" ]] && NETWORK_MANUAL=$ARG_NETWORK_MANUAL 65 | [[ ! -z "$ARG_URL" ]] && URL=$ARG_URL 66 | [[ ! -z "$ARG_IGNORE_SYSTEMD" ]] && IGNORE_SYSTEMD='1' 67 | [[ ! -z "$ARG_IGNORE_SANITY" ]] && IGNORE_SANITY='1' 68 | # Default value downgrading 69 | [[ -z "$NETWORK" && -z "$NETWORK_MANUAL" ]] && NETWORK='0' && echo "Neither network number nor manual network name was set, defaulting network to 0(CERNET)" 70 | [[ -z "$URL" ]] && URL='http://172.19.1.9:8080/eportal/InterFace.do?method=login' && echo "Using default eportial authorization URL 'http://172.19.1.9:8080/eportal/InterFace.do?method=login'" 71 | # Check systemd 72 | if [[ -z "$INVOCATION_ID" && "$IGNORE_SYSTEMD" != 1 ]]; then 73 | echo "You are running this script manually or in a non-systemd environment, it's better to manage this script with systemd." 74 | echo "Check the github repo to learn how to use this script properly: https://github.com/7Ji/auto-whu-standard" 75 | echo "You can set IGNORE_SYSTEMD='1' in the config file or use the argument -f to ignore this check" 76 | fi 77 | # Check intergrity or sanity. return code 1 for insanity. 78 | if [[ "$IGNORE_SANITY" != 1 ]]; then 79 | echo "Starting sanity check for username, password and network, you can set IGNORE_SANITY='1' in config file, or use argument -n to ignore this check." 80 | [[ ! "$USERNAME" =~ ^[0-9]{13}$ ]] && echo "ERROR:The username '$USERNAME' you provided is not a number of 13 digits" && exit 1 81 | [[ -z "$PASSWORD" ]] && echo "ERROR:You've specified an empty password" && exit 1 82 | [[ ! "$NETWORK" =~ ^[0-3]$ && -z "$NETWORK_MANUAL" ]] && echo "ERROR:You've specified a network number not supported, only 0-3 is supported, 0 for CERNET(default), 1 for China Telcom, 2 for China Unicom, 3 for China Mobile" && exit 1 83 | echo "Sanity check pass." 84 | fi 85 | # Network number conversion 86 | if [[ -z "$NETWORK_MANUAL" ]]; then 87 | if [[ "$NETWORK" = 0 ]]; then 88 | NETWORK_STRING=Internet 89 | elif [[ "$NETWORK" = 1 ]]; then 90 | NETWORK_STRING=dianxin 91 | elif [[ "$NETWORK" = 2 ]]; then 92 | NETWORK_STRING=liantong 93 | else 94 | NETWORK_STRING=yidong 95 | fi 96 | else 97 | NETWORK_STRING=$NETWORK_MANUAL 98 | fi 99 | # Authorization 100 | echo "Trying to authorize..." 101 | curl -d "userId=$USERNAME&password=$PASSWORD&service=$NETWORK_STRING&queryString=`curl baidu.com | grep -oP "(?<=\?).*(?=\')" | sed 's/&/%2526/g' | sed 's/=/%253D/g'`&operatorPwd=&operatorUserId=&validcode=&passwordEncrypt=false" $URL 1>/dev/null 2>&1 102 | check_online && [[ $? = 0 ]] && exit 103 | echo "Failed to authorize, you may need to check your account info and credit and network connection" 104 | -------------------------------------------------------------------------------- /auto-whu.timer: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Run auto-whu periodly to check online 3 | 4 | [Timer] 5 | # Immediately authorization after booting up 6 | OnBootSec=0min 7 | # How much time should auto-whu wait until re-check if you're online. In this case we wait 1 min. 8 | OnUnitActiveSec=1min 9 | Persistent=true 10 | 11 | [Install] 12 | WantedBy=timers.target --------------------------------------------------------------------------------