当前位置:网站首页>Shell script configures root to login to other hosts without secret
Shell script configures root to login to other hosts without secret
2022-07-19 05:18:00 【RobertTeacher】
Shell Script configuration root Login to other hosts without password
This article will guide you to use shell Script to configure the secret free operation between hosts .
Scripting depends on expect Package implements interactive operation .
About expect Package installation method , Please refer to the appendix to complete the installation .
One 、shell Script code
Here is shell Script code
There are three steps
One 、 To configure hosts file ,host Add the information of the secret free host to the file .
Two 、 Determine whether the local public key exists , If it does not exist, you need to generate a public key .
3、 ... and 、 Copy the public key to the secret free host , Realize secret free operation .
The code is as follows ( Example )
#!/bin/bash
# To configure root Login to other hosts without password
# Pass three parameters 1、 Password free host 2、 Secret free host IP 3、 The host name of the secret free host
# @Author Cym
########################
# One 、 To configure hosts file
sed -i '/'"$3"'/d' /etc/hosts eval `echo $2 $3 >> /etc/hosts` filepath=~/.ssh/id_rsa.pub # Two 、 Determine whether the local public key exists , If it does not exist, you need to generate a public key [ ! -f $filepath ] && { ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
}
# 3、 ... and 、 Realize secret free operation
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;} }";
Two 、 Demonstration of script execution results
The script results run as shown in the following figure ,expect It will automatically help us complete the interactive operation .
appendix
install expect
Offline installation
Offline installation expect Two offline packages are required ,expect_5.45-2_amd64.deb and tcl8.5_8.5.11-1ubuntu1_amd64.deb, I have sorted it out for you and put it in my personal space resources , Please download .
Online installation
ubuntu The system is installed online with the following commands
The code is as follows
apt-get install expect
centOS The system is installed online with the following commands
The code is as follows
yum install expect
Conclusion
That's all Shell Script configuration root All contents of password free login to other hosts , If you like, pay attention , I will update technical articles from time to time .
边栏推荐
- STL容器——queue与deque的基本操作
- OpenDDS的QoS和自定义QoS(校时TimingQosPolicy)
- PAT乙级1017: A除以B
- 【C语言—零基础_学习_复习_第四课】数据类型及其运算
- 遍历的方法总结
- MapBox 加载本地离线地形
- Continue from the previous issue: the remaining two methods of the rotation chart
- RK356x U-Boot研究所(命令篇)3.4 mem内存相关命令的用法
- STL容器——set集合的应用
- Internship project 2 - Homepage configuration - my data module
猜你喜欢
随机推荐
STL container - basic operation of vector
数据可视化
学习C语言的第五天
uni-app 条件编译#ifdef #endif 兼容多个终端
Wechat applet wx Setclipboarddata copy text
Easypoi excel simple export
Two methods of rotation chart and automatic rotation
小程序云开发表单提交并在页面中获取数据
滚动轮加载的两种js方法及模态框拖拽归总
web3js开发技术
STL container -- basic operation of map
【Es6】利用添加数据,筛选并传输至页面等多项功能实现案例
微信小程序状态栏
【C语言—零基础_学习_复习_第五课】基本运算符的运算性质
(elaborate) ES6 remaining parameters, ES6 built-in objects, template string content (detailed example Dictionary) and practical cases of flexible use of the project
学习C语言的第四天
cookie是否有效时间限定?如何设置cookie?手把手教你设置
指针进阶简单总结
(精讲)Es6 剩余参数,ES6内置对象,模板字符串内容(详例宝典)及灵活运用项目的实战案例
模拟库函数









