批量部署ssh认证


批量部署ssh认证
 
     我想,当搭建hadoop集群的时候大家都会碰到一个问题,手动配置ssh双向认证,当集群规模很小时候还能忍受,但是假如配置几十台乃至上百台节点的时候难道也要手动配置吗? 
     所以推荐用脚本来批量解决这个问题,网络上有不少相关解答,但都不太好用,在此从新记录下。 
 
1,安装所需要的rpm包,tcl-8.4.13-3.ML5.x86_64.rpm,expect-5.43.0-8.el5.x86_64.rpm,我用的linux为redhat6.1 x86_64,经测试没有问题
 
2,脚本代码 
Java代码  
#!/bin/bash    
#2013-09   
#创建本地公钥  
if [ ! -d /root/.ssh ];then  
expect -c "  
spawn ssh-keygen -t dsa  
    expect {  
        \"*key*\" {send \"\r\"; exp_continue}     
                \"*passphrase*\" {send \"\r\"; exp_continue}     
                \"*again*\" {send \"\r\";}     
    }  
"  
fi  
  
ssh-add   ~/.ssh/id_dsa #需要手动加载下私钥  
  
#批量ssh认证建立    
for p in $(cat /root/ip.txt)  #注意ip.txt文件的绝对路径     
do     
ip=$(echo "$p"|cut -f1 -d":")       #取ip.txt文件中的ip地址    
password=$(echo "$p"|cut -f2 -d":") #取ip.txt文件中的密码    
    
#expect自动交互开始    
expect -c "     
spawn ssh-copy-id -i /root/.ssh/id_dsa.pub root@$ip    
        expect {     
                \"*yes/no*\" {send \"yes\r\"; exp_continue}     
                \"*password*\" {send \"$password\r\"; exp_continue}     
                \"*Password*\" {send \"$password\r\";}     
        }     
"     
  
ssh root@$ip '/root/slave_master.sh'  #调用远程脚本  
  
done  
 
 
3,ip.txt 
需要部署的ip+密码 
Java代码  
10.185.224.105:123456  
10.185.224.104:123456  
10.185.224.103:123456  
 
 
4,说明 
对于远程节点上的脚本和本机脚本无太多区别,再部署之前需要用脚本把远程脚本拷贝好

相关内容

    暂无相关文章