#!/bin/bash
#变量
username=$1
hostname=$2
password=$3
# 1. 生成密钥
if [ -f /root/.ssh/id_rsa ] || [ -f /root/.ssh/id_rsa.pub ];then
echo "当地存在密钥对,现在进行清理并重新生成"
rm -rf /root/.ssh/id_rsa /root/.ssh/id_rsa.pub &>/dev/null && echo "清理成功"
sleep 3
fi
echo "生成密钥对...."
expect <<-EOF
spawn ssh-keygen
expect "(/root/.ssh/id_rsa):"
send "\r"
expect "(empty for no passphrase):"
send "\r"
expect "again:"
send "\r"
expect eof;
EOF
# 2. 部署公钥(输入一次密码)
expect <<EOF
spawn ssh-copy-id ${username}@${hostname}
expect {
"*(yes/no)*" {send "yes\r";exp_continue}
"password" {send "$password\r"}
}
spawn ssh $username@$hostname
sleep 3
expect "*]#"
send "ip addr show\r"
expect "*]#"
send "exit\r"
expect eof;
EOF


6536

被折叠的 条评论
为什么被折叠?



