dynpasswd

From NAT, 7 Years ago, written in Plain Text, viewed 695 times.
URL https://code.nat.moe/view/79a2147a Embed
Download Paste or View Raw
  1. #!/bin/bash
  2.  
  3. # dynpasswd - Dynamic Password for CGS ID
  4.  
  5. SHADOW_DIR="./shadow/"
  6. SSID_GEN_URL="http://127.0.0.1:8080/mis/info/menu_info.asp?type=%D1%A7%C9%FA%CD%F8%D2%B3"
  7. AUTH_URL="http://127.0.0.1:8080/mis/info/list.asp"
  8. PWD_URL="http://127.0.0.1:8080/mis/info/pub_info/set_pass.asp"
  9. SSID_FILENAME="./.ssid.cookie"
  10.  
  11. UPDATE_INT=60
  12.  
  13. function deamon {
  14.         echo "[$(date)] dynpasswd-deamon started"
  15.         while true
  16.         do
  17.                 for update in $(ls $SHADOW_DIR)
  18.                 do
  19.                         [[ $(auth $update) == "true" ]] && echo -n "[$(date)] $update: successfly auth... " && update_passwd $(randpw) $update
  20.                         [[ $(auth $update) == "false" ]] && rm $SHADOW_DIR$update && echo "[$(date)] Failed to auth $update. Remove shadow $update"
  21.                 done
  22.                 sleep $UPDATE_INT
  23.         done
  24. }
  25.  
  26. function randpw {
  27.         RAND_PW=$RANDOM$RANDOM
  28.         LEN=${#RAND_PW}
  29.         RAND_PW=${RAND_PW:$((LEN - 6))}
  30.         echo $RAND_PW
  31. }
  32.  
  33. function update_ssid {
  34.         echo -n "[$(date)] Updateing ssid... "
  35.         curl -c $SSID_FILENAME $SSID_GEN_URL > /dev/null 2> /dev/null
  36.         [[ $? == "0" ]] && echo "OK"
  37.         [[ $? != "0" ]] && echo "Failed!" && exit 1
  38. }
  39.  
  40. function auth {
  41.         [[ -z $1 ]] && return 1
  42.         [[ ! -e $SSID_FILENAME ]] && return 1
  43.         passwd=$(cat $SHADOW_DIR$1)
  44.         [[ ! -z $(curl -b $SSID_FILENAME --data "tbarno=$1&passwd=$passwd&hd=002" $AUTH_URL 2> /dev/null | iconv -f gbk -t utf-8 | grep 个人资料) ]] && echo "true" && return 0
  45.         echo "false" && return 1
  46.  
  47. }
  48.  
  49. function update_passwd {
  50.         [[ ! -z $(curl -b .ssid.cookie --data "hd=002&T1=$1&T2=$1" $PWD_URL 2> /dev/null | iconv -f gbk -t utf-8 | grep OK) ]] && echo "Updated: $1"&& echo $1 > $SHADOW_DIR$2 && return 0
  51.         echo "ERROR" && return 1
  52. }
  53.  
  54. function usage {
  55.         echo "Usage: dynpasswd --add|-a <new id> <password>
  56.        dynpasswd --remove|-r <id>
  57.        dynpasswd --deamon|-d"
  58.         exit
  59. }
  60.  
  61. function main {
  62.         [[ -z $* ]] && usage
  63.         case $1 in
  64.                 -a|--add)
  65.                         [[ -z $3 ]] && usage
  66.                         echo $3 > $SHADOW_DIR$2
  67.                 ;;
  68.                 -r|--remove)
  69.                         [[ -z $2 ]] && usage
  70.                         rm $SHADOW_DIR$2
  71.                 ;;
  72.                 -d|--deamon)
  73.                         update_ssid
  74.                         deamon
  75.                 ;;
  76.                 *)
  77.                         usage
  78.                 ;;
  79.         esac
  80. }
  81.  
  82. main $*

Reply to "dynpasswd"

Here you can reply to the paste above

captcha