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 > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces how to achieve automatic script interaction through expect tools in Linux, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Expect is an automated interaction suite that is based on tcl and can communicate automatically through script settings.
1 install the expect tool
Expect is an automatic interaction suite based on tcl. In some scenarios where interactive input instructions are needed, interactive communication can be carried out automatically through script settings. The interactive process is as follows:
Spawn starts the specified process-> expect gets the specified keyword-> send wants the specified process to send the specified instruction-> execution completed, exit. Because expect is based on tcl, you need to ensure that tcl is installed on the system:
# check if tcl is installed: [root@localhost ~] # whereis tcl tcl: / usr/lib64/tcl8.5 / usr/include/tcl.h / usr/share/tcl8.5 # if not, install tcl and expect using yum: [root@localhost ~] # yum install-y tcl [root@localhost ~] # yum install-y expect # View the installation path of expect: [root@localhost ~] # command-v expect / usr/bin/expect2 expect
The command states that spawn starts a new interactive process, followed by a command or a specified program expect to receive information from the process. If the match is successful, the action after expect is executed send sends the string send exp_send to the process to send the specified string information exp_continue matches multiple times in expect needs to be used to print out the equivalent of echointeract in shell to allow user interaction exit to exit expect script eofexpect execution ends. Exit set definition variable puts output variable set timeout setting timeout 3 principle introduction
3.1 sample script
Here, take the script of ssh remote login to a server as an example, assuming that the name of the script is remote_login.sh:
#! / usr/bin/expect set timeout 30 spawn ssh-l root 172.16.22.131 expect "password*" send "123456\ r" interact3.2 script function interpretation
(1) #! / usr/bin/expect the above must be on the first line of the script file to tell the operating system which script parsing engine the script needs to use to execute.
The specific path can be viewed through the command-v expect command.
Note:
Expect here, like Linux's bash and Windows's cmd, is a script execution engine.
The script needs executable permission (chmod + x remote_login.sh, or chmod 755 auto_login.sh), and then run it through the command. / remote_login.sh
If you type sh remote_login.sh, the meaning is different: explicitly call the sh engine to execute the script, and the #! / usr/bin/expect in the first line becomes invalid.
(2) set timeout 30 sets the connection timeout to 30 seconds.
(3) spawn ssh-l root 172.16.22.131
Spawn, send and other commands are internal commands in expect tools. If expect tools are not installed, errors such as "spawn not found" will occur.
Do not use commands such as which spawn to find spawn, because there is no such program.
(4) the command expect "password*" is used to determine whether the last output contains a string of "password*". If so, it will be returned immediately, otherwise it will be returned after a period of time. The waiting time here is the timeout set above, that is, 30 seconds.
(5) send "123456\ r" here is to perform an interactive action, which is equivalent to manually entering a password.
Tip: add\ r to the end of the command string so that if there is an abnormal waiting state, you can stay for further verification.
(6) maintain the interactive state of the user after the completion of the interactexpect execution, at this time the user can operate manually.
Without this sentence, the expect will exit the script and remotely log in to the past terminal, and the user will not be able to continue the operation.
4 examples of using other scripts
4.1 execute multiple commands directly through expect
Pay attention to the first line, in which case you can only execute such scripts through. / script.sh:
#! / usr/bin/expect-f set timeout 10 # switch to root user Then execute the ls and df commands: spawn su-root expect "Password*" send "123456\ r" expect "] *" # wildcard send "ls\ r" expect "# *" # wildcard another form of wildcard send "df-Th\ r" send "exit\ r" # exit the process started by spawn expect eof # exit this expect interactive program 4.2 execute multiple commands through shell calling expect
Note the first line, in which case you can execute such scripts through sh script.sh, bash script.sh, or. / script.sh:
#! / bin/bash ip= "172.16.22.131" username= "root" password= "123456" # specify execution engine / usr/bin/expect set time 30 spawn ssh $username@$ip df-Th expect {"* yes/no" {send "yes\ r"; exp_continue} "* password:" {send "$password\ r"} expect eof EOF5 spawn not found solution
It is basically the scholar who makes this error: there are two ways for Linux to execute shell scripts:
One is to use the script as a command line argument to sh, such as sh remote_login.sh, or sh / data/remote_login.sh
One is to use a script as an executable script with execution permissions, such as. / remote_login.sh, or / data/remote_login.sh.
If you run it as a sh command line argument, the #! / usr/bin/expect on the first line of the script will become invalid, so there will be errors such as spawn not found, send not found, and so on. All the above automate_login.sh scripts must be run with the following command:
Automate_expect.sh on how to use expect tools in Linux to achieve automatic script interaction is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.