

Shell脚本自动化配置SSH免密登录和取消SSH免密配置脚本
source link: https://blog.51cto.com/u_15538119/5518663
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

2.实现代码
#!/bin/bash
#Author:cosann
#date:2022/7/21
#description:批量部署SSH免密登录脚本
E_ERROR=65
#传参检测
if [ $# -ne 1 ]
then
echo -e "Usage:$0 ip_list_file "
exit E_ERROR
fi
#初始化
ip_list_file=$1
#从文本读取值初始化变量
ip_address=(`awk '{print $1}' $ip_list_file`)
username=(`awk '{print $2}' $ip_list_file`)
password=(`awk '{print $3}' $ip_list_file`)
#环境配置
rpm -qa | grep "expect" &> /dev/null
if [ $? -ne "0" ]
then
yum install -y expect &> /dev/null || ( echo -e "部署必须组件Expect失败,请检查Yum配置" && exit E_ERROR )
fi
#打印菜单
echo -e "\n==============================================="
echo -e "该脚本可以实现批量部署和删除SSH免密配置"
echo -e "Author:Cosann"
echo -e "Create time:2022/07/27"
echo -e "==============================================="
while :
do
echo -e ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo -e "1. 配置SHH免密登录"
echo -e "2. 取消SHH免密配置"
echo -e "3. 退出程序"
#功能控制及实现
read -p "请输入序号>>> " nu
if [[ "$nu" == "3" ]]
then
#退出程序
echo -e "\n###!!!感谢使用,再见!!!###"
exit 0
elif [[ "$nu" == "1" ]]
then
echo "开始推送"
#检测公钥文件是否存在,不存在则生成
if ! [ -e "/root/.ssh/id_rsa.pub" ];then
ssh-keygen -t rsa -F "" -f /root/.ssh/id_rsa
else
echo -e "已创建公钥文件,开始向远端服务器推送公钥"
fi
#循环控制
count=`grep -v '^$' $ip_list_file | wc -l `
for (( i=0;i<$count;i++ ))
do
#echo -e "${ip_address[$i]}\t${username[$i]}\t${password[$i]}"
#自动化交互实现推送ssh公钥
/usr/bin/expect<<-EOF
spawn ssh-copy-id -i $HOME/.ssh/id_rsa.pub ${username[$i]}@${ip_address[$i]}
expect {
"*yes/no" { send "yes\r";exp_continue }
"*password" { send "${password[$i]}\r" }
}
expect eof
EOF
done
echo -e "--------------------------------------------------------------------------------------"
echo -e "--------------------------------------------------------------------------------------"
echo -e "--------------------------------------------------------------------------------------"
echo -e "###推送完成,尝试免密登录###"
#推送公钥成功免密结果通知
for (( i=0;i<$count;i++ ))
do
/usr/bin/expect<<-EOF
spawn ssh ${username[$i]}@${ip_address[$i]}
expect "*]#"
send "echo "##登录成功##"\r"
expect "*]#"
send "exit\r"
EOF
done
echo -e "-------------------------------------------"
echo -e "已完成SHH免密配置,请尝试SHH登录远端主机确认"
elif [[ "$nu" == "2" ]]
then
#自动化交互实现删除配置免密的远程主机上的authorized_keys
count=`grep -v '^$' $ip_list_file | wc -l `
for (( i=0;i<$count;i++ ))
do
/usr/bin/expect<<-EOF
spawn ssh ${username[$i]}@${ip_address[$i]}
expect "*]#"
send "rm -f /root/.ssh/authorized_keys 2> /dev/null\r"
expect "*]#"
send "exit\r"
EOF
done
echo -e "-------------------------------------------"
echo -e "已取消SHH免密配置,请尝试SHH登录远端主机确认"
else
echo -e "\033[41;37m 非法输入,请检查输入!!! \033[0m"
fi
done
</div
Recommend
-
17
群晖 SSH 公钥免密登录 首先 SSH 登录群晖,检测群晖当前用户主目录下是否有 .ssh 文件夹,如果没有使用下列命令创建: mkdir ~/.ssh 2. 使用 vim ~/.ssh/authorize...
-
9
北京工业大学校园网自动登录shell脚本 运动会没课,正好有时间,用shell脚本重写了一下之前的python自动登录脚本,这次写死了一些在北工大校园网内不会变的值,比原来完全py...
-
7
shell脚本使用expect自动化交互登录远程主机进行批量关机 原创 1.目标主机登录用户都为root,...
-
10
一个 ssh 设置免密登陆脚本 2022-09-24 tech
-
9
Linux 下配置 hosts 并设置免密登录 作者:Grey 原文地址:
-
30
Abp.Zero 手机号免密登录验证与号码绑定功能的实现(一):验证码模块 ...
-
8
两台服务器实现免密登录 可以用于配置多台(>2)服务器之间的免密登录 使用shell的远程操作命令ssh,通过ssh协议,连接192.168.100.148服务器,执行echo 1命令。
-
14
配置 vscode 远程开发,免密登录(处理 IdentityFile 失效的问题) Published on Apr 4, 2023 in 前端 with
-
16
快速添加SSH公钥进行免密登录 Linux运维 / 2023年11月23日 / 1...
-
12
MacOS Sonoma14.2.1系统SSH免密登录 摘要:MacOS...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK