私钥认证脚本

#!/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

batch_sshkey.sh 该脚本会调用ip.txt文件,做ssh私钥认证。

按下面的格式把客户端机器ip写入ip.txt文件里:

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



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

remote_operate.sh这个是用来把认证文件复制到客户端里的。最终实现远程登录不用户手工输入密码。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章