#!/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私鑰認證。bash
按下面的格式把客戶端機器ip寫入ip.txt文件裏:ssh
ip.txt(前面是IP,後面是密碼,用冒號分割。)ide
!#/bin/bash if [! -d /root/.ssh ];then mkdir /root/.ssh fi cp /tmp/authorized_keys /root/.ssh/
remote_operate.sh這個是用來把認證文件複製到客戶端裏的。最終實現遠程登陸不用戶手工輸入密碼。
spa