Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30410
  • 博文数量: 11
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 131
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-14 19:23
个人简介

a person

文章分类

全部博文(11)

文章存档

2015年(11)

我的朋友

分类: LINUX

2015-05-24 14:51:15

情景描述

当需要进行集群配置时,如hadoop集群,mongodb集群等等,都需要使用ssh建立安全连接;使用ssh进行登陆之前,首先每台机器要生成一个密钥对,即公钥和私钥;例如A要登陆到B,首先需要把A的公钥添加到B的登陆授权文件中,通常是 ~/.ssh/authorized_keys中,这样A才能登陆到B,反过来如果B要登陆到A,同样也需要将B的公钥添加到A的登陆授权文件中;


手动操作

1.生成密钥对

点击(此处)折叠或打开

  1. ssh-keygen -t rsa
2. 拷贝公钥到远程服务器

点击(此处)折叠或打开

  1. ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host
3.最后登陆到远程服务器,无需密码

点击(此处)折叠或打开

  1. ssh user@remote-host
经过以上步骤实现了A免密码登录B,如果B要登陆A同样还需要拷贝公钥,如果一个集群中有好几十台服务器,手动操作就显得非常繁琐;就需要进行批处理解决集群中的任意两台机器之间的相互免密码登录;


批处理方式

思路:首先录入集群中每台服务器的ip,用户名,密码;然后登陆到每台机器,生成密钥对;拷贝远程脚本到各个服务器上,登陆到远程服务器上,并执行ssh-copy-id,两两相互拷贝公钥到对方授权文件中;最后删除远程服务器上的远程脚本;执行脚本主要依赖于expect,需要事先确保每一台机器都有安装expect;

远程脚本,主要实现了拷贝公钥到远程服务器
ssh-id-cp.sh

点击(此处)折叠或打开

  1. #!/bin/bash

  2. # param: host, passwd

  3. function ssh_id_auto_copy(){
  4.     expect << EOF
  5.         spawn ssh-copy-id -i $HOME/.ssh/id_rsa.pub $1
  6.         expect {
  7.             "*yes/no" { send "yes\r"; exp_continue }
  8.             "*assword" { send "$2\r" }
  9.         }
  10.         expect eof
  11. EOF
  12. }

  13. ssh_id_auto_copy $1 $2
批处理脚本scatter-aware.sh

点击(此处)折叠或打开

  1. #!/bin/bash

  2. ###
  3. # Note: Before running this script, please ensure you have installed 'expect' on each system
  4. ###
  5. # check expect command
  6. if ! type "expect" > /dev/null; then
  7.     echo "please intall expect on each matchine in this cluster"
  8.     echo "run: sudo apt-get install expect"
  9.     exit 1
  10. fi

  11. # params: user, host, passwd
  12. function ssh_auto_keygen(){
  13.     expect << EOF
  14.     spawn ssh $1@$2 "ssh-keygen -t rsa"
  15.     expect {
  16.         "yes/no"        { send "yes\r"; exp_continue }
  17.         "*assword" { send "$3\r" ; exp_continue }
  18.         "save the key" { send "\r" ; exp_continue }
  19.         "Overwrite" { send "y\r" ; exp_continue }
  20.         "no passphrase" { send "\r" ; exp_continue }
  21.         "again" { send "\r" ; }
  22.     }
  23.     expect eof;
  24. EOF
  25. }


  26. if [ $( id -u ) -ne 0 ]; then
  27.     echo "Permission denied."
  28.     exit 1
  29. fi


  30. flag="y";
  31. i=0;
  32. while [ $flag != "n" ]
  33. do
  34.     read -p "Enter host:" hosts[$i]
  35.     read -p "Enter ${hosts[$i]}'s user:" users[$i]
  36.     read -p "Enter ${hosts[$i]}'s passwd:" passwds[$i]
  37.     let i++
  38.     read -p "Continue(y/n)" flag
  39.     while [ $flag != "n" ] && [ $flag != "y" ]
  40.     do
  41.         read -p "Continue(y/n)" flag
  42.     done
  43. done

  44. length=$i;

  45. # copy remote script
  46. for (( i=0; i<$length; i++ ))
  47. do
  48.     ssh_auto_keygen ${users[$i]} ${hosts[$i]} ${passwds[$i]}
  49.     expect << EOF
  50.     spawn scp ssh-id-cp.sh ${users[$i]}@${hosts[$i]}:$HOME
  51.     expect {
  52.         "*assword" { send "${passwds[$i]}\r";}
  53.     }
  54.     expect eof;
  55. EOF
  56. done

  57. # copy ssh identify
  58. for (( i=0; i<$length; i++ ))
  59. do
  60.     for (( j=0; j<$length; j++))
  61.     do
  62.         if [ $i -eq $j ]; then
  63.             continue
  64.         fi
  65.         expect << EOF
  66.         spawn ssh ${users[$i]}@${hosts[$i]} "bash $HOME/ssh-id-cp.sh ${hosts[$j]} ${passwds[$j]}"
  67.         expect {
  68.             "*yes/no" { send "yes\r"; exp_continue }
  69.             "*assword" { send "${passwds[$i]}\r" }
  70.         }
  71.         expect eof;
  72. EOF
  73.     done
  74. done

  75. # Clean
  76. for (( i=0; i<$length; i++))
  77. do
  78.     expect << EOF
  79.     spawn ssh ${users[$i]}@${hosts[$i]} "rm -f $HOME/ssh-id-cp.sh"
  80.     expect {
  81.         "*yes/no" { send "yes\r"; exp_continue }
  82.         "*assword" { send "${passwds[$i]}\r" }
  83.     }
  84.     expect eof;
  85. EOF
  86. done

  87. echo "done"
以上仅仅在Ubuntu上实用,脚本写的仓促简陋,欢迎大家补充;

代码位置:







阅读(1323) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~