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--
Overview of Expect
Expect 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, which may require input (yes/no) password and other information. To simulate this input, you can use an Expect script.
Expect installation
Yum install expect-y
Basic command send:
Sends a string to the process to simulate the user's input. This command cannot enter and wrap automatically. It is generally necessary to add\ r (enter).
Expect:
An internal command of expect to determine whether the specified string was included in the last output, and return immediately if so, otherwise it will return after waiting for the timeout. Only the output of processes started by spawn can be captured.
Spawn:
Start the process and track subsequent interactions.
Interact:
Maintain the interactive state after the completion of the execution, and hand over the control to the console.
Timeout:
Specify the timeout period, and continue to execute subsequent instructions when it expires.
Unit is: seconds
Timeout-1 for never timeout
By default, timeout is 10 seconds.
Exp_ continue:
Allow expect to continue to execute instructions down
Send_ user:
Echo command, equivalent to echo
$argv parameter array:
The Expect script can accept parameters passed from bash, which can be obtained using [lindex $argv n], where n starts at 0, representing the first, the second, and the third, respectively. Parameters.
Expect eof:
The Expect script must end with interact or expect eof, expect eof is usually enough to perform automation tasks, and expect eof is waiting for the end flag. Commands initiated by spawn end with an eof tag that expect eof is waiting for.
Expect syntax single branch statement
Expect "password:" {send "mypassword\ r";}
Multi-branch pattern syntax expect "aaa" {send "AAA\ r"} expect "aaa" {send "AAA\ r"} expect "aaa" {send "AAA\ r"}
The send command does not have the function of enter and line feed, and usually adds\ r or\ n.
Expect {"aaa" {send "AAA\ r"} "bbb" {send "BBB\ r"} "ccc" {send "CCC\ r"}
As long as any one is matched, execute the item's hard send statement and exit the expect statement.
Expect {"aaa" {send "AAA\ r"; exp_continue} "bbb" {send "BBB\ r"; exp_continue} "ccc" {send "CCC\ r"}}
Exp_continue means to continue with the following matches, and if the aaa is matched, continue to match the bbb down after the send statement is executed.
Expect execution is executed directly:
Direct interaction-free ssh login by executing scripts
[root@localhost opt] # vim demo01.sh #! / usr/bin/expect / / Expect binary file path set timeout 20 / / timeout 20 Unit: log_file test.log / / log file log_user 1 / / number of log users set hostname [lindex $argv 0] set password [lindex $argv 1] / / define variables, and pass content through parameters Similar to the location variable spawn ssh root@$hostname / / tracking command expect {"Connection refused" exit / / captures service denial information and executes exit exit interaction-free "service not konwn" exit / / to capture unknown services or connection address errors And execute exit exit interaction-free "(yes/no)" / / capture the information {send "yes\ r" that asks if you are connected. Exp_continue} / / enter "yes", enter, and continue to match "* password" / / capture the password entered {send "$password\ r"} / / enter the password through the variable, enter, and do not continue to match. } interact / / maintain the interactive state and hand over the control to the console for embedded execution:
Create a user through a script, and then set a password for the user without interaction.
[root@localhost opt] # vim demoo 02. The location variable $1 is userpassword=$2 / / defines the location variable $2 as passworduseradd $user / / add user / 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.