課時5-字符集和許可權安全----檢查密碼強弱性

小亮520cl發表於2015-11-19
指令碼

點選(此處)摺疊或開啟

  1. [root@localhost lscj]# more checkpass2.sh
  2. #! /bin/sh

  3. # set -x

  4. # Get the key value of input arguments format like '--args=value'.
  5. get_key_value()
  6. {
  7.     echo "$1" | sed 's/^--[a-zA-Z_-]*=//'
  8. }

  9. # Usage will be helpful when you need to input the valid arguments.
  10. usage()
  11. {
  12. cat <<EOF
  13. Usage: $0 [configure-options]
  14.   -?, --help Show this help message.
  15.   --host=<> Set mysql host ip address
  16.   --user=<> Set mysql user
  17.   --password=<> Set mysql password
  18.   --port=<> Set mysql host port
  19. Note: this script is intended for internal use by developers.

  20. EOF
  21. }

  22. # Print the default value of the arguments of the script.
  23. print_default()
  24. {
  25. cat <<EOF
  26.   The default value of the variables:
  27.   host=127.0.0.1
  28.   user=root
  29.   port=3306
  30. EOF
  31. }

  32. # Parse the input arguments and get the value of the input argument.
  33. parse_options()
  34. {
  35.   while test $# -gt 0
  36.   do
  37.     case "$1" in
  38.     --host=*)
  39.       HOST=`get_key_value "$1"`;;
  40.     --user=*)
  41.       USER=`get_key_value "$1"`;;
  42.     --password=*)
  43.       PASSWORD=`get_key_value "$1"`;;
  44.     --port=*)
  45.       PORT=`get_key_value "$1"`;;
  46.     -? | --help)
  47.       usage
  48.       print_default
  49.       exit 0;;
  50.     *)
  51.       echo "Unknown option '$1'"
  52.       exit 1;;
  53.     esac
  54.     shift
  55.   done
  56. }

  57. ############################################################
  58. # Define the variables the script used for executing.
  59. HOST=127.0.0.1
  60. USER=root
  61. PORT=3306
  62. currentDIR=`dirname ${0}`

  63. # Call the parse_options function to parse the input arguments.
  64. parse_options "$@"
  65. echo ${HOST}
  66. echo ${USER}
  67. echo ${PASSWORD}
  68. echo ${PORT}

  69. mysql -u${USER} -p${PASSWORD} -h${HOST} -P${PORT} -Ne 'select user,host,password from mysql.user'>./userinfo

  70. #echo "------------ host:${HOST} port:${PORT} checkTime:`date +'%Y-%m-%d %T'`-------------">>./weakPassword.log
  71. cat /root/python/lscj/userinfo|while read line
  72.  do
  73.         userName=`echo $line|awk -F' ' '{print $1}'`
  74.         userHost=`echo $line|awk -F' ' '{print $2}'`
  75.         userPwd=`echo $line|awk -F' ' '{print $3}'`
  76.         echo "userName=$userName,userHost=$userHost,userPwd=$userPwd"
  77.         result=`mysql -uroot -pESBecs00 -Ne "select 1 from test.tb_weak_password where password='$userPwd'"`
  78.                 if [ ! -n "$result" ]
  79.                      then
  80.                         echo "User:${userName}@${userHost} is safe!!">>safe.log
  81.                 else
  82.                         echo "User:${userName}@${userHost} is unsaft!!">>unsafe.log
  83.                 fi
  84.         echo ""
  85.         echo ""
  86. done

test.tb_weak_password是密碼字典!需要自己去生成,透過對比檢查使用者密碼的強弱性

執行結果如下
[root@localhost lscj]# sh checkpass2.sh  --password=ESBecs00
[root@localhost lscj]# more unsafe.log 
User:root@localhost is unsaft!!
User:liang@% is unsaft!!
[root@localhost lscj]# more safe.log 
User:jonn@% is safe!!
User:tom@% is safe!!
User:marry@% is safe!!
User:karry@% is safe!!



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-1840999/,如需轉載,請註明出處,否則將追究法律責任。

相關文章