实现免密登陆脚本, expect登陆远程主机,将生成的密钥写入到目标主机, expect测试远程登陆
#设置输出字体颜色
RED="echo -e \E[1;31m"
GREEN="echo -e \E[1;32m"
END="\E[0m"
#设置ssh免密钥登录
function ssh_expect(){
NET=192.168.177
user=root
password=admin@123
IPLIST="
134
135
136
"
for ID in $IPLIST ;do
ip=$NET.$ID
expect <<EOF
set timeout 20
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" {send "$password\n" }
}
expect "#" { send "exit\n" }
expect eof
EOF
done
}
function os_type(){
if grep -i -q ubuntu /etc/os-release;then
echo ubuntu
elif grep -i -q centos /etc/os-release;then
echo centos
else
echo "系统不支持"
fi
}
function install_mysql(){
if [ `os_type` = centos ] ;then
yum install mysql -y
elif [ `os_type` = ubuntu ] ;then
apt install mysql -y
else
echo "系统不支持"
fi
${GREEN}安装成功$END
}
function install_httpd(){
if [ `os_type` = centos ] ;then
yum install httpd -y
elif [ `os_type` = ubuntu ] ;then
apt install apache2 -y
else
echo "系统不支持"
fi
${GREEN}安装成功$END
}
PS3="请选择功能(1-4): "
select menu in 安装mysql 安装apache 免密钥登录主机 退出;do
case $REPLY in
1)
install_mysql
;;
2)
install_httpd
;;
3)
ssh_expect
;;
4)
break
;;
*)
echo "输出错误"
esac
done