shell脚本实现秘钥认证

写一个shell脚本,当我们执行时,提示要输入对方的ip和root密码,然后可以自动把本机的公钥增加到对方机器上,从而实现密钥认证。

#!/bin/bash
#这个脚本用来自动配置密钥认证
#作者:猿课-阿铭 www.apelearn.com
#日期:2018-12-14

read -p "输入一个IP地址: " ip
read -p "输入此机器的root密码: " pasd

is_install()
{
    if ! rpm -q $1 &>/dev/null
    then
	yum installl -y $1
    fi
}

is_install openssh-clients
is_install expect

if [ ! -f ~/.ssh/id_rsa.pub ]
then
    echo -e "\n" |ssh-keygen  -P ''
fi

cat > key.expect <<EOF
#!/usr/bin/expect
set host [lindex \$argv 0]
set passwd [lindex \$argv 1]
spawn ssh-copy-id root@\$host
expect {
    "yes/no" { send "yes\r"; exp_continue}
    "password:" { send "\$passwd\r" }
}
expect eof
EOF

chmod a+x key.expect

./key.expect $ip $pasd

发表评论

后才能评论