风影子
作者风影子·2012-09-21 09:41
数据库管理员·深圳

【转】 SSH无密码登录-多节点自动化部署SHELL篇

字数 26631阅读 8815评论 0赞 1
来源:http://f.dataguru.cn/thread-19920-1-1.html
      
      在多个节点上手动配置SSH无密码登陆是个很闹心,又容易出错的事儿。如果有毅力的话,一台一台的配置,肯定是可以的,而且
可以提高打字的速度。但是如果在成千上万台集群节点上这么干的话,估计得把人给累死啦 

      所以这种吃力不讨好的活儿硬逼着同学们赶紧想办法让机器帮着我们干点活儿。

      终于经过长时间的失败,再失败,嘿嘿,现在大家看到的应该是可行的方案啦。
      希望可以节省大家的体力,当然也算抛砖引玉,希望大家可以有集思广益,搞出更多更好的方案,耶 ~

本解决方法主要包括两个脚本: sshpass.shssh4slaves
OS: CentOS 6.3 64 bit
大家需要在每个节点上提前装好"expect"工具,我们主要靠这个兄弟干活儿......, 具体脚本里面有说明

1. sshpass.sh
  1. #!/bin/bash
  2. # Name     : sshpass.sh
  3. # Time     : 17/09/2012
  4. # Author   : simplestone@dbinterest.com
  5. # Purpose  : For fast and easy setup of the SSH Passwordless access among all the nodes
  6. #            in a cluster. 
  7. # User     : Any user you are performing the test! Better to settup a separate user from your
  8. #            working env to avoid troubles!!! "root" is used in this example, and you can change it
  9. #            via the export virable "USER=root"
  10. # Attention: The test env is assuming that each $USER on each $HOST is usring the same password!
  11. #            And this likely makes sense as no body want to put more trouble on this.
  12. # Usage    : 1st, make sure the script has the execute permisison "chmod +x ssh_pass.sh"
  13. #            ./ssh_pass.sh password
  14. #          : 2nd, ensure the "ssh4slaves.sh" script is with ssh_pass.sh for all nodes setup!!!
  15. #          : 3rd, "expect" has to be installed on all the nodes for the SSH config

  16. export FILELOC="/root"
  17. export SLAVESFILE="$FILELOC/sshslaves"
  18. export HOSTS=`cat $FILELOC/sshhosts`
  19. export SLAVES=`cat $FILELOC/sshslaves`
  20. export SSH4SLAVESCRIPT="$FILELOC/ssh4slaves.sh"
  21. export MASTER=hdp01
  22. export USER=root
  23. export PASSWD=$1
  24. export SSHLOC="$FILELOC/.ssh/"
  25. export RSAFILE="$FILELOC/.ssh/id_rsa"
  26. export RSAPUBFILE="$FILELOC/.ssh/id_rsa.pub"
  27. export AUTHFILE="$FILELOC/.ssh/authorized_keys"
  28. export EXPECTCHK=`rpm -qa expect | wc -l`

  29. #
  30. if [ $EXPECTCHK != 1 ]
  31.   then
  32.   echo ''
  33.   echo "########################################################################################"
  34.   echo "Please install the "expect" package first on all nodes to allow the script to run!!!"
  35.   echo "yum -y install expect"
  36.   echo "########################################################################################"
  37. else
  38.   if [ -e $RSAFILE ]
  39.     then
  40.     echo "########################################################################################"
  41.     echo "Attention: This is for TEST ONLY, please fully test it before applying it to PROD"
  42.     echo "environment!!! OR you might get in trouble!!!"
  43.     echo ''
  44.     echo "BETTER TO HAVE A NEW USER FOR THE TEST TO AVOID DESTROYING YOUR ENVIRONMENT!"
  45.     echo ''
  46.     echo "Please manually delete the ssh related file on each host before executing the script!!!"
  47.     echo ''
  48.     for host in $HOSTS
  49.     do 
  50.     echo "Please run command on $host: rm -rf $SSHLOC"
  51.     done
  52.     echo "########################################################################################"
  53.   else
  54.   # Just generate 
  55.     for host in $HOSTS
  56.     do
  57.       if [ $host = "$MASTER" ]
  58.         then 
  59.         echo ''
  60.         echo "###########################################################"
  61.         echo "Generating RSA keys for MASTER host $MASTER"
  62.         echo "###########################################################"
  63.         echo ''
  64.         expect -c "
  65.             set timeout 1
  66.             spawn ssh $USER@$host
  67.             expect "yes/no"
  68.             send -- "yesr"
  69.             expect "password:"
  70.             send -- "$PASSWDr"
  71.             expect "#"
  72.             send "ssh-keygen -t rsa -P '' -f $RSAFILEr"
  73.             expect "#"
  74.             send "ssh-copy-id -i $RSAPUBFILE $MASTERr"
  75.             expect "password:"
  76.             send -- "$PASSWDr"
  77.             expect eof
  78.          "
  79.         else
  80.          echo ''
  81.          echo "###########################################################"
  82.          echo "Generating RSA keys for all OTHER hosts..."
  83.          echo "hostname is $host"
  84.          echo "###########################################################"
  85.          echo ''
  86.          expect -c "
  87.            set timeout 1
  88.            spawn ssh $USER@$host
  89.            expect "yes/no"
  90.            send -- "yesr"
  91.            expect "password:"
  92.            send -- "$PASSWDr"
  93.            expect "#"
  94.            send "ssh-keygen -t rsa -P '' -f $RSAFILEr"
  95.            expect "#"
  96.            send "ssh-copy-id -i $RSAPUBFILE $MASTERr"
  97.            expect "yes/no"
  98.            send -- "yesr"
  99.            expect "password:"
  100.            send -- "$PASSWDr"
  101.            expect eof
  102.            "
  103.          fi
  104.     done            
  105.           
  106.     ### 
  107.     for host in $SLAVES 
  108.     do
  109.         echo ''
  110.         echo "############################################################################"
  111.         echo "Copying authorized_keys to host $host from the MASTER host $MASTER..."
  112.         echo "############################################################################"
  113.         echo ''
  114.         expect -c "
  115.         set timeout 1
  116.         spawn scp $AUTHFILE "$USER@$host:$SSHLOC"
  117.         expect "password:"
  118.         send -- $PASSWDr
  119.         expect eof
  120.         "
  121.     done
  122.   
  123.   #
  124.     for host in $SLAVES
  125.     do
  126.       echo ''
  127.       echo "############################################################################"
  128.       echo "Distributing the $SLAVESFILE file to slave host $host..."
  129.       echo "############################################################################"
  130.       echo ''
  131.       scp $SLAVESFILE "$host:$FILELOC"
  132.       echo ''
  133.       echo "############################################################################"
  134.       echo "Distributing the $SSH4SLAVESCRIPT script to slave host $host..."
  135.       echo "############################################################################"
  136.       echo ''
  137.       scp $SSH4SLAVESCRIPT "$host:$FILELOC"
  138.     done
  139.   
  140.   
  141.     for host in $SLAVES
  142.     do
  143.       echo ''
  144.       echo "############################################################################"
  145.       echo "Working on the slaves node $host to ensure no prompt for the "yes/no" question..."
  146.       echo "############################################################################"
  147.       echo ''
  148.       ssh -q $USER@$host $SSH4SLAVESCRIPT
  149.     done
  150.     
  151.     ### Check whether the Passwordless ssh works ###
  152.     for host in $HOSTS
  153.     do
  154.       echo ''
  155.       echo "############################################################################"
  156.       echo "Check whether the Passwordless SSH works for $host..."
  157.       echo "############################################################################"
  158.       echo ''
  159.       ssh $host uname -a && date
  160.     done
  161.   fi
  162. fi
复制代码


2.ssh4slaves
  1. #!/bin/bash
  2. # Name     : ssh4slaves.sh
  3. # Time     : 17/09/2012
  4. # Author   : simplestone@dbinterest.com
  5. # Purpose  : For fast and easy setup of the SSH Passwordless access among all the slave nodes
  6. #            in a cluster. Mainly to ensure no prompt for "yes/no" again!!!
  7. # User     : Any user you are performing the test! Better to settup a separate user from your
  8. #            working env to avoid troubles!!! "root" is used in this example, and you can change it
  9. #            via the export virable "USER=root"
  10. # Attention: The test env is assuming that each $USER on each $HOST is usring the same password!
  11. #            And this likely makes sense as no body want to put more trouble on this.
  12. # Usage    : This script is called by the main script "ssh_pass.sh"
  13. #            1st, make sure the script has the execute permisison "chmod +x ssh4slaves.sh" before
  14. #            distributing it to other slaves node.
  15. #            2nd, Remember to change variable "PASSWORD" before start the main script "sshpass.sh" 

  16. export FILELOC="/root"
  17. export SLAVES=`cat $FILELOC/sshslaves`
  18. export USER=root
  19. export PASSWD=stonetest

  20. for host in $SLAVES
  21. do
  22.     echo ''
  23.     echo "Ensure ssh passwordless works among all slave nodes..."
  24.     echo ''
  25.     expect -c "
  26.         set timeout 1
  27.         spawn ssh $USER@$host
  28.         expect "yes/no"
  29.         send -- "yesr"
  30.         expect eof
  31.      "
  32. done
复制代码
3. 其他配置
  1. [root@hdp01 ~]# pwd
  2. /root
  3. [root@hdp01 ~]# cat sshhosts
  4. hdp01
  5. hdp02
  6. hdp03
  7. [root@hdp01 ~]# cat sshslaves
  8. hdp02
  9. hdp03
  10. [root@hdp01 ~]# ls -lrth | tail -2
  11. -rwxr-xr-x  1 root root 1.3K Sep 18 02:08 ssh4slaves.sh
  12. -rwxr-xr-x  1 root root 6.5K Sep 18 02:11 ssh_pass.sh
复制代码


4. 测试输出
  1. [root@hdp01 ~]# ./ssh_pass.sh stonetest

  2. ###########################################################
  3. Generating RSA keys for MASTER host hdp01
  4. ###########################################################

  5. spawn ssh root@hdp01
  6. The authenticity of host 'hdp01 (192.168.1.121)' can't be established.
  7. RSA key fingerprint is 23:fa:69:0b:a5:b0:c2:80:13:13:ba:2b:7d:b1:5b:ff.
  8. Are you sure you want to continue connecting (yes/no)? yes
  9. Warning: Permanently added 'hdp01,192.168.1.121' (RSA) to the list of known hosts.
  10. root@hdp01's password: 
  11. Last login: Tue Sep 18 02:09:29 2012 from hdp02.dbinterest.local
  12. [root@hdp01 ~]# ssh-keygen -t rsa -P '' -f /root/.ssh/id_rsa
  13. Generating public/private rsa key pair.
  14. Your identification has been saved in /root/.ssh/id_rsa.
  15. Your public key has been saved in /root/.ssh/id_rsa.pub.
  16. The key fingerprint is:
  17. 3a:c3:98:b3:e4:39:fa:fe:87:c6:22:90:16:57:4e:47 root@hdp01.dbinterest.local
  18. The key's randomart image is:
  19. +--[ RSA 2048]----+
  20. |      .E         |
  21. |     o .         |
  22. |    + .          |
  23. | . . .           |
  24. | .o     S        |
  25. |o.   + .         |
  26. |..  =.=.         |
  27. |  .oo++o.        |
  28. |  .=*=..         |
  29. +-----------------+
  30. [root@hdp01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub hdp01
  31. root@hdp01's password: 
  32. Now try logging into the machine, with "ssh 'hdp01'", and check in:

  33.   .ssh/authorized_keys

  34. to make sure we haven't added extra keys that you weren't expecting.

  35. [root@hdp01 ~]# 
  36. ###########################################################
  37. Generating RSA keys for all OTHER hosts...
  38. hostname is hdp02
  39. ###########################################################

  40. spawn ssh root@hdp02
  41. The authenticity of host 'hdp02 (192.168.1.122)' can't be established.
  42. RSA key fingerprint is 23:fa:69:0b:a5:b0:c2:80:13:13:ba:2b:7d:b1:5b:ff.
  43. Are you sure you want to continue connecting (yes/no)? yes
  44. Warning: Permanently added 'hdp02,192.168.1.122' (RSA) to the list of known hosts.
  45. root@hdp02's password: 
  46. Last login: Tue Sep 18 02:09:23 2012 from hdp02.dbinterest.local
  47. [root@hdp02 ~]# ssh-keygen -t rsa -P '' -f /root/.ssh/id_rsa
  48. Generating public/private rsa key pair.
  49. Created directory '/root/.ssh'.
  50. Your identification has been saved in /root/.ssh/id_rsa.
  51. Your public key has been saved in /root/.ssh/id_rsa.pub.
  52. The key fingerprint is:
  53. a9:89:fe:40:8a:8e:21:55:da:3b:6b:68:4f:3e:8f:fc root@hdp02.dbinterest.local
  54. The key's randomart image is:
  55. +--[ RSA 2048]----+
  56. |                 |
  57. |                 |
  58. |    .            |
  59. |   +     .       |
  60. |  o o   S        |
  61. | o o o o         |
  62. |+ ..* o          |
  63. |+.o=o=           |
  64. |.o oB=E          |
  65. +-----------------+
  66. [root@hdp02 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub hdp01
  67. The authenticity of host 'hdp01 (192.168.1.121)' can't be established.
  68. RSA key fingerprint is 23:fa:69:0b:a5:b0:c2:80:13:13:ba:2b:7d:b1:5b:ff.
  69. Are you sure you want to continue connecting (yes/no)? yes
  70. Warning: Permanently added 'hdp01,192.168.1.121' (RSA) to the list of known hosts.
  71. root@hdp01's password: 
  72. Now try logging into the machine, with "ssh 'hdp01'", and check in:

  73.   .ssh/authorized_keys

  74. to make sure we haven't added extra keys that you weren't expecting.


  75. ###########################################################
  76. Generating RSA keys for all OTHER hosts...
  77. hostname is hdp03
  78. ###########################################################

  79. spawn ssh root@hdp03
  80. The authenticity of host 'hdp03 (192.168.1.123)' can't be established.
  81. RSA key fingerprint is 23:fa:69:0b:a5:b0:c2:80:13:13:ba:2b:7d:b1:5b:ff.
  82. Are you sure you want to continue connecting (yes/no)? yes
  83. Warning: Permanently added 'hdp03,192.168.1.123' (RSA) to the list of known hosts.
  84. root@hdp03's password: 
  85. Last login: Tue Sep 18 02:09:19 2012 from hdp02.dbinterest.local
  86. [root@hdp03 ~]# ssh-keygen -t rsa -P '' -f /root/.ssh/id_rsa
  87. Generating public/private rsa key pair.
  88. Created directory '/root/.ssh'.
  89. Your identification has been saved in /root/.ssh/id_rsa.
  90. Your public key has been saved in /root/.ssh/id_rsa.pub.
  91. The key fingerprint is:
  92. a4:3d:dd:54:42:c0:45:ec:ed:ae:d6:bd:14:a0:9b:16 root@hdp03.dbinterest.local
  93. The key's randomart image is:
  94. +--[ RSA 2048]----+
  95. |         ..*= .  |
  96. |          . .o   |
  97. |        .  ..o   |
  98. |       + . oo o  |
  99. |      . S .E.. . |
  100. |         .  + . .|
  101. |           + o o |
  102. |          . . + .|
  103. |           ... ..|
  104. +-----------------+
  105. [root@hdp03 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub hdp01
  106. The authenticity of host 'hdp01 (192.168.1.121)' can't be established.
  107. RSA key fingerprint is 23:fa:69:0b:a5:b0:c2:80:13:13:ba:2b:7d:b1:5b:ff.
  108. Are you sure you want to continue connecting (yes/no)? yes
  109. Warning: Permanently added 'hdp01,192.168.1.121' (RSA) to the list of known hosts.
  110. root@hdp01's password: 
  111. Now try logging into the machine, with "ssh 'hdp01'", and check in:

  112.   .ssh/authorized_keys

  113. to make sure we haven't added extra keys that you weren't expecting.

  114. [root@hdp03 ~]# 
  115. ############################################################################
  116. Copying authorized_keys to host hdp02 from the MASTER host hdp01...
  117. ############################################################################

  118. spawn scp /root/.ssh/authorized_keys root@hdp02:/root/.ssh/
  119. root@hdp02's password: 
  120. authorized_keys                                                                                            100% 1227     1.2KB/s   00:00    

  121. ############################################################################
  122. Copying authorized_keys to host hdp03 from the MASTER host hdp01...
  123. ############################################################################

  124. spawn scp /root/.ssh/authorized_keys root@hdp03:/root/.ssh/
  125. root@hdp03's password: 
  126. authorized_keys                                                                                            100% 1227     1.2KB/s   00:00    

  127. ############################################################################
  128. Distributing the /root/sshslaves file to slave host hdp02...
  129. ############################################################################

  130. sshslaves                                                                                                  100%   12     0.0KB/s   00:00    

  131. ############################################################################
  132. Distributing the /root/ssh4slaves.sh script to slave host hdp02...
  133. ############################################################################

  134. ssh4slaves.sh                                                                                              100% 1277     1.3KB/s   00:00    

  135. ############################################################################
  136. Distributing the /root/sshslaves file to slave host hdp03...
  137. ############################################################################

  138. sshslaves                                                                                                  100%   12     0.0KB/s   00:00    

  139. ############################################################################
  140. Distributing the /root/ssh4slaves.sh script to slave host hdp03...
  141. ############################################################################

  142. ssh4slaves.sh                                                                                              100% 1277     1.3KB/s   00:00    

  143. ############################################################################
  144. Working on the slaves node hdp02 to ensure no prompt for the yes/no question...
  145. ############################################################################


  146. Ensure ssh passwordless works among all slave nodes...

  147. spawn ssh root@hdp02
  148. The authenticity of host 'hdp02 (192.168.1.122)' can't be established.
  149. RSA key fingerprint is 23:fa:69:0b:a5:b0:c2:80:13:13:ba:2b:7d:b1:5b:ff.
  150. Are you sure you want to continue connecting (yes/no)? yes
  151. Warning: Permanently added 'hdp02,192.168.1.122' (RSA) to the list of known hosts.
  152. Last login: Tue Sep 18 02:11:54 2012 from hdp01.dbinterest.local
  153. [root@hdp02 ~]# 
  154. Ensure ssh passwordless works among all slave nodes...

  155. spawn ssh root@hdp03
  156. The authenticity of host 'hdp03 (192.168.1.123)' can't be established.
  157. RSA key fingerprint is 23:fa:69:0b:a5:b0:c2:80:13:13:ba:2b:7d:b1:5b:ff.
  158. Are you sure you want to continue connecting (yes/no)? yes
  159. Warning: Permanently added 'hdp03,192.168.1.123' (RSA) to the list of known hosts.
  160. Last login: Tue Sep 18 02:11:55 2012 from hdp01.dbinterest.local
  161. [root@hdp03 ~]# 
  162. ############################################################################
  163. Working on the slaves node hdp03 to ensure no prompt for the yes/no question...
  164. ############################################################################


  165. Ensure ssh passwordless works among all slave nodes...

  166. spawn ssh root@hdp02
  167. The authenticity of host 'hdp02 (192.168.1.122)' can't be established.
  168. RSA key fingerprint is 23:fa:69:0b:a5:b0:c2:80:13:13:ba:2b:7d:b1:5b:ff.
  169. Are you sure you want to continue connecting (yes/no)? yes
  170. Warning: Permanently added 'hdp02,192.168.1.122' (RSA) to the list of known hosts.
  171. Last login: Tue Sep 18 02:11:58 2012 from hdp02.dbinterest.local
  172. [root@hdp02 ~]# 
  173. Ensure ssh passwordless works among all slave nodes...

  174. spawn ssh root@hdp03
  175. The authenticity of host 'hdp03 (192.168.1.123)' can't be established.
  176. RSA key fingerprint is 23:fa:69:0b:a5:b0:c2:80:13:13:ba:2b:7d:b1:5b:ff.
  177. Are you sure you want to continue connecting (yes/no)? yes
  178. Warning: Permanently added 'hdp03,192.168.1.123' (RSA) to the list of known hosts.
  179. Last login: Tue Sep 18 02:11:59 2012 from hdp02.dbinterest.local

  180. ############################################################################
  181. Check whether the Passwordless SSH works for hdp01...
  182. ############################################################################

  183. Linux hdp01.dbinterest.local 2.6.32-279.el6.x86_64 #1 SMP Fri Jun 22 12:19:21 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
  184. Tue Sep 18 02:12:05 PDT 2012

  185. ############################################################################
  186. Check whether the Passwordless SSH works for hdp02...
  187. ############################################################################

  188. Linux hdp02.dbinterest.local 2.6.32-279.el6.x86_64 #1 SMP Fri Jun 22 12:19:21 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
  189. Tue Sep 18 02:12:05 PDT 2012

  190. ############################################################################
  191. Check whether the Passwordless SSH works for hdp03...
  192. ############################################################################

  193. Linux hdp03.dbinterest.local 2.6.32-279.el6.x86_64 #1 SMP Fri Jun 22 12:19:21 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
  194. Tue Sep 18 02:12:06 PDT 2012
复制代码
5. 其他节点测试
  1. [root@hdp02 ~]# 
  2. [root@hdp02 ~]# ssh hdp02
  3. Last login: Tue Sep 18 02:12:00 2012 from hdp03.dbinterest.local
  4. [root@hdp02 ~]# exit
  5. logout
  6. Connection to hdp02 closed.
  7. [root@hdp02 ~]# ssh hdp03
  8. Last login: Tue Sep 18 02:12:02 2012 from hdp03.dbinterest.local
  9. [root@hdp03 ~]# exit
  10. logout
  11. Connection to hdp03 closed.
  12. [root@hdp02 ~]# 

  13. ----------

  14. [root@hdp03 ~]# 
  15. [root@hdp03 ~]# ssh hdp01
  16. Last login: Tue Sep 18 02:12:22 2012 from hdp02.dbinterest.local
  17. [root@hdp01 ~]# exit
  18. logout
  19. Connection to hdp01 closed.
  20. [root@hdp03 ~]# ssh hdp02
  21. Last login: Tue Sep 18 02:12:25 2012 from hdp02.dbinterest.local
  22. [root@hdp02 ~]# exit
  23. logout
  24. Connection to hdp02 closed.
  25. [root@hdp03 ~]# ssh hdp03
  26. Last login: Tue Sep 18 02:12:30 2012 from hdp02.dbinterest.local
  27. [root@hdp03 ~]# exit
  28. logout
  29. Connection to hdp03 closed.
  30. [root@hdp03 ~]#
复制代码



如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

1

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

相关文章

相关资料

X社区推广