当前位置: > shell编程 >

shell无密钥脚本

时间:2015-05-31 13:41来源:blog.51cto.com 作者:andyten 的BLOG 举报 点击:

##### 批量部署ssh私钥认证 #####

一、首先安装expect,直接yum即可

二、批量部署ssh私钥脚本

batch_sshkey.sh
==============================================================

#!/bin/bash

cd /root

cat /root/.ssh/id_rsa.pub > /root/.ssh/authorized_keys

for i in `cat ip.txt`
do
ip=$(echo "$i"|cut -f1 -d":")
password=$(echo "$i"|cut -f2 -d":")

 

expect -c "
spawn scp /root/.ssh/authorized_keys /root/remote_operate.sh  root@$ip:/tmp/
        expect {
                \"*yes/no*\" {send \"yes\r\"; exp_continue}
                \"*password*\" {send \"$password\r\"; exp_continue}
                \"*Password*\" {send \"$password\r\";}
        }
"

expect -c "
spawn ssh root@$ip "/tmp/remote_operate.sh"
        expect {
                \"*yes/no*\" {send \"yes\r\"; exp_continue}
                \"*password*\" {send \"$password\r\"; exp_continue}
                \"*Password*\" {send \"$password\r\";}
        }
"

done

============================================================

ip.txt(前面是IP,后面是密码,用冒号:分割)

192.168.8.23:123456
192.168.8.24:456789

============================================================

 remote_operate.sh

#!/bin/bash

if [ ! -d /root/.ssh ];then 
mkdir /root/.ssh
fi
cp /tmp/authorized_keys /root/.ssh/

 

==========================================================

运行batch_sshkey.sh即可。

 

batch_sshkey.sh设置可执行权限,需./执行,执行过程中无需按回车,自动会完成部署。

------分隔线----------------------------
发表评论
为了和诣的生活,我关闭了评论页面,请大家到QQ群里交流吧:348944156,也欢迎关注本站微信公众号:centoscn
栏目分类