当前位置:网站首页>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免密登录到其他主机的全部内容,喜欢的话点个关注,我会不定期更新技术文章。
边栏推荐
- (精讲)Es6 剩余参数,ES6内置对象,模板字符串内容(详例宝典)及灵活运用项目的实战案例
- [2022 10th Teddy Cup Challenge] Title A: complete version of pest identification (general idea. Detailed process and code and results CSV in compressed package)
- Applet editor rich text editing and rich text parsing
- Interface parameters return encapsulated class result
- 实习项目1-个性化主页配置
- Excel导入长数据末尾变000
- H5页面使用js生成二维码
- Convolutional neural network
- 滚动轮加载的两种js方法及模态框拖拽归总
- Mysql database experiment training 6, data view (detailed)
猜你喜欢

ThreadLocal thread safety example and its principle

Baidu map realizes thermal map

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

Uni app conditional compilation ifdef ENDIF compatible with multiple terminals

数据可视化

Internship project 2 - Homepage configuration - my data module

Hire the server, and the pytorch environment training yolov5 model tutorial deployed on pycharm professional edition. Server environment installation library file:

Ucharts chart, pie chart, bar chart and line chart are used in uniapp

CityEngine 三维管道建模教程

UML(用例图,类图,对象图,包图)
随机推荐
Email (including attachments, Netease, QQ)
【C语言—零基础第十四课】变量的作用域与存储类
vlookup函数的使用方法及实例
【Es6】forEach,for... in ,for... Of column, which allows you to quickly distinguish the usage and differences of various for statements through project cases (full version). There are detailed notes ins
Hire the server, and the pytorch environment training yolov5 model tutorial deployed on pycharm professional edition. Server environment installation library file:
Wechat applet obtains the week, morning, noon and evening of month, year and day
获取URL参数的两种方法及location对象的各项获取方式
Bi design: distributed high concurrency epidemic prevention health management system based on vue+socket+redis
微信小程序状态栏
Uni app conditional compilation ifdef ENDIF compatible with multiple terminals
数据可视化
H5如何获取内网IP和公网IP
Internship project 2 - Homepage configuration - my data module
[ES6] quickly print user information to the page
微信小程序获取年月日周及早上、中午、晚上
es6新增-数组部分
小程序云开发 上传图片到云存储
实习项目3-更改所有者
Actual cases of data analysis and data mining local house price prediction (716):
IDL 读取葵花8(Himawari-8)HSD数据