shell批次傳輸金鑰

2020-08-11 19:20:09

shell寫的批次傳輸金鑰

#!/bin/bash
if [ -f ~/.ssh/id_rsa ];then
  echo yes
else
  expect << EOF
  set timeout 300
  spawn ssh-keygen
  expect ":"
  send "\n"
  expect ":"
  send "\n"
  expect "n"
  send "\n"
  expect eof
EOF
fi

iipp=($(cat $1|awk '{print $1}'))
pawd=($(cat $1|awk '{print $2}'))

for ((i=0;i<=${#iipp[*]};i++))
do
  expect <<EOF
  set timeout 300
  spawn  ssh-copy-id ${iipp[$i]}
  expect {
    "yes/no" { send "yes\n"; exp_continue }
    "password:" { send "${pawd[$i]}\n"}
  }
  expect eof
EOF
done

IP檔案(ip 密碼)
cat ip.txt
222.222.222.222 123456
223.223.223.223 123456

執行指令碼
sh ssh-copy-id.sh ip.txt