diff --git a/README.md b/README.md index ead10a8..fd91500 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,12 @@ Command Line Scripts for ZJU (VPN / WLAN / DNS) +## Require + +```bash +sudo apt-get install curl +``` + ## TODO - zjunet vpn @@ -25,3 +31,5 @@ Command Line Scripts for ZJU (VPN / WLAN / DNS) ## Links - [Array in unix Bourne Shell](http://unix.stackexchange.com/questions/137566/array-in-unix-bourne-shell) + +- [How do you tell if a string contains another string in Unix shell scripting?](http://stackoverflow.com/questions/2829613/how-do-you-tell-if-a-string-contains-another-string-in-unix-shell-scripting) diff --git a/user.sh b/user.sh index 1201e2c..46c44c6 100755 --- a/user.sh +++ b/user.sh @@ -22,6 +22,10 @@ DIR="$HOME/.zjunet" mkdir -p $DIR +getall() { + ls -1A $DIR | xargs | tr "\n" " " +} + # dispatch case "$1" in @@ -38,7 +42,8 @@ case "$1" in ;; delete) - read -p "USERNAME: " USERNAME + USERS=$(getall) + read -p "Delete User [ ${USERS}]: " USERNAME rm -i "$DIR/${USERNAME}" ;; @@ -55,7 +60,7 @@ case "$1" in exit 1 else if [ "${COUNT}" -gt "1" ]; then - USERS=$(ls -1A $DIR | xargs | tr "\n" " ") + USERS=$(getall) read -p "Choose User [ ${USERS}]: " USERNAME else USERNAME=$(ls -1 $DIR | head -n1) @@ -67,7 +72,7 @@ case "$1" in # Get all users # @private getall) - ls -1A $DIR | xargs | tr "\n" " " + getall ;; # @private diff --git a/wlan.sh b/wlan.sh index 14a292b..c9e5516 100755 --- a/wlan.sh +++ b/wlan.sh @@ -25,10 +25,16 @@ logout() { echo "Logout: ${USERNAME}" RESPONSE=$(curl "https://net.zju.edu.cn/rad_online.php" -H "Content-Type: application/x-www-form-urlencoded" -d "action=auto_dm&username=${USERNAME}&password=${PASSWORD}" -s) - if [[ "${RESPONSE}" != "ok" ]]; then - echo "${RESPONSE}" >&2 - exit 1 - fi + + case "${RESPONSE}" in + *ok*) + echo "Logout: success." + ;; + *) + echo "Logout: ${RESPONSE}." + exit 1; + ;; + esac } login() { @@ -39,19 +45,25 @@ login() { echo "Login: ${USERNAME}" RESPONSE=$(curl "https://net.zju.edu.cn/cgi-bin/srun_portal" -H "Content-Type: application/x-www-form-urlencoded" -d "action=login&username=${USERNAME}&password=${PASSWORD}&ac_id=3&type=1&is_ldap=1&local_auth=1" -s) - if [[ "${RESPONSE}" = *"help.html"* || "${response}" = *"login_ok"* ]]; then - echo "Login successful" - else - echo "${RESPONSE}" >&2 - exit 1 - fi + + case "${RESPONSE}" in + *help.html*) + echo "Login: success." + ;; + *login_ok*) + echo "Login: success." + ;; + *) + echo "Login: ${RESPONSE}" >&2 + exit 1 + ;; + esac } BASEDIR=$(dirname $0) USER="${BASEDIR}/user.sh" USERNAME=$($USER get) - PASSWORD=$($USER getpwd $USERNAME) case "$1" in @@ -61,4 +73,10 @@ case "$1" in logout) logout $USERNAME $PASSWORD ;; + *) + echo "Usage: " + echo " zjunet wlan login" + echo " zjunet wlan logout" + ;; esac + diff --git a/zjunet.sh b/zjunet.sh index ba14981..c269b91 100755 --- a/zjunet.sh +++ b/zjunet.sh @@ -28,10 +28,10 @@ case "$1" in ip route show 0/0 | cut -d " " -f 3 ;; wlan) - "${BASEDIR}/wlan.sh" + "${BASEDIR}/wlan.sh" $2 ;; vpn) - "${BASEDIR}/vpn.sh" + "${BASEDIR}/vpn.sh" $2 ;; dns) "${BASEDIR}/dns.sh"