当前位置:网站首页>shell实现ssh登录并执行命令
shell实现ssh登录并执行命令
2022-06-10 20:00:00 【Li_XiaoJin】
最近搞灰度发布,机器太多了,四个国家,每个国家两台机器,启停应用很费时间,所有打算写个shell脚本来提高效率(偷懒~)
使用expect可以不用输入密码,避免重复劳动。 那什么是expect? 查了一下,expect是一个免费的编程工具,用来实现自动的交互式任务,而无需人为干预。说白了,expect就是一套用来实现自动交互功能的软件。
在实际工作中,我们运行命令、脚本或程序时,这些命令、脚本或程序都需要从终端输入某些继续运行的指令,而这些输入都需要人为的手工进行。而利用expect,则可以根据程序的提示,模拟标准输入提供给程序,从而实现自动化交互执行。这就是expect!!!真的很好用。
先安装expect
yum install -y expect
touch test.sh chmod 755 test.sh
使用expect时发现执行脚本的时候不能使用sh test.sh 应该用 ./test.sh,因为expect不是bash
ssh 正常是使用 ssh [email protected] "sh /data/uk/script/start-job.sh" 然后还需要输密码等操作
expect + ssh
#!/usr/bin/expect
set timeout 3
spawn ssh [email protected] "sh /data/uk/script/start-job.sh"
expect {
"*yes/no" {send "yes\r"; exp_continue}
"*password:" {send "password\r"}
}
interact
expect 主要是用 send、expect、spawn、interact 四个命令。 send 命令接收一个字符串参数,并将该参数发送到进程。
expect 命令和send命令相反,expect通常用来等待一个进程的反馈,我们根据进程的反馈,再发送对应的交互命令。
spawn 命令用来启动新的进程,spawn后的send和expect命令都是和使用spawn打开的进程进行交互。
interact 命令用的其实不是很多,一般情况下使用spawn、send和expect命令就可以很好的完成我们的任务;但在一些特殊场合下还是需要使用interact命令的,interact命令主要用于退出自动化,进入人工交互。比如我们使用spawn、send和expect命令完成了ftp登陆主机,执行下载文件任务,但是我们希望在文件下载结束以后,仍然可以停留在ftp命令行状态,以便手动的执行后续命令,此时使用interact命令就可以很好的完成这个任务。
到此就完成了,不用一台台机器登录上去执行命令了,一行命令搞定。
Copyright: 采用 知识共享署名4.0 国际许可协议进行许可 Links:https://lixj.fun/archives/shell实现ssh登录并执行命令
边栏推荐
- CVPR 2022 Tsinghua University proposed unsupervised domain generalization (UDG)
- Node (express) implements interfaces such as adding, deleting, modifying, and paging
- KCon 2022 议题大众评选火热进行中!不要错过“心仪”的议题哦~
- P5723 [deep base 4. example 13] prime number pocket
- 蛮力法/1~n个整数中取k个整数
- 魔塔类游戏实现源码及关卡生成
- PDF. JS - - - - JS analyse le fichier PDF pour réaliser l'aperçu et obtenir le contenu du fichier PDF (sous forme de tableau)
- The new audio infinix machine appears in the Google product library, and Tecno CaMon 19 is pre installed with Android 13
- 面试必备——synchronized底层原理的基础知识
- vulnhub-The Planets: Earth
猜你喜欢

Magic tower game implementation source code and level generation

Lengsuanling, a 30-year tortuous history of IPO of a domestic brand

Kcon 2022 topic public selection is hot! Don't miss the topic of "favorite"

An old programmer of about 10 years said: simple crud function enters the era of codeless development 1. Adding, deleting, modifying and checking interface information

自注意力(self-attention)和多头注意力(multi-head attention)

详解三级缓存解决循环依赖

In MySQL basics, MySQL adds an automatically added primary key (or any field) to an existing table

Analysis on rendering principle of mobile terminal

Diablo immortal wiki address Diablo immortal database address sharing

Self attention and multi head attention
随机推荐
农产品期货开户的条件是什么?现在开户的手续费是多少?
Solve the problem that the idea automatically becomes * when there are more than 5 identical packages
torch. nn. Simple understanding of parameter / / to be continued. Let me understand this paragraph
Talk about server performance optimization ~ (recommended Collection)
冷酸灵,一个国产品牌IPO的30年曲折史
终于有人说清楚了Cookie、Session、Token的区别。详解,面试题。
解决idea超过5个相同包的时候自动变成*的问题
Finally, someone explained the difference among cookies, sessions and tokens. Detailed explanation, interview questions.
观点丨Play and Earn 会让加密游戏误入歧途
用一个性能提升了666倍的小案例说明在TiDB中正确使用索引的重要性
The old programmer said: stop translating the world, developers should return to programming
Knowledge map / relationship visualization
Four methods to obtain the position index of the first n values of the maximum and minimum values in the list
知识图谱/关系可视化
分布式服务理论基础
Construction of RT thread smart win10 64 bit compilation environment
堆排序与加强堆代码 用于记忆
LeetCode:1037. Effective boomerang - simple
Microsoft Word 教程,如何在 Word 中更改页面方向、为页面添加边框?
自定义日期组件,左右按钮控制向前或向后翻年、翻月、翻周、翻日