linux interaction-free

1. Interaction-free concept

Concept: For automated operation and maintenance of shell scripts, it is necessary to achieve the effect of automated operation and maintenance without interaction.

2. Examples of basic interaction-free

Interaction-free counting of line numbers

2.1 Command line interaction-free statistics

wc -l <<EOF
> a
> b
> c
> d
> EOF
4

Insert image description here

2.2 Use scripts to avoid interactive statistics

cat 1.sh
wc -l <<EOF
a
b
EOF
[root@localhost ~]# sh 1.sh
2

Insert image description here

2.3 Print using interactive commands

[root@localhost ~]# read i <<EOF
> a
> EOF
[root@localhost ~]# echo $i
a
[root@localhost ~]# 

Insert image description here

2.4 Change password without interaction

passwd user <<EOF
> 123123
> 123123
> EOF

Insert image description here

2.5 Redirect View

cat <<EOF
> 尾声:半个月亮
> 跋:从山峦到海洋
> EOF
尾声:半个月亮
跋:从山峦到海洋

Insert image description here

2.6 Redirect to specified file

cat <<EOF>1.txt
尾声:半个月亮
跋:从山峦到海洋
EOF

Insert image description here

2.7 Redirection directly specifies the file

tee 1.txt <<EOF
> 《额尔古纳河右岸》
> EOF
《额尔古纳河右岸》

Insert image description here

2.8 Using scripts to complete redirected input

#!/bin/bash
file="1.txt"
i='girl'
cat >$file<<EOF
mws is a $i
EOF

Insert image description here

2.9 Interaction-free script completes assignment of variables

#!/bin/bash
var="Life is fucking move!"
myvar=$(cat<<EOF
人生如戏
$var
EOF
)

echo "$myvar"

Insert image description here

2.10 Turn off the variable substitution function and output as it is

#!/bin/bash
var="Life is fucking move!"
myvar=$(cat<<'EOF'
人生如戏
$var
EOF
)

echo "$myvar"

Insert image description here

2.11 Go to the tab key, but cannot go to the space

#!/bin/bash
var="Great! I am going to school!"
myvar=$(cat <<-'EOF'
        this is line 1.
     today is monday.
   $var
EOF
)

echo $myvar

Insert image description here

2.12 Comment printing, unable to take effect: batch comment

Script display

#!/bin/bash
i=ab
:<<EOF
     abcd
abcd
$i
EOF

Insert image description here

Command display
Insert image description here

3、expect

expect: a tool based on the tcl language, which is often used for automated control and testing to solve interaction-related problems in shell scripts.
How to use: Indicates the capture keyword display line

3.1 Script interpreter

Adding the reference program path indicates the use of expect, indicating that this shell
can only be run using ./run, and the authorization is 777 before running.

#!/usr/bin/expect

3.2 spawn: start a new process and monitor and capture

spawn is usually followed by a Linux execution command, which means opening a session, starting the process, and tracking subsequent interaction information.
For example: spawn passwd root

3.3 expect : Receive a string from the process

  • Determine whether the last output result contains the specified string, if so, return immediately, otherwise wait for the timeout and return;
  • Can only capture the output of processes started by sawn;
  • Used to receive the output after command execution and then match it with the expected string

3.4 send: used to send a string to the process

  • Send a string to the process to simulate user input
  • This command cannot automatically enter or line-feed. Generally, you need to add \r (carriage return) or \n to represent the enter key.

method one:

expect "密码"{
    
    send "123123\r"}					#同一行send部分要有{  }

Method 2:

expect"密码										#换行send部分不需要有{ ) 
send "abc123\r"

Method 3: expect supports multiple branches

expect											#只要匹配了其中一个情况,执行相应的send语句后退出该expect语句
{
    
    											
"密码1" {
    
    send "123123\r"}
"密码2" {
    
    send "abcdef\r"}
"密码3" {
    
    send "abc123\r"}
}

3.5 exp_continue: Match multiple strings and add this command after executing the action

exp_continue is appended to an expect judgment item, so that after the item is matched, it can continue to match other items in the expect judgment statement.
exp_continue is similar to the continue statement in control statements. Indicates that expect is allowed to continue to execute instructions downward.

Note: When using exp_continue, if you are tracking a command like passwd that ends the process after entering the password, do not add expect eof in addition to expect{} because after the spawn process ends, eof will be sent to expect by default, which will cause the subsequent expect eof to be executed. Report an error

3.6 expect eof: terminator

Indicates the end of the interaction, waiting for the execution to end, and returning to the original user, corresponding to spawn.

For example, when switching to the root user, the expect script defaults to waiting for 10 seconds. After executing the command, the default stay is 10 seconds, and then it automatically switches back to the original user.

3.7 interact: Allow user interaction

  • It will stay on the target terminal and will not return to the original terminal. At this time, you can manually operate it. The command after interact will not work;
  • For example, adding exit after interact will not exit the root user. If there is no interact, it will exit after the login is completed instead of staying on the remote terminal.
  • Using interact will stay in the terminal and will not return to the original terminal;
    note: expect eof and interact can only choose one of the two.

3.8 set: set session timeout

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

3.9 send_users: echo command

Indicates the echo command, which is equivalent to echo

3.10 Receive parameters

The expect script can accept arguments passed from the bash command line, using [ lindex $argv n ]. Where you start from 0, representing the first, second, third... parameters respectively

set hostname [lindex $argv 0]						#相当于 hostname=s§1				
set password [lindex Sargv 1]						#相当于 password=$2				

4. Sample script: ssh remote login

Insert image description here
Insert image description here

おすすめ

転載: blog.csdn.net/m0_62231324/article/details/132394988