私鑰認證腳本

#!/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這個是用來把認證文件複製到客戶端裏的。最終實現遠程登錄不用戶手工輸入密碼。


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