当前位置:网站首页>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

原网站

版权声明
本文为[Li_ XiaoJin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101959594403.html