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/03 Report--
Expect interaction-free programming in Shll
Overview of 1.Expect
2.Expect installation (underlying language Tcl)
3.Expect basic command
4.Expect execution mode
5.Expect case
Expect Overview:
1.Expect is a tool based on TCL language, 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.
two。 In linux operation and development, we often need to log on to the server remotely. The login process is an interactive process, and Yuyao may enter yes/no password and other information. To simulate this input, you can use an Expect script.
Expect installation
1. Mount the CD
two。 Make a local YUM source
Execute the installation command: yum-y intall expect
Basic command: 1.send: sends a string to the process to simulate the user's input
This command cannot automatically enter to change the line, and usually add\ r (enter).
2.expect
An internal command of expect determines whether the specified string is included in the last output, and returns it immediately if so, otherwise it will return after waiting for the timeout.
You can only capture the output of processes started by spawn
# 3.spawn: start the process and track subsequent interactions
4.interact: maintain the interactive state after the execution is completed, and transfer control to the console 5.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
6.exp_continue
Allow expect to continue to execute instructions down
7.send_user:
Echo command, equivalent to echo
8.$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 and represents the first, second, and third, respectively. Parameters.
9.Expect scripts must end with interact or expect ecof, and expect eof is usually enough to perform automated tasks
Expect eof is really waiting for the end sign. Commands initiated by spawn end with an eof tag that expect eof is waiting for.
Expect execution method: directly execute [root@ce~] # more a.sheme "use argv" / / Expect binary path set timeout 60 / / 60 seconds wait time log_file test.log / / log file log_user 1 / / log user set hostname [log $argv 0] / / append parameter 0, statistical variables Load first position set password [lindex $argv 1] / / append parameter 1, statistical variable, load second position spawn ssh root@$hostname expect {/ / to match "(yes/no)" {send "yes/r" Exp_continue} / / enter yes Continue to execute the "* password" / / parameter 2 {send "$password\ r"} / / give it to the console to enter} interact [root@ce~] # chmod astatx a.sh [root@ce~] #. / a.shDemo1: remote login [root@localhost opt] # yum install expect-y / / installation service [root@localhost opt] # vim a.shallows # timeout set timeout 20log_file test.loglog_user timeout parameter passed in set hostname [lindex $argv 0] set password [lindex $argv 1] # tracking command spawn ssh root@$hostname# captures information and matches Interactive execution-free expect {"(yes/no)" {send "yes\ r" Exp_continue} "* password" {send "$password\ r"} # Control is transferred to the console to execute interact [root@localhost opt] # chmod + x a.sh [root@localhost opt] #. / a.sh 192.168.56.129 123123spawn ssh root@192.168.56.129The authenticity of host '192.168.56.129 (192.168.56.129)' can't be established.ECDSA key fingerprint is SHA256:DSqXtMX4L3DIIYCai7ACEftdbCIH5n0Y5SPpGCzsjdw.ECDSA key fingerprint is MD5 14:93:39:65:27:31:a6:28:8b:28:d1:2c:fb:32:7b:4d.Are you sure you want to continue connecting (yes/no)? YesWarning: Permanently added '192.168.56.129' (ECDSA) to the list of known hosts.root@192.168.56.129's password: Thu Oct 10 09:46:08 2019 from 192.168.56.1 / at this time has been remotely logged on to the other host [root@localhost ~] # logout / press Ctrl+d to exit remote login Connection to 192.168.56.129 closed. Embed execution: [root@ce~] # more b.shrunken 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.