Use expect to realize automatic interaction (9)

One: Expect language

1.1: Introduction to expect

EXPECT is a software suite for automating interactive tools (EXPECT is a software suite for automating interactive tools), which provides tools for automating interactive tools.

1.2: Expect application scenarios

Imagine a scenario: For example, when you want to log in to the server with ssh or telnet, the command line prompts you to enter the user and password.

Wouldn’t it be a bit difficult to ask you to write an automatic login script using a shell script? You would say that if you execute the ssh command in the shell, he will stay there waiting for your password. This should be done manually, right?

Expect is a tool designed to handle such situations. In simple terms, it can control and process input and output streams, and then provide automatic data filling and other places that require interactive input by users to achieve automatic processing.

Expect is a tool for dealing with "automatic interaction".

Expect supports custom scripts, so you can achieve the automatic data filling function you want by writing custom scripts.

1.3: Expect workflow

Expect's workflow can be understood as:

spawn starts the process---->expect expects the keyword---->send sends characters to the incoming process—>exit ends.

1.4: Expect syntax

1.4.1:spawn

The spawn command is the initial command of expect.

It is used to start a process, and then all expect operations are carried out in this process. If there is no spawn statement, the entire expect cannot be executed.

The method of using spawn is as follows:

spawn ssh [email protected]
// 在spawn命令后面,直接加上要启动的进程、命令等信息

1.4.2:expect

The expect command is used to wait for the output of a matching content. Once it matches, execute the action or command following expect. The syntax is as follows:

expect 表达式 动作 表达式 动作...

Insert picture description hereIt can be seen from the above example that expect is attached to the spawn command. When the ssh command is executed, expect matches the keyword after the command is executed: password.

If the keyword is matched, the exp_send action contained in the {} brackets will be executed. The matching and action can be placed on two lines, so that {} brackets are not needed, as shown below, the actual completed function is the same as above the same

Insert picture description here

1.4.3:exp_send 和send

The exp_send command or send command is an action command in expect. It is
used to send a character string to the process. It supports escape characters, and \r means carriage return. The
usage is as follows:

Insert picture description here

1.4.3:timeout

timeout is an important variable in expect.

It is a global time control switch, you can specify the entire expectedc operation time by assigning a value to this variable.

Note that this variable is service and expect global, it will not entangle a certain command, even if there is no error in the command, the variable will still be activated at the time.

The method of use is as follows:

Insert picture description hereIn the timeout variable, setting to 0 means timeout immediately, and -1 means not timeout forever.

1.4.3:expect eof

Corresponding to spawn means that the output of the capture terminal is terminated.

1.4.4:set

set creates a variable
[lindex $argv 0] that refers to the first value in the script parameter;
[lindex $argv 0-2] refers to the 0th to 2nd value in the script parameter.

$argv,参数数组,使用[lindex $argv n]获取,$argv 0为脚本名字
# $argc,参数个数
set username [lindex $argv 1]  # 获取第1个参数
set passwd [lindex $argv 2]    # 获取第2个参数

1.4.5:interact

interact keep the interactive state

By default, spawn starts the child process to execute the target command, and the control will be returned to the original process after the execution of the command (that is, the user can input!),

Help maintain the interaction of the child process through interaction, this operation is very important in ssh; for example, after the ftp interaction completes the transfer of a file, the user can still stay in the ftp CLI through interaction.

Two: Use expect to realize ssh automatic login

2.1: Implementation ideas

According to the general ssh login steps, the realization idea can be obtained: execute the ssh command to remotely log in to the ssh server and wait for the ssh server to return the interface for entering the user name and password. Enter the user name and password to log in

2.2: Programming realization

/usr/bin/expect 
        spawn ssh root@ip
        expect {
    
    
        "yes/no" {
    
     send "yes\r";exp_continue } #\r表示回车
        "password:" {
    
    send "123456\r"}
 
        }
        expect "]#"
 
        send "ifconfig\r"
        send "exit\r"
        expect eof
send {
    
    ifconfig ens33|grep -w inet |awk '{print $2}'}
send "\r"
send "exit\r"
expect eof

Three: Use expect and scp to realize automatic file transfer

3.1: Implementation ideas

Start the scp command server to return to the input user name and password interface to send the user name and password to complete the login

3.2: Programming realization

Insert picture description here

Four: Use expect to realize ftp automatic file upload

4.1: Implementation ideas

Realization idea: start the command server, return the input user name and password interface, send the user name and password, log in to the server, return the command, and send the operation command to exit

4.2: Programming realization

#!/usr/bin/expect
spawn ftp localhost  #执行ftp命令
expect "Name*"       #如果出现Name字符
send "ftp\n"         #则输入ftp并回车
expect "ftp>*"
send "cd pub/other\n"
expect "ftp>*"
send "put /tmp/123.txt xo.txt\n"
expect {
    
    
        "150 Ok to send data" {
    
     send_user "upload sucessfull!";send "quit\n" }  #send_user 类型shell的echo命令
        "553*" {
    
     send_user "upload error!";send "quit\n" }
}
expect eof           #expect eof,与spawn对应,表示捕捉终端输出信息终止,类似if...endif

Guess you like

Origin blog.csdn.net/zhangshaohuas/article/details/109146206