Interaction-free shell programming (Document and Expect)

One, Here Document is free of interaction

(I. Overview

Use I/O redirection to provide a list of commands to interactive programs or commands, such as ftp, cat, or read commands. Is a substitute for standard input can help script developers not to use temporary files to construct input information, but directly generate a "file" on the spot and use it as standard input for "commands". Here Document can also be used with non-interactive programs and commands.
1. Grammar format:

命令 <<标记
...
内容    #标记直接是传入内容
...
标记

Precautions:

  • Marks can use any legal characters (usually EOF)
  • The ending mark must be written in the top grid without any characters in front
  • There can be no characters (including spaces) after the ending tag
  • The spaces before and after the opening tag will be omitted
    Insert picture description here
    == 1. Interaction-free mode realizes the statistics of the number of lines, put the content to be counted between the tags "EOF", and directly pass the content to wc -l for statistics ==
wc -l <<EOF
>Line1
>Line2
>EOF

Insert picture description here

==2. Receive input and print through the read command. The input value is the part between the two EOF tags, as the value of variable a ==

read a <<EOF
>123
>EOF
echo $a

Insert picture description here
Insert picture description here

3. Set a password for the user through passwd

passwd list <<EOF
>abc123123            
>abc123123
EOF

Support variable substitution
==4. When writing the file, the variable will be replaced with the actual value, and then combined with the cat command to complete the writing ==

#/bin/bash
file="EOFtest.txt"
i=home
cat>$file <<EOF
I am going $i
EOF

cat EOFtest.txt 

Insert picture description here

5. Assign the whole value to the variable, and then print the variable value through the echo command

#/bin/bash
var="Great I am going school!"
i=school
mytest=$(cat << EOF
This is `date +%F`
I am going $i
EOF
)
echo $mytest

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

6. Turn off the function of variable substitution, output according to the original appearance of the characters, without any modification or replacement

#/bin/bash
var="Great I am going school!"
i=school
mytest=$(cat << 'EOF'  #也可以使用单引号''
This is `date +%F`
I am going $i
$var
EOF
)
echo "$mytest"

Insert picture description here
Insert picture description here
==7. Remove the TAB character before each line ==

#/bin/bash
var="Great I am going school!"
i=school
mytest=$(cat <<-EOF             #在标记前加“-”,即可抑制各行tab字符
        This is `date +%F`
                I am going $i
                        $var
EOF
)
echo "$mytest"

Insert picture description here
Insert picture description here
8. Multi-line comments Bash's default comment is "#", this comment method only supports single-line comments: The introduction of Here Document solves the problem of multi-line comments. ":" represents an empty command that does nothing. The content of the middle mark area will not be executed and will be ignored by bash, so the effect of batch comments can be achieved

#/bin/bash
var="Great I am going school!"
i=school
:<<-EOF   #多行注释,":"开头的Here Document 标记内容不会被执行
        This is `date +%F`
                I am going $i
                        $var
EOF
echo "$mytest"

Insert picture description here

Two, Expect

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

rpm -q expect
rpm -q tcl
yum -y install expect

(1) Basic commands:

1. Script interpreter

The file is first introduced in the expect script to indicate which shell is used

#!/usr/bin/expect

2.spawn

Spawn is usually followed by a Linux execution command, which means opening a session, starting a process, and tracking 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 a character string to simulate the user's input; this command cannot automatically enter and line feed, usually add \r (carriage return) or \n

Example:
method one

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

Way two

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

Way three
expect supports multiple branches

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

5. Terminator

1.expect eof

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 waits for 10s by default. When the command is executed, it will automatically switch back to the original user after it stays for 10s by default.

2.interact

After the execution is completed, 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, you can manually operate it. The command after interact does not work, such as adding exit after interact. It will not exit the root user. And if there is no interaction, it will log out after the login is completed, 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, ssh to another A server will always be at the target server terminal, and will not switch back to the original server
Note: Expect eof and interact can only choose one

6.set

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

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. Indicates that expect is allowed to continue to execute instructions downward.
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 *password, 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 like passwd to end the process after entering the password, do not add expect eof outside of expect{}
because after the spawn process ends, it will send eof to expect by default, which will cause the subsequent expect eof to execute 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]    相当于 hostname=$1 #在内嵌时使用hostname=$1
set password [lindex $argv 1]  相当于 password=$2

(2) Related implementation examples

1. Expect to execute directly, you need to use the expect script to execute the script

When executing, you can only use "./" to execute and you can only use set to pass parameters
su switch user

#!/usr/bin/expect   #一定要注释解释器  使用内嵌的#!/bin/bash
set username [lindex $argv 0]    
set password [lindex $argv 1]
#开始追踪命令
spawn su $username
#免交互执行,捕捉信息并匹配
expect "密码"
send "$password\r"
expect "*]#"
send_user "ok"   #send_user表示回显命令不被解释器解释
#把控制权交给控制台
interact
#expect eof

Insert picture description here

Insert picture description here
Insert picture description here

2. Embedded execution mode, integrate the expect process into the Shell, which is convenient for execution and processing

Create user and set password

#!/bin/bash
user=$1
password=$2
#非交互命令放在expect外面
useradd $user
#开始免交换执行
/usr/bin/expect <<-EOF
#expect开始标志
spawn passwd $user
#开启-一个进程跟踪passwd命令,expect只能捕捉该进程信息
expect "新的*"
send "${password}\r"
expect "重新*"
send "${password}\r"
expect eof
EOF

Insert picture description here
Insert picture description here

3. Realize ssh automatic login

Direct execution mode

#!/usr/bin/expect   #注意与内嵌模式的区别
set host [lindex $argv 0]
set passwd [lindex $argv 1]
spawn ssh $host
expect {
    
    
"Connection refused" exit     #连接失败情况,比如对方ssh服务关闭
"No route to host" exit       #无法连接的情况
"(yes/no)" {
    
    send "yes\r"; exp_continue}   #匹配成立输入yes
"*password:" {
    
    send "${passwd}\r"}        #条件成立输入密码
}
interact

Insert picture description here
Insert picture description here
Embedded

#!/bin/bash
username=$1
passwd=$2
/usr/bin/expect<<-EOF
spawn ssh $username
expect {
    
    
"Connection refused" exit
"No route to host" exit
"(yes/no)" {
    
    send "yes\r";exp_contiune}
"password:" {
    
    send "$passwd\r"}
}
expect eof
EOF

Insert picture description here
Insert picture description here

5. Create disk without interaction

#!/bin/bash
disk=$1
/usr/bin/expect <<-EOF
spawn fdisk $disk
expect "命令" {
    
    send "n\r"}
expect "Select" {
    
    send "\r"}
expect "分区" {
    
    send "\r"}
expect "起始" {
    
    send "\r"}
expect "Last" {
    
    send "\r"}
expect "命令(输入 m 获取帮助):" {
    
    send "w\r"}
expect eof
EOF
partprobe $disk
mkfs.xfs $disk -f &> /dev/null
if [ $? -eq 0 ]
then
echo "finsh"
mount $disk /mnt
else
echo "fail"
fi

Guess you like

Origin blog.csdn.net/weixin_53567573/article/details/114897483