No interaction

No interaction

Here Document Free interactive
use I/O redirection to provide the command list to the interactive program, a substitute for standard input

Grammatical format

Command << tag
... # between the tags is the incoming content
...
tags

Precautions

The mark can use any legal character (usually EOF). The
ending mark must be written in the top
box, and there can be no characters
before the ending mark. There can be no characters after the ending mark (including spaces) . The spaces before and after the beginning mark will be omitted.

Instance

Count the number of rows
Insert picture description here
Assign values ​​to variables through read

Insert picture description here
Insert picture description here
Change password for user
Insert picture description here
Single variable replacement
Insert picture description here

Insert picture description here
Assign a value to the variable as a whole

Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here
Turn off the variable substitution function to
Insert picture description here
Insert picture description here
remove the TAB characters or spaces before each line
Insert picture description here
Insert picture description here

Expect

A tool built on the basis of the tcl language, often used for automated control and testing, to solve interactive problems in shell scripts

Basic command

(1) Script interpreter

The file is first introduced in the expect script to indicate which shell is used.
#!/usr/bin/expect
Insert picture description here

(2)spawn

Spawn is usually followed by a command to open a session, start a process, and track subsequent interaction information.
Example: spawn passwd root

(3)expect

Determine whether the last output result contains the specified string, if there is, return immediately, otherwise, wait for the timeout to return; only capture the output of the process started by spawn; used to receive the output after the command is executed, and then match the expected String matching

(4)send

向进程发送字符串,用于模拟用户的输入;该命令不能自动回车换行,一般要加\r(回车)或者\n
例:
case1="密码"
respond="abc1234"

expect "$case1" {
    
    send "$respond1\r"}		#同一行send部分要有{
    
    }

expect "$case1"	
send "$response1\r"							#换行send部分不需要有{
    
    }

expect					#只要匹配了其中一个情况,执行相应的send语句后退出该expect语句
{
    
    
"$case1" {
    
    send "$response1\r"}
"$case2" {
    
    send "$response2\r"}
"$case3" {
    
    send "$response3\r"}
}

(5) Terminator

expect eof
means the end of the interaction, wait for the end of execution, and return to the original user, corresponding to spawn

For example, if you switch to the root user, the expect script waits for 10s by default. After executing the command, it will automatically switch back to the original user after it stays for 10s

After interact is
executed, the interactive state is maintained, and the control is transferred to the console. It will stay at the target terminal instead of returning to the original terminal. At this time, it can be operated manually. The commands after interact do not work, such as adding exit after interact. , And will not exit the root user. If there is no interaction, it will log out after logging in, instead of staying on the remote terminal.

Using interact will remain in the terminal and will not return to the original terminal. For example, if you switch to the root user, you will always be in the root user state; for example, when you ssh to another server, you will always be at the target server terminal instead of switching back to the original server. .

Note: Expect eof and interact can only choose one.

(6)set

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

(7)exp_continue

exp_continue is appended to a certain expect judgment item, so that after the item is matched, it can continue to match other items in the expect judgment sentence. exp_continue is similar to the continue statement in the control statement.

For example: The following example will determine whether there is yes/no or *assword in the interactive output. If it matches yes/no, output yes and execute the judgment again; if it matches *assword, output abc123 and end the expect statement.

expect {
    
    
    "(yes/no)" {
    
    send "yes\r"; exp_continue;}
    "*password" {
    
    set timeout 300; send "abc123\r";}
}

Note: When using exp_continue, if you track commands that terminate the process after entering a password like passwd, do not add expect eof outside of expect{}
because after the spawn process ends, eof will be sent to expect, which will cause the subsequent expect eof execution to report an error

(8)send_user

send_user means echo command, equivalent to echo

(9) Receive parameters

The expect script can accept parameters passed from the bash command line and get it using [lindex $argv n]. Among them, n starts from 0 and represents the first, second, third...parameters respectively.
Example:
set hostname [lindex $argv 0] is equivalent to hostname=$1
set password [lindex $argv 1] is equivalent to password=$2

Expect to execute directly, you need to use the expect command to execute the script

su switch user

Insert picture description here
Insert picture description here

Embedded execution mode, integrate the expect process into the Shell to facilitate execution and processing

Create user and set password

Insert picture description here
Insert picture description here

Realize ssh automatic login

Insert picture description here
Insert picture description here

Embedded ssh automatic login

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/Jun____________/article/details/114894414