当前位置:网站首页>Shell脚本配置root免密登录到其他主机
Shell脚本配置root免密登录到其他主机
2022-07-17 05:06:00 【RobertTeacher】
Shell脚本配置root免密登录到其他主机
本篇文章将会指导您使用shell脚本来配置主机间的免密操作。
脚本依赖expect包实现交互操作。
关于expect包安装方法,请参考附录完成安装。
一、shell脚本代码
下面是shell脚本代码
共分三个步骤
一、配置hosts文件,host文件添加免密主机的信息。
二、判断本地的公钥是否存在,如果不存在则需要生成公钥。
三、复制公钥到免密主机上,实现免密操作。
代码如下所示(示例)
#!/bin/bash
# 配置root免密登录到其他主机
# 传递三个参数 1、免密的主机密码 2、免密主机的IP 3、免密主机的主机名称
# @Author Cym
########################
# 一、配置hosts文件
sed -i '/'"$3"'/d' /etc/hosts eval `echo $2 $3 >> /etc/hosts` filepath=~/.ssh/id_rsa.pub # 二、判断本地的公钥是否存在,如果不存在则需要生成公钥 [ ! -f $filepath ] && { ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
}
# 三、实现免密操作
expect -c "set timeout -1; spawn ssh-copy-id $3; expect { *(yes/no)* {send -- yes\r;exp_continue;} *password:* {send -- $1\r;exp_continue;} eof { exit 0;} }";
二、脚本执行结果演示
脚本结果运行如下图所示,expect会自动帮我们完成交互操作。
附录
安装expect
离线安装
离线安装expect需要两个离线包,expect_5.45-2_amd64.deb和tcl8.5_8.5.11-1ubuntu1_amd64.deb,我已经为大家整理完毕放在我的个人空间资源里面,请移步下载。
在线安装
ubuntu系统在线安装使用下面的命令
代码如下所示
apt-get install expect
centOS系统在线安装使用下面的命令
代码如下所示
yum install expect
结语
以上就是Shell脚本配置root免密登录到其他主机的全部内容,喜欢的话点个关注,我会不定期更新技术文章。
边栏推荐
- Wechat applet cloud development and use method-1
- ArcGIS Pro发布服务
- IDL 读取葵花8(Himawari-8)HSD数据
- 【C语言—零基础第十一课】旋转大转盘之指针
- 读论文《SNUNet-CD: A Densely Connected Siamese Network for Change Detection of VHR Images》
- Internship project 2 - Homepage configuration - my data module
- 获取URL参数的两种方法及location对象的各项获取方式
- PAT乙级1017: A除以B
- 数据可视化
- Pat class B 1002: write this number
猜你喜欢

实习项目2-主页配置-我的数据模块

C语言 带你 手撕 通讯录

Uni app conditional compilation ifdef ENDIF compatible with multiple terminals

Excel导入长数据末尾变000

es6新增-数组/对象的解构赋值

小程序云开发 上传图片到云存储

IText modify PDF Text
![Database training 7 [index and creation of data integrity constraints]](/img/7d/2855d945c0d7ffb970634451b600a1.png)
Database training 7 [index and creation of data integrity constraints]

读论文《Learning to Measure Changes: Fully Convolutional Siamese Metric Networks for Scene Change Detec》

Internship project 1 - personalized homepage configuration
随机推荐
【Es6】forEach,for...in ,for...of专栏,让你通过项目案例快速分辨各种for语句的使用方式及区别(完整版)内部有详细注释
【C语言_复习_学习第二课】什么是进制?进制之间应该如何转换
【C语言—零基础_学习_复习_第五课】基本运算符的运算性质
遍历的方法总结
es6新增-字符串部分
实习项目1-个性化主页配置
Es6最新常用知识宝典(能够帮助你解决面试题困惑,编写程序中出现的问题等)
读论文《SNUNet-CD: A Densely Connected Siamese Network for Change Detection of VHR Images》
(elaborate) ES6 remaining parameters, ES6 built-in objects, template string content (detailed example Dictionary) and practical cases of flexible use of the project
Get the multi-functional version of the maximum and minimum values of the internal values of the objects in the array and the full version of the roll call system, and show the effect
小程序云开发 上传图片到云存储
Ucharts chart, pie chart, bar chart and line chart are used in uniapp
PAT乙级1002:写出这个数
Using JS to realize the second level menu of anjuke and the full version (demonstration of precautions and problem points)
markdown笔记以及Typora相关快捷键
小程序editor富文本编辑使用及rich-text解析富文本
PAT乙级1017: A除以B
Pat class B 1017: a divided by B
使用Echars实现水滴状、环形图、分割图、堆叠、组织架构图、地图轮廓等图表
cookie是否有效时间限定?如何设置cookie?手把手教你设置