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 mainly introduces "how to use expect to achieve automatic script interaction in Linux". In daily operation, I believe many people have doubts about how to use expect to achieve automatic script interaction in Linux. Xiaobian consulted all kinds of materials and sorted out simple and easy to use operation methods. I hope to answer your doubts about "how to use expect to achieve automatic script interaction in Linux"! Next, please follow the small series to learn together!
Expect is an automated interactive suite, mainly used to execute commands and programs, the system requires input of specified strings in an interactive form to achieve interactive communication.
1 install expect tool spawn start specified process-> expect get specified keyword-> send want specified process send specified instruction-> execute complete, exit.
Because expect is tcl based, you need to make sure tcl is installed on your system:
#Check if tcl: [root@localhost~]#whereis tcltcl:/usr/lib 64/tcl8.5/usr/include/tcl.h/usr/share/tcl8.5 #If not installed, install tcl and expect using yum:[root@localhost~]#yum install-y tcl [root@localhost~]#yum install-y expect #View the install path of expect:[root@localhost~]#command-v expect/usr/bin/expect 2 expect command states spawn to start a new interactive process, followed by the command or specify the program expect to receive information from the process, if the match is successful, Send the string send exp_send to the process for sending the specified string information exp_continue If you match multiple times in expect, you need to use send_user to print the output equivalent to echointeract in shell to allow user interaction exit expect script eofexpect execution ends, exit set define variable puts output variable set timeout set timeout
show details
3 Brief introduction to the principle of action
3.1 sample script
Here is an example of a script that remotely logs in to a server using ssh, assuming the script name is remote_login.sh:
#!/ usr/bin/expectset timeout 30spawn ssh -l root 172.16.22.131expect "password*"send "123456\r"interact
3.2 Script function interpretation
(1) #!/ usr/bin/expect
This must be the first line of the script file to tell the operating system which script parsing engine to use to execute the script.
The exact path can be viewed with command-v expect.
❝
Note:
Expect here is a script execution engine, like bash for Linux and cmd for Windows.
The script needs to have executable permissions (chmod + x remote_login.sh, or chmod 755 auto_login.sh), and then pass the command./ remote_login.sh can be run;
If you type sh remote_login.sh, the meaning is different: explicitly call the sh engine to execute this script, in which case the #!/in the first line /usr/bin/expect is disabled.
(2) set timeout 30
Set timeout for connections to 30 seconds.
(3) spawn ssh -l root 172.16.22.131
Spawn, send and other commands are internal commands in the expect tool. If the expect tool is not installed, errors such as "spawn not found" will appear.
❝
Don't use which spawn commands to find spawn, because there is no such program.
(4) expect "password*"
This command is used to determine whether the last output contains the string "password *". If so, return immediately, otherwise wait for a while and return. The wait time here is the timeout set above, which is 30 seconds.
(5) send "123456\r"
Here is the interactive action performed, which is equivalent to manually entering a password.
Tip: Add\r to the end of the command string, so that if an exception waits, it can be stopped for further verification.
(6) interact
Expect execution is completed to maintain the user's interactive state, this time the user can manually operate.
If there is no such sentence, expect will exit the script when it is finished, and the user will not be able to continue the operation after logging in remotely to the terminal.
4 Examples of Other Script Uses
4.1 Execute multiple commands directly through expect
Pay attention to the first line, in this case you can only pass./ script.sh to execute such scripts:
#!/ usr/bin/expect-fset timeout 10#Switch to root and execute ls and df commands: spawn su-rootexpect "Password *" send "123456\r" expect "]*" #wildcard send "ls\r" expect "#*" #Another form of wildcard send "df-Th\r" send "exit\r" #exit spawn expect eof #Exit this expect interactive program
4.2 Execute multiple commands via shell call expect
Note the first line, in this case via sh www.example.com, bash script.sh or./ script.sh
#!/ bin/baship ="www.example.com" 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 eofEOF 5 spawn not found
This error is mostly caused by scholars: Linux executes shell scripts in two ways:
❝
One is to use scripts as command-line arguments to sh, such as sh remote_login.sh, or sh/data/remote_login.sh;
One is to make scripts executable with execution permissions, such as./ remote_login.sh, or/data/remote_login.sh.
#!/sh command line argument, then #!/sh command line argument usr/bin/expect will fail, so spawn not found, send not found, etc. All the above automate_login. sh scripts must be run with the following command:
./ automate_expect.sh At this point, the study of "how to use expect to achieve automatic script interaction in Linux" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
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.