In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Today, the editor will bring you an article about shell programming without interaction with expect. The editor thinks it's pretty good, so I'll share it for you as a reference. Let's follow the editor and have a look.
1: expect Overview 1.1expectexpect is a tool based on tcl, and expect is a tool for automatic control and testing. Mainly solve the problem of non-interactive in shell script. It is very helpful for large-scale linux operation and maintenance. In linux operation and development, we often need to log on to the server remotely. The login process is an interactive process, and we may need to enter yes/no, password and other information. To simulate this input, you can use expect script 2: expect installation 2.1 mount CD 2.2 to create a local yum repository 2.3.Execute installation commands yum install expect-y the system does not have expect software installed by default, you need to manually install three: expect basic commands 3.1 spawn: start the process and track subsequent interactions 3.2 expectexpect an internal command Determine whether the specified string is included in the last output, and return immediately if so, otherwise it will be returned after waiting for more time. Can only capture the output of the process started by spawn 3.3Timeout: send a string to the process to simulate the input of the user. The command cannot automatically enter the newline, usually add\ r (enter) 3.4interact: keep interactive after execution, give control to the console 3.5Timeout: specify the timeout, and if it expires, continue to execute the subsequent instructions: seconds timeout-1 is never timeout by default Timeout is the 10s3.6 exp_continue that allows expect to continue to execute the instruction 3.7 send_user echo command, which is equivalent to echo
Note:
Never too often used when the network is unstable to use the continue in the shell script to end this cycle, to start the next loop exp_continue in the expect script will not end, to continue to match the next line 3.8$ argv parameters array expect script can accept parameters passed from bash, can be obtained using the [lindex $argv n] format, n starts at 0, representing the first, second, and third, respectively. The parameter v represents the value value, which is preceded by the fixed format agv with a space between n. 3.9 expect scripts must end with interact or expect eof, and performing automation tasks is usually sufficient for expect eof. Expect eof is waiting for the end flag. The command started by spawn produces an eof tag at the end. Expect eof waits for this tag to see eof, which means the end script IV: expect syntax
Expect "content to capture" {send "parameters to be entered\ r";}
\ r carriage return
If there is no exp_continue, the match will be launched once. If so, execute the match again-the re parameter indicates the match regular expression 5: experct execution mode 5.1 directly executes the path of the binary file [root@localhost ~] # vim a. # setting environment # timeout set timeout 9 / / setting timeout log_file test.log / / log file path log_user 1 / / the number of users is "define variable set hostname [lindex $argv 0] set password [lindex $argv 1] # enable tracking spawnspawn ssh root@$hostname / / spawn startup command # matching condition Capture expect {/ / conditional capture to send to execute "(yes/no)" {send "yes\ r" Exp_continue} "* passwor" {send "$password\ r"} # permissions transferred to the console interact [root @ localhost ~] # chmod + x a.sh [root@localhost ~] #. / a.sh $argv0 $argv1
Don't write interact in curly braces.
[gsy@localhost root] $which expect/usr/bin/expect
Two log can be added or not added.
The two variables set represent passing parameters.
Then use spawn to open the service.
Exprct to capture conditions, intermediate send output instructions,\ r enter, exp_continue continue to match
Interact ends 5.2embed execution [root@localhost ~] # vim b.sh #! / bin/bash hostname=$1 password=$2 / usr/bin/expect
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.