Detailed explanation of expect operation in Linux learning

1. Expect installation introduction

1.expect command installation

Installation statement: yum install expect

2. Expect command meaning

expect is a scripting language that can replace manual interaction with the terminal. It is mainly used when executing commands and programs. The system requires the input of a specified string in an interactive form to achieve interactive communication.

3.expect usage scenarios

(1) and answer its questions based on predetermined criteria, answering "yes", "no" or returning control to you

(2) Remotely connect devices and perform automated operations

(3) Mainly in some places that require human-computer interaction, if you know in advance what commands you should enter, you can use the expect tool

Second, the principle of use of expect

1. Principle introduction:

spawn starts the specified process—expect obtains the specified keyword—send sends the specified character to the specified program—executes and exits

2. Introduction to the spawn command:

The spawn command is used to start a new process. The send and expect commands after spawn interact with the process opened by spawn,

3. Introduction to the send command:

The send command receives a string parameter and sends the parameter to the process, which is similar to simulating a human inputting a password

4. Introduction to the interact command:

Combining spawn, expect, and send to complete many tasks automatically, the interact command can intervene in the task at an appropriate time. For example, after downloading the ftp file, it can still stay in the ftp command line state to manually execute subsequent commands

3. expect syntax

1. expect enable option:

  • -c The command to execute before executing the script, which can be used multiple times
  • -d debug mode, which can output some diagnostic information at runtime, similar to using exp_internal 1 at the beginning of the script.
  • -D Enables the exchange of debuggers, an integer parameter can be set.
  • -f Read commands from a file, only used when #! is used. If the filename is "-", read from stdin (use "./-" to read from a filenamed -).
  • -i Enter commands interactively, use "exit" or "EOF" to exit the input state
  • -- Mark the end of the option (if you need to pass parameters similar to the expect option to the script), you can put it in the #! line: #!/usr/bin/expect --
  • -v Display expect version information

2. Expect command parameters:

  • The spawn interactive program starts and executes the following command or program. You need to enter the expect environment before it can be executed, and it cannot be executed directly in the shell environment
  • set timeout n Set the timeout time, indicating that the script code needs to be completed within n seconds, and exit if it exceeds. It is used to prevent the ssh remote host from getting stuck when the network is unreachable and from executing commands on the remote host. If set to -1 means no timeout
  • set defines variables
  • The $argv expect script can accept bash's external parameter passing, you can use [ lindex $argv n ] n is 0 to indicate the first parameter passing, 1 to represent the second parameter passing, and so on
  • expect specifies to receive information from the interactive program process, if the match is successful, execute the command interaction of send; otherwise wait for timeout seconds and then automatically exit the expect statement
  • If send matches the information received by expect, it will interactively pass the instructions in send and execute the interactive action. Adding \r at the end means that if there is an abnormal waiting state, it can be checked
  • exp_continue means circular matching. Usually, the statement will be exited after matching, but if there is exp_continue, it can continuously loop matching, enter multiple commands, and simplify the writing.
  • exit Exit the expect script
  • After the expect eof spawn process ends, it will send eof to expect, and receiving eof means the end of the process
  • interact maintains the interactive state after executing the code, passing control to the user. Does not automatically exit after the command is executed instead of staying on the remote terminal
  • puts output variable

Four, expect use example

1. The expect script implements ssh to automatically log in to the remote server

1

2

3

4

5

6

7

#!/usr/bin/expect

spawn ssh [email protected]

expect "*password:"

send "test20221007\r"

expect "*#"

interact

Execute expect expect_demo1.sh to view the running results:

2. The expect script implements ssh to automatically log in to the remote server (general script, manually input parameters)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#!/usr/bin/expect

if {$argc < 3} {

    puts "Usage:cmd <host> <username> <password>"

    exit 1

}

set timeout -1

set host [ lindex $argv 0 ]

set username [ lindex $argv 1 ]

set password [ lindex $argv 2 ]

spawn ssh  $username@$host

expect "*password*" {send "$password\r"}

interact

./expect_demo2.sh 192.168.37.9 mrswhite test20221007 Execute to view the running results:

3.在shell 中嵌套expect

通过expect嵌套shell使用语句在shell内直接执行,任何这样可以实现更多的功能

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

#!/bin/bash

user="mrswhite"

host="192.168.37.9"

password="test20221007"

/usr/bin/expect << EOF

set time 20

spawn ssh $user@$host

expect {

"*yes/no" { send "yes\r"; exp_continue }

"*password:" { send "$password\r" }

}

expect "*#"

send "pwd\r"

expect "*#"

send "df -h\r"

expect "*#"

send "exit\r"

interact

expect eof

EOF

五、expect相关错误处理

1.invalid command name "/usr/bin/expect"

解决方案:此时是使用bash脚本嵌套了expect代码,所以执行采用以下两种方式都可以

1

2

./expect_demo3.sh

sh expect_demo3.sh

2.invalid command name ":" 转义问题

 解决方案:send里面的内容中的括号[]有问题,不能使用[],将其去除或者添加转义字符

1

2

send "cat 20221007.txt | awk -F : '{print $2}'"

send "cat 20221007.txt | awk -F \[:\] '{print $2}'\r"

Guess you like

Origin blog.csdn.net/jh035/article/details/127977231