Linux automatic interactive script expect

Time:2020-12-23

1、 Introduction

Expect script is an extension of TCL scripting language. It is used to realize automatic interactive tasks without human intervention. In the actual development, running shell scripts sometimes input Linux password or MySQL password, and expect can help us enter it.

2、 Installation

Expect does not come with Linux system, so we need to install it by ourselves. Under the Ubuntu system, the installation command is as follows


sudo apt-get update
sudo apt-get install expect

3、 Simple expect script

3.1. Writing scripts

First create the expect script


sudo vim hello.exp

stay hello.exp Write the following:

#!/usr/bin/expect

set timeout 5

spawn su

expect {
  Password {send "123456 / R"} your Linux password
}

interact

function hello.exp


expect hello.exp

After running, you will find that your user becomes the root user. We who have used linux will know that you need to enter the root user with a password. If you use the expect script, he will automatically enter the password for us. This is the automated interaction of expect.

在这里插入图片描述

3.2. Explain the script

(1) #!/usr/bin/expect
Specifies the script to run, and the#!/bin/bashIf you don’t know where your expect is installed, you can use thewhich expectTo see. Mine is installed under / usr / bin / expect

在这里插入图片描述

(2) set timeout 5
Set: is the setting variable, which can be setCustom variableYou can also set the expect scriptInternal variablesValue of
timeout:timeoutIt’s the expect scriptInternal variables, we can’t use this custom nameInternal variable name。 Timeout means timeout time. The unit is seconds. The default is 10s timeout. You can also set – 1 to never timeout

Set timeout 5: it means that in the expect statement, it will time out after 5 seconds and no more choices will be made.

(3) spawn su
Start a new process to executesucommand

(4) expect { assword send {“123465\r”}}
Expect: receives information from the process. If the match is successful, the action after expect is executed
Send: sends a string to the process


expect { 
	assword send {"123465\r"}
} 

Note: judge whether the output information containsassword, if any, sends a command123456\r(R is the enter) and exit the expect statement; if not, wait for the timeout time to exit the expect statement.

(5) interact
We will not exit after executing the commands in spawn, and the rest operations will be executed by us manually. If you log in to SSH, the expect script can help us log in (without manually entering the password). After login, we need to interact manually.

3.3 summary

Seeing this, I believe you already know the essence of expect script: know the return information of Linux system in advance, and capture the information to make response.

4、 Text

4. Common commands

 

command explain
set timeout n Set the expect statement timeout to N seconds. -1 is never overtime
set name value Set the variable name as name and its value as value
set name [lindex $argv 0] Set the variable name as name, and its value is the first parameter passed into the expect script. The index value of the first parameter is 0, the second is 1, and so on
spawn Start a new process, execute a command or specify a program
expect Receive the information returned in the process. If the match is successful (case sensitive), the action after expect will be executed
send Send a string to the process
send_user Used to print information, equivalent to echo in shell
exp_continue After executing the actions after expect, make expect not exit and continue to match downward
expect eof Do not allow user interaction, exit directly (this will use more than interact)
interact Allow user interaction

4.2 example: running shell script

(1) Create a shell script, 1. Sh


#!/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 script means to enter your account number, password, and print it out

(2) Create an expect script, 1. Exp. Let 1. Exp help us input information

#!/usr/bin/expect

#To run a shell script, write the absolute path to the shell script
spawn bash /home/hadoop/test/1.sh

#Set the timeout for the expect statement. Default 10s
set timeout 3

expect {
  #Expect is similar to the swtich statement. If a matching statement is found, it will be executed,
  #After execution, it will exit and add exp_ After that, continue will not continue.

  #Here is the matching string. There is no need for double quotation marks. It is case sensitive
  name {
    send "meizhaowei\r"
    send_user "success\n"
    #Return the information displayed to the user, similar to echo, and print the information to the console
    
    exp_continue
  }
  assword {send "123456\r"}
}

Expect EOF ා is contrary to interact, and does not enter into manual interaction after executing the program

(3) Operation

在这里插入图片描述

Normally, running 1.sh will let you enter name and password manually, but after running 1.exp here, you can let 1.exp help you enter 1.sh information

If you want to study expect syntax more deeply, here is the user manual
Original English version: http://www.tcl.tk/man/expect5.31/expect.1.html
Chinese translation: https://blog.csdn.net/cbuy888/article/details/80561461

This article about the Linux automation interaction script expect will introduce here. For more related Linux expect automation interactive script content, please search the previous articles of developeppaer or continue to browse the related articles below. I hope you can support developeppaer more in the future!