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/01 Report--
This article is about how linux automates the interactive script expect. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
I. introduction
Expect scripting is an extension of the Tcl scripting language. It is used to realize automatic interactive tasks without human intervention. In actual development, running the shell script will sometimes enter the linux password or mysql password, and expect can help us enter it.
II. Installation
Expect is not included in the linux system and needs to be installed by ourselves. Under the Ubuntu system, the installation command is as follows
?
one
two
Sudo apt-get update
Sudo apt-get install expect
3. Simple expect script
3.1. Write a script
First create an expect script
?
one
Sudo vim hello.exp
Write the following under hello.exp:
?
one
two
three
four
five
six
seven
eight
nine
ten
eleven
#! / usr/bin/expect
Set timeout 5
Spawn su
Expect {
Assword {send "123456\ r"} # your linux password
}
Interact
Run hello.exp
?
one
Expect hello.exp
After running, you will find that your users will become root users. Those of us who have used linux will know that users who enter root need to enter their passwords, and if they use expect scripts, they will automatically enter their passwords for us. This is the automated interaction of expect.
3.2. Explain the script
(1) #! / usr/bin/expect
Specify the script to run, which means the same as #! / bin/bash in the shell script. If you don't know where your expect is installed, you can use which expect to check it. Mine is installed under / usr/bin/expect
(2) set timeout 5
Set: is a setting variable. You can set a custom variable or set the value of an internal variable in an expect script
Timeout:timeout is an internal variable of the expect script, and we cannot use this kind of internal variable name when using our custom name. Timeout means timeout (in seconds). The default is 10s timeout. You can also set-1 to never time out.
Set timeout 5: it means that in the expect statement, the timeout occurs after 5s and no more selections are made.
(3) spawn su
Start a new process to execute the su command
(4) expect {assword send {"123465\ r"}}
Expect: receives information from the process and, if the match is successful, executes the action after expect
Send: sends a string to the process
?
one
two
three
Expect {
Assword send {"123465\ r"}
}
Note: determine whether the output information contains a string containing assword, if so, send a command 123456\ r (\ r is to hit enter) and exit the expect statement; if not, exit the expect statement after waiting for timeout.
(5) interact
Do not quit after executing the commands in the spawn, and we will perform the rest manually. For example, the login ssh,expect script can help us log in (without manually entering the password). After logging in, we need to interact manually.
3.3. Summary
Seeing here, I believe you already know the nature of the expect script: know the return information of the linux system in advance, and capture this information to respond.
IV. Text
4.1. Common command description
The command states that set timeout n sets the expect statement timeout to n seconds. -1 to never time out set name value sets the variable named name and its value to valueset name [lindex $argv 0] sets the variable named name, whose value is the first parameter passed into the expect script. The index value of the first parameter is 0, and the second is 1, which in turn analogizes spawn to start a new process, executes the command or specifies the program expect to receive the information returned in the process. If the match is successful (case sensitive), the action after expect is executed send sends the string send_user to the process to print information, which is equivalent to making the expect not quit after the echoexp_continue in shell executes the action after expect. Continue to match down expect eof does not allow user interaction, directly exit (this will use more than interact) interact allows user interaction
Example: run the shell script
(1) create a shell script, 1.sh
?
one
two
three
four
five
six
seven
eight
nine
#! / bin/bash
Echo "read print"
Read-p "please input name in there" NAME
Echo ${NAME}
Read-p "input password in there" PASSWORD
Echo ${PASSWORD}
The meaning of the script is to enter your account number, password and print it out.
(2) create an expect script, 1.exp. Ask 1.exp to input the information for us.
?
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
twenty-two
twenty-three
twenty-four
#! / usr/bin/expect
# to run a shell script, write the absolute path of the shell script
Spawn bash / home/hadoop/test/1.sh
# set the timeout for expect statements. Default 10s
Set timeout 3
Expect {
# expect is similar to the swtich statement. It matches the statement in parentheses {} and executes as soon as a matching statement is found
# will exit after execution, but will not quit after adding exp_continue, and will continue to execute.
# here is the matching string, which does not need double quotation marks and is case sensitive
Name {
Send "meizhaowei\ r"
Send_user "success\ n"
# return the information displayed to the user, similar to echo, print the information to the console
Exp_continue
}
Assword {send "123456\ r"}
}
Expect eof #, in contrast to interact, does not enter into human interaction after executing the program
(3) run
Normally, running 1.sh allows you to enter name and password manually, but after running 1.exp here, you can ask 1.exp to enter 1.sh information for you.
If you want to delve deeper into expect grammar, here is the user's manual.
English original: http://www.tcl.tk/man/expect5.31/expect.1.html
Chinese translation: https://blog.csdn.net/cbuy888/article/details/80561461
Thank you for reading! This is the end of the article on "how to automate the interactive script expect in linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Original link: https://blog.csdn.net/lendsomething/article/details/109066545
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.