1. 程式人生 > >shell 腳本交互模式expect批量上傳ssh密鑰

shell 腳本交互模式expect批量上傳ssh密鑰

end copy 密鑰 地址 usr shell 獲取IP地址 rsa 腳本交互

#!/usr/bin/bash
###批量上傳密鑰

V.1.0 by chenght 2019-03-29

password=12345qwert
for i in {2..254} ######批量獲取ip地址
do
{
ip=192.28.1.$i
ping -c1 -W1 $ip &>/dev/null ####記錄成功的ip
if [ $? = 0 ];then
echo "$ip">>ip.txt

EOF 交互內容容易出錯,請註意
   /usr/bin/expect <<-EOF
   set timeout 10
   spawn ssh-copy-id -i /root/.ssh/id_rsa.pub -p 10022 admin@$ip   ##上傳的密鑰寫全路徑#
   expect {
           "yes/no" { send "yes\r";exp_continue }
            "password:" { send "$password\r" }
          }
   expect eof

EOF
fi
}&
done
wait
echo "fininsh.."

shell 腳本交互模式expect批量上傳ssh密鑰