当前位置:网站首页>Shell implements SSH login and executes commands
Shell implements SSH login and executes commands
2022-06-10 21:23:00 【Li_ XiaoJin】
Recently, we launched the grayscale release , There are too many machines , Four countries , Two machines per country , Starting and stopping applications takes time , All plan to write a shell Scripts to improve efficiency ( lazy ~)
Use expect You don't have to enter a password , Avoid duplication of effort . What is expect? Checked the ,expect It's a free programming tool , Used to implement automatic interactive tasks , Without human intervention . To put it bluntly ,expect It's a set of software to realize the automatic interaction function .
In practice , We run the command 、 When a script or program , These orders 、 Scripts or programs need to input some instructions to continue running from the terminal , And these inputs need to be done manually . And the use of expect, You can follow the prompts of the program , Analog standard input is provided to the program , So as to realize automatic interactive execution . This is it. expect!!! It really works .
Install first expect
yum install -y expect
touch test.sh chmod 755 test.sh
Use expect It is found that... Cannot be used when executing scripts sh test.sh Should use the ./test.sh, because expect No bash
ssh It's normal to use ssh [email protected] "sh /data/uk/script/start-job.sh" Then you need to input the password and other operations
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 Mainly used send、expect、spawn、interact Four orders . send The command receives a string parameter , And send the parameter to the process .
expect Command and send The order is the opposite ,expect Usually used to wait for feedback from a process , We're based on feedback from the process , Then send the corresponding interactive command .
spawn The command is used to start a new process ,spawn After send and expect Commands are and use spawn Open processes interact .
interact Not a lot of commands are used , In general use spawn、send and expect Orders can do our job well ; But in some special occasions, you still need to use interact Ordered ,interact The command is mainly used to exit automation , Enter human interaction . For example, we use spawn、send and expect The order is complete ftp Landing host , Perform the download file task , But we hope that after the file download , You can still stay in ftp Command line status , So that you can execute the following commands manually , At this time to use interact Command can complete this task well .
That's it , You don't have to log in to execute commands from one machine to another , One line of command .
Copyright: use Creative Commons signature 4.0 International license agreement to license Links:https://lixj.fun/archives/shell Realization ssh Log in and execute commands
边栏推荐
- LeetCode:497. Random points in non overlapping rectangles -- medium
- "O & M youxiaodeng" self service account unlocking tool
- CET-6 - Business English - the last recitation before the test
- 自媒体视频创作如何选择领域?
- Introduction to database system -- Chapter 1 -- Introduction (important knowledge points)
- App test case
- Fast Planner - detailed explanation of kinetic astar
- The programmed navigation route jumps to the current route (the parameters remain unchanged), and the navigationduplicated warning error will be thrown if it is executed multiple times?
- shell实现ssh登录并执行命令
- How to use Diablo immortal database
猜你喜欢

分布式服务理论基础

Meetup Preview: introduction to the new version of linkis and the application practice of DSS

聊聊服务器性能优化~(建议收藏)

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

^30H5 Web Worker多线程

"O & M youxiaodeng" self service account unlocking tool

魔塔类游戏实现源码及关卡生成

pytorch深度学习——神经网络卷积层Conv2d

Can you still have a wonderful life if you are laid off at the age of 35?

【生成对抗网络学习 其一】经典GAN与其存在的问题和相关改进
随机推荐
Video monitoring system storage control, bandwidth calculation method
Error code 1129, state HY000, host 'xxx' is blocked because of many connection errors
Leetcode advanced path - the first unique character in a string
app測試用例
leetcode 划分数组使最大差为 K
Pytorch deep learning -- neural network convolution layer conv2d
Portable FDW framework for Pb
ros虚拟时间
LeetCode 进阶之路 - 167.两数之和 II - 输入有序数组
Record today's MySQL failures
Vissim仿真快速入门
冷酸灵,一个国产品牌IPO的30年曲折史
Connexion MySQL errorcode 1129, State hy000, Host 'xxx' is Blocked because of many Connection Errors
Codeforces Round #798 (Div. 2)
LeetCode 进阶之路 - 删除排序数组中的重复项
^29 event cycle model
Leetcode advanced road - 167 Sum of two numbers II - input ordered array
Unity analyzes the rendering of built-in terrain and does some interesting things
Practical | how to use burp suite for password blasting!
You have to learn math to play art?