Technology Sharing

Shell Expect automation interaction (example)

2024-07-08

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Shell Expect Automation Interaction

In daily Linux operation and maintenance, you often need to log in to the server remotely. The login process requires interaction and you may need to enter yes/no and other information, so expect is used to implement the interaction.

Key syntax

❶[#!/usr/bin/expect]

This line tells the operating system which shell to use to execute the code in the script. The expect here is actually the same as bash in Linux and cmd in Windows. Note: This line needs to be the first line of the script.

❷set

The default timeout of expect is 10 seconds. The session timeout can be set through the set command. If the timeout is not limited, it should be set to -1.

For example: set timeout 300 // indicates that the session timeout is 300 seconds

For example: set timeout -1 / set / never time out, common settings

Note: Why do we need to set a timeout? Because the default time is 10s, it is very likely to be disconnected during the interactive execution, causing the task to terminate before completion. A scheduled task in our company that synchronizes files from a remote FTP always terminates. Later, we set the timeout to -1 and it worked.

❸spawn

Spawn is usually followed by a Linux execution command, which means opening a session, starting a process, and tracking subsequent interaction information.

❹expect

Only the results of the command executed by spawn are captured by expect, including the prompt information of the standard input, eof and timeout. Waiting for the command prompt information to appear, that is, capturing the prompt input by the user:

➎send

Sends values ​​that require interaction, replacing the manual input of the user; this command cannot automatically enter a new line, usually you need to add r (Enter) or n

expect "password"{ send "abc123r"} //The send part of the same line must have {}

❻Expect eof/interact

expect eof

This must be added, corresponding to spawn, indicating the termination of the capture terminal output information, similar to if....endif

interact

After the execution is completed, keep the interactive state and hand over the control to the console, then you can operate manually. If there is no such sentence, the login will be logged out instead of staying on the remote terminal.

PS: The expect script must end with interact or expect eof. Expect eof is usually enough to perform automated tasks.

Example

Insert the expect command in the shell script in the following format:

Example 1: 22.sh create folder script

#!/bin/sh

expect

spawn ssh 10.1.1.196

expect password {send xxxn}

send cd /home/cg/hmf_app/testr

send mkdir newworkr

send exitr

expect eof

EOF

Example 2: Create a folder shell script

expect

spawn ssh 10.1.1.196

expect password {send xxxn}

send cd /home/cg/hmf_app/testr

send mkdir newworkr

send exitr

expect eof

EOF

Example 3: Copying a file

expect

set timeout -1

spawn scp [email protected]:/data/package/casb/$casbzip ./

expect password {send xxxn}

expect eof

EOF

Remark:

EOF

...

EOF

Represents a shell script

refer to:Shell Expect automated interaction_shell script interaction automatic input content-CSDN blog