使用使用shell脚本实现ssh免密登录
目录
1.expect命令:可以在外输入,某些命令执行后弹出的输入提示
1.expect命令:可以在外输入,某些命令执行后弹出的输入提示
-
spawn shell 命令程序
-
expect "捕获到shell 命令程序执行之后输出的字符串"
-
send "发送给 shell 命令程序的字符串"
-
[root@localhost .ssh]# ssh-keygen -t rsa -b 1024
-
Generating public/private rsa key pair.
-
Enter file in which to save the key (/root/.ssh/id_rsa):
-
/root/.ssh/id_rsa already exists.
-
Overwrite (y/n)?
-
/usr/bin/expect << eof
-
-
spawn ssh-keygen -t rsa -b 1024
-
-
# 开始进连续捕获
-
# 获取“Overwrite (y/n)?”的提示行,输入“y”表示同意(其中\n是换行符)
-
expect {
-
".ssh/id_rsa)" { send "\n"; }
-
"Overwrite (y/n)?" { send "y\n"; }
-
}
-
# 结束捕获
-
eof
2.使用expect命令来创建本地的公钥文件
-
create_ssh_pub(){
-
echo "生成本地ssh公钥"
-
/usr/bin/expect << eof
-
# 设置捕获字符串后,期待回复的超时时间
-
set timeout 30
-
-
spawn ssh-keygen -t rsa -b 1024
-
-
## 开始进连续捕获
-
expect {
-
".ssh/id_rsa)" { send "\n"; exp_continue }
-
"Overwrite (y/n)?" { send "y\n"; exp_continue }
-
"no passphrase):" { send "\n"; exp_continue }
-
"passphrase again:" { send "\n"; exp_continue }
-
}
-
eof
-
}
-
if [ ! -f /root/.ssh/id_rsa.pub ];then
-
create_ssh_pub
-
fi
3.复制ssh的公钥文件到各个主机上:
我的shell脚本在本机master上
-
# 定义复制ssh公钥方法
-
copy_ssh(){
-
echo "复制公钥到对应的主机上"
-
/usr/bin/expect << eof
-
# 设置捕获字符串后,期待回复的超时时间
-
set timeout 30
-
-
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $1@$2
-
-
## 开始进连续捕获
-
expect {
-
"connecting (yes/no)?" { send "yes\n"; exp_continue }
-
"s password:" { send "${ssh_passwd}\n"; exp_continue }
-
}
-
eof
-
}
-
for name in ${ssh_networkname[*]};do
-
timeout 5 ssh root@${name} "echo ${name}: 'This is success!'"
-
if [[ $? -ne 0 ]];then
-
echo "复制文件到: ${name}"
-
copy_ssh root ${name} > /dev/null
-
fi
-
-
done
4.完整的shell脚本:
-
#! /bin/bash
-
-
ssh_hosts=(192.168.178.101 192.168.178.151 192.168.178.201)
-
ssh_networkname=(master node1 node2)
-
ssh_passwd=110119
-
-
# 定义修改/etc/hosts文件的方法
-
echo "开始修改地址映射"
-
-
for ((i=0;i<${#ssh_hosts[*]};i ));do
-
sed -i '/^'"${ssh_hosts[$i]}"'/d' /etc/hosts
-
echo "尝试连接: ${ssh_networkname[$i]}"
-
timeout 5 ssh root@${ssh_networkname[$i]} "echo ${ssh_networkname[$i]}: 'This is success!'"
-
if [[ $? -ne 0 ]];then
-
echo "添加地址映射:${ssh_hosts[$i]} ${ssh_networkname[$i]}"
-
echo "${ssh_hosts[$i]} ${ssh_networkname[$i]}" >> /etc/hosts
-
fi
-
done
-
-
expect -v
-
if [ `echo $?` -ne 0 ];then
-
echo "安装expect命令"
-
yum install -y expect
-
fi
-
create_ssh_pub(){
-
echo "生成本地ssh公钥"
-
/usr/bin/expect << eof
-
# 设置捕获字符串后,期待回复的超时时间
-
set timeout 30
-
-
spawn ssh-keygen -t rsa -b 1024
-
-
## 开始进连续捕获
-
expect {
-
".ssh/id_rsa)" { send "\n"; exp_continue }
-
"Overwrite (y/n)?" { send "y\n"; exp_continue }
-
"no passphrase):" { send "\n"; exp_continue }
-
"passphrase again:" { send "\n"; exp_continue }
-
}
-
eof
-
}
-
if [ ! -f /root/.ssh/id_rsa.pub ];then
-
create_ssh_pub
-
fi
-
-
-
# 定义复制ssh公钥方法
-
copy_ssh(){
-
echo "复制公钥到对应的主机上"
-
/usr/bin/expect << eof
-
# 设置捕获字符串后,期待回复的超时时间
-
set timeout 30
-
-
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $1@$2
-
-
## 开始进连续捕获
-
expect {
-
"connecting (yes/no)?" { send "yes\n"; exp_continue }
-
"s password:" { send "${ssh_passwd}\n"; exp_continue }
-
}
-
eof
-
}
-
for name in ${ssh_networkname[*]};do
-
timeout 5 ssh root@${name} "echo ${name}: 'This is success!'"
-
if [[ $? -ne 0 ]];then
-
echo "复制文件到: ${name}"
-
copy_ssh root ${name} > /dev/null
-
fi
-
-
done
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfccbja
系列文章
更多
同类精品
更多
-
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24 -
微信小程序没声音怎么办
PHP中文网 06-15 -
微信提示登录环境异常是什么意思原因
PHP中文网 04-09 -
excel图片置于文字下方的方法
PHP中文网 06-27 -
使用云服务器搭建个人游戏加速器教程
AuroraJay 07-06 -
微信运动停用后别人还能看到步数吗
PHP中文网 07-22 -
微信人名旁边有个图标有什么用
PHP中文网 03-11 -
抖音国际版要用什么加速器能流畅刷Tiktok的加速器
TK小达人 08-02 -
Excel筛选和排序是灰色的怎么办
PHP中文网 06-22 -
键盘上的负数是哪个键
PHP中文网 03-17