批量实现无密码认证脚本

1020阅读 0评论2016-01-26 sync_1521
分类:LINUX

该脚本适用场景:
   一台linux机器需要无密码登录到N台linux机器;
   具体用法请参考usage(注:yourpasswd为你需要创建信任关系机器的登录密码);
   该脚本首先会去检查无密码认证是否ok,如果不ok,那就创建信任关系,并再次检查无密码认证是否ok。

  1. #!/bin/bash

  2. function usage()
  3. {
  4. cat <<EOF
  5.     usage:
  6.          auth-check.sh IpList Password
  7.     examle:
  8.          auth-check.sh 192.168.1.1..100,192.168.99.22..99,192.168.9.10 yourpasswd
  9. EOF
  10. }
  11. #检查机器是否能无密码登录
  12. function check()
  13. {
  14.         ret=`ssh -o ConnectTimeout=3 -o PasswordAuthentication=no -o KbdInteractiveDevices=no -o StrictHostKeyChecking=no $1 date 2>/dev/null
  15. `
  16.         if [[ -z $ret ]]
  17.         then
  18.                 ssh-keygen -R $1 &>/dev/null
  19.                 return 1
  20.         else
  21.                 echo "check ssh ok:$1"
  22.                 return 0
  23.         fi
  24. }
  25. #本机生成公钥私钥
  26. function create_key()
  27. {
  28.         umask 077; test -d ${HOME}/.ssh || mkdir ${HOME}/.ssh
  29.         /usr/bin/expect <<-EOF
  30.         spawn ssh-keygen -t rsa -P "" -f ${HOME}/.ssh/id_rsa
  31.         expect {
  32.                 "Overwrite (y/n)?" {
  33.                         send -- "n\r"
  34.                 } eof {
  35.                         puts ">> generate id_rsa and id_rsa.pub ... ...\n"
  36.                 } timeout {
  37.                 exit 1
  38.             }
  39.         }
  40.         expect eof
  41. EOF
  42. }
  43. #上传公钥到目标机器
  44. function auth()
  45. {
  46.         /usr/bin/expect <<-EOF
  47.         spawn ssh-copy-id -i ${HOME}/.ssh/id_rsa.pub $1
  48.         expect {
  49.                 "Are you sure you want to continue connecting (yes/no)?" {
  50.                         send -- "yes\r"
  51.                         exp_continue
  52.                 }
  53.                 "*word:" {
  54.                         send -- "${password}\r"
  55.                 } eof {
  56.                         exit 0
  57.                 } timeout {
  58.                         exit 1
  59.                 }
  60.         }
  61.         expect eof
  62. EOF
  63. }
  64. #做无密码认证后重新检查无密码登录是否成功
  65. function recheck()
  66. {
  67.         echo
  68.         echo -e "\e[33mre-auth:$1 \e[0m"
  69.         check $1
  70.         if [[ $? -eq 0 ]]
  71.         then
  72.                 echo -e "\e[33mauthority sucess:$1 \e[0m"
  73.         else
  74.                 echo -e "\e[31mauthority failed! please check machine:$1 \e[m"
  75.         fi
  76. }
  77. #主函数
  78. function main()
  79. {
  80.         if [[ $# -ne 2 ]];then
  81.                 usage
  82.                 exit 2

  83.         fi
  84.         password=$2
  85.         create_key &>/dev/null
  86.         iparry=`echo $1|sed 's/,/ /g'`
  87.         echo "IpList:$iparry"
  88.         for ip in ${iparry}
  89.         do
  90.                 if grep -Ex "([0-9]{1,3}\.){3}[0-9]{1,3}" <<<"$ip" &>/dev/null;then
  91.                         check $ip
  92.                         if [[ $? -ne 0 ]];then
  93.                                 auth $ip &>/dev/null
  94.                                 recheck $ip
  95.                         fi

  96.                 elif grep -Ex "([0-9]{1,3}\.){3}[0-9]{1,3}\.\.[0-9]{1,3}" <<<"$ip" &>/dev/null ;then
  97.                         ip_pre=`echo $ip |cut -d '.' -f -3`
  98.                         ip_start=`echo $ip|cut -d '.' -f 4`
  99.                         ip_end=`echo $ip|cut -d '.' -f 6`
  100.                         for ((i= ${ip_start};i<=${ip_end};i++))
  101.                         do
  102.                                 ip_dst="${ip_pre}.$i"
  103.                                 check $ip_dst
  104.                                 if [[ $? -ne 0 ]];then
  105.                                         auth $ip_dst &>/dev/null
  106.                                         recheck $ip_dst
  107.                                 fi
  108.                         done
  109.                 else
  110.                         echo -e "\e[31m please check IP parameter \e[0m"
  111.                 fi
  112.         done
  113.         exit 0

  114. }

  115. main "$@"

上一篇:Shell输入密码时关闭屏幕回显
下一篇:大型网站运维探讨和心得