当前位置:网站首页>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
边栏推荐
- View play and earn will lead crypto games astray
- 在YUV图像上根据背景色实现OSD反色
- 自注意力(self-attention)和多头注意力(multi-head attention)
- What is the difference between localhost and 127.0.0.1?
- Serial Print() and serial The difference of write() function, and the problem of hexadecimal and string sending and receiving format in serial port communication and detailed explanation of the conver
- 记录一下今天的MySQL故障
- Portable FDW framework for Pb
- Understanding deep learning attention
- 魔塔类游戏实现源码及关卡生成
- Redis cache avalanche
猜你喜欢

设计多层PCB板需要注意哪些事项?

Pytorch deep learning -- convolution operation and code examples

Redis cache breakdown

^29 event cycle model

1、 Vulkan develops theoretical fundamentals

72. 编辑距离 ●●●

Game compatibility test (general scheme)

app测试用例

Quick start to elastic job, three minutes to experience distributed scheduled tasks

魔塔类游戏实现源码及关卡生成
随机推荐
LeetCode 进阶之路 - 反转字符串
Connexion MySQL errorcode 1129, State hy000, Host 'xxx' is Blocked because of many Connection Errors
Redis缓存雪崩
「运维有小邓」自助帐户解锁工具
MySQL service startup failed
在手机上买基金安全吗?会不会被吞本金?
Nodejs: official document 3 Dgram stream
游戏兼容性测试(通用方案)
Fast Planner - detailed explanation of kinetic astar
微积分复习1
LeetCode 进阶之路 - 169.多数元素
Leetcode advanced road - 169 Most elements
Leetcode advanced road - plus one
20192407 2021-2022-2 experimental report on Experiment 8 of network and system attack and Defense Technology
H.264中NALU、RBSP、SODB的关系
Identity and access management (IAM)
Leetcode advanced road - 167 Sum of two numbers II - input ordered array
LeetCode:497. 非重叠矩形中的随机点————中等
MySQL Basics
H265 Nalu type judgment and SPS data analysis