In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Sometimes we need to send ssh commands to the server in batches, but there may be some servers that are newly joined and have not yet been configured with ssh secret-free. At this time, we will be prompted to enter yes/no or password and so on. The expect script command is used to automatically type the corresponding text for us when prompted for this.
Expect script
First, look at a shell script to realize the automatic connection of ssh.
#! / usr/bin/expect spawn ssh 192.168.1.241expect "password" send "123456\ r" expect "] #" {send "ls-la\ r"} interact
Note that the first line uses #! / usr/bin/expect instead of the normal bash script
Spawn is used to start a new process.
Expect "password". Note that this is an internal command in the expect script, which is used to wait for process feedback. It can accept strings and regular expressions. This means that the output of the process started by spawn is regarded as the input of the expect command. If the password script is included, it will output 123456\ r to the standard output.
Note that\ r represents a line break, which is the same as the line break after we enter a command.
Send: send interaction values instead of entering them manually
Then we enter the server of 241and the window will output [root@hadoop01 ~] #, which matches our] #, so we output the ls-la command to the console and wrap. Here we have curly braces after our expect, which is consistent with the effect written on two lines above. It can be understood as another way of writing.
The role of interact is very special, which means waiting for the end of the spawn command and staying at the server to continue interaction. without this, it is possible that the send corresponding to the expect is over before the send is executed. We can use expect eof instead of interact to exit after waiting for the end of spawn (eof will be sent to expect after the end of spawn process)
Multi-branch syntax
The above expect belongs to the single branch pattern, which represents a match. If the match is not, you have to type it yourself, but we may have a different prompt the first time, and you need to use multi-branch syntax at this time.
Expect will be output as long as it matches to either aaa or password.
#! / usr/bin/expectspawn ssh 192.168.1.241expect {"aaa" {send "bbb\ r"} "password" {send "nf123456\ r"}} expect "] #" {send "ls-la\ r"} interact
Expect command line arguments
The above expect script functions may have been achieved, to avoid having a lot of expect scripts, I prefer to use the expect command
#! / bin/bashSERVER= "192.168.1.241" PASSWD=nf123456expect-c "set timeout-1; spawn ssh $SERVER; expect {\" yes/no\ "{send\" yes\ r\ "; exp_contine;}\" password:\ "{send\" $PASSWD\ r\ "; expect\"] #\ "{send\" ls-la\ r\ "} Expect\ "] #\" {send\ "exit\ r\"}; expect eof; "
The above shell function is the same as that of the expect script, which is entered after logging in through ssh, the ls-la command
Set timeout-1 sets the timeout
Expect needs to be followed by-c
The expect command is enclosed in double quotation marks, which should be noted
If there is a "need to use\" escape.
Line 15 represents the end of the ls-la command to send an exit command, which generally needs to be added to prevent blocking
End of expect eof matching spawn
Ssh batch secret-free demo
#! / bin/bashSERVERS= "192.168.1.241 192.168.1.242" PASSWD= "123456" function sshcopyid {expect-c "set timeout-1; spawn ssh-copy-id $1; expect {\" yes/no\ "{send\" yes\ r\ "; exp_contine;}\" password:\ "{send\" $PASSWD\ r\ "; exp_continue;}} Expect eof; "} for server in $SERVERSdo sshcopyid $serverdone
The above is the whole process of the expect command to achieve batch ssh secret-free under linux. I hope it can help you.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.