Shell free of interaction

Shell free of interaction

Here Document

Here Document overview

Use I/O redirection to provide a list of commands to interactive programs
. An alternative to marking input

Grammar format

Command << mark
...
mark

Precautions for using Here Document

Marks can use any legal characters
. Marks at the end must be in a fixed format, and there must be no characters in front
of the mark at the end. There must be no characters (including spaces) behind the mark at the end.
The spaces before and after the start mark will be omitted.

Here Document Free Interaction

Receive input and print via the read command
[root@localhost ~]# vim a.sh
#!/bin/bash
readi <<EOF
Hi
EOF
echo $i
[root@localhost ~]# chmod +x a.sh
[root@localhost ~]# ./a.sh
[root@localhost ~]# Hi

Set a password for the user through passwd
[root@localhost ~]# vim here.sh
#!/bin/bash
passwd zhangsan<<EOF
123123
123123
EOF

Here Document variable settings

变量替换
[root@localhost ~]# vim here_ var replace.sh
#!/bin/bash
doc file=“2019.txt”
i=“company”
cat> $doc
file << HERE
Take him from home to KaTeX parse error: Expected 'EOF', got '#' at position 30: …ot@localhost ~]#̲ chmod +x here_…(cat <<EOF
This is Line 1.
That are Sun,Moon and Stars.
$ivar
EOF

echo $myvar
[root@localhost ~]# sh here.sh
This is Line 1. That are Sun,Moon and Stars. Great! Beautyful!

Here Document format control

Turn off variable replacement function
[root@localhost ~]# cat here.sh
#!/bin/bashcat <<'EOF' Single quotes turn off variable replacement
This is Line 1.
$num1
EOF
[root@localhost ~]# sh heresh
This is Line 1.
$num1

Here Document multi-line comments

Make Bash support multi-line comments through Here Document.
Syntax format
: << DO-NOTHING
First line comment
Second line comment
...
DO-NOTHING

Expect overview

Expect is a tool based on tcl, and Expect is a tool for automated control and testing. Mainly to solve the problem of non-interaction in shell scripts, which is very helpful for large-scale Linux operation and maintenance.
In Linux operation and maintenance and development, we often need to remotely log in to the server to operate. The login process is an interactive process, which may be required Enter information such as yes/no password.
In order to simulate this input, you can use the Expect script

Expect basic commands

Expect installation
system generally does not come with it, you need to install it yourself.
Installation command
yum install expect -y

Basic command

expect process command

spawn: start the process and track the subsequent interaction information
send: send a string to the process to simulate the user's input.
This command cannot automatically enter and line feed. Generally , an internal command of \r (carriage return)
expect
expect should be added . Whether the specified string is included in the secondary output result, if there is, it will be returned immediately, otherwise it will be returned after the timeout time.
Only the output of the process started by the spawn can be captured.
Interact: keep the interactive state after the execution is completed, and transfer the control to the console

expect content command

Timeout: Specify the timeout period, and continue to execute subsequent instructions when expired. The
unit is: second
timeout -1 means never timeout.
By default, timeout is 10 seconds.
exp_continue
allows expect to continue to execute the command
send_user
echo command, which is equivalent to the
basic echo command ( expect other commands)
$argv parameter array
Expect script can accept the parameters passed from bash, you can use [lindex $argc n] to get, n starts from 0, respectively represents the first, second, third...parameter
arg: Parameter
v: value

At the end of the Expect script, the
expect script must end with either interact or expect eof. Normally expect eof is sufficient to perform automated tasks.
Expect eof is waiting for the end sign. The command initiated by spawn will generate an eof tag at the end, and expect eof is waiting for this tag.
Expect execution mode.
Expect syntax
Single branch syntax
expect “password:” {send “mypassword\r”}

Multi-branch mode syntax
expect “aaa” {send “AAA\r”} expect “aaa” {send “AAA\r”} expect “aaa” {send “AAA\r”} The send command does not have the function of carriage return and line feed. So you need to add the \r or \n'
multi-branch mode syntax
expect {"aaa" {send "AAA\r"} "bbb" {send "BBB\r"} "ccc" {send "CCC\r"}} 'As long as any one of aaa or bbb or ccc is matched, the expect statement will exit after executing the corresponding send statement'
expect {"aaa" {send "AAA\r";exp_continue}"bbb" {send "BBB\ r";exp_continue}"ccc" {send "CCC\r"}}'exp_continue means to continue the subsequent matching. If aaa is matched, it will continue to match bbb down after the send statement is executed. "Capture the content with double quotes "Send" should be written in {}, the output information should also be enclosed in double quotation marks, semicolon ";" should be written in }'-
re parameter means that the regular expression is matched and
Expect is directly executed (take ssh as an example)
Among them, $argv 0 represents the location variable $1
$argv 1 represents the location variable $2
#!/usr/bin/expect is the path of the Expect binary file

[the root @ localhost ~] #vim a.sh
#! / usr / bin / Expect
# timeout
SET 20 is timeout
# log open
the log_file the test.log
# Display information
LOG_USER. 1
# define variable
SET hostname [the lindex the argv $ 0]
SET password [ lindex KaTeX parse error: Expected'EOF', got'#' at position 10: argv 1] # ̲Tracking instruction spawn ssh… {hostname}
#Capture prompt information
expect { “connecting (yes/no)” {send “yes\ r”;exp_continue} “*password:” {send “${password}\r”;} } #Transfer control interaction






Expect embedded execution (take ssh as an example)
[root@localhost~]#vim a.sh
#!/bin/bash
hostname=$1
password= KaTeX parse error: Expected'EOF', got'#' at position 3: 2 # ̲Expect embedded in /usr/b... {hostname} #Capture
prompt message
expect { "connecting (yes/no)" {send "yes\r"; exp_continue} " password:" {send "${password}\r";} } expect " ]#" send “exit\r” expect eof EOF








Summary of Expect script commands

#!/usr/bin/expect -re
tells the operating system which shell to execute the code in the script

  1. -re means enable regular expression matching

  2. set timeout -1
    Set timeout duration -1 means never expire, the default time is 10 seconds

  3. exp_continue
    means loop matching. After matching the changed keywords, continue to match from the beginning. If exp_continue is not added, it will exit directly

  4. Expect eof to
    match the end, for example, when the command is executed, it can be matched to eof

  5. exit, interact
    exit End of
    interaction Exit interact means to maintain the interactive state after execution, and hand over control to the console. At this time, you can operate manually

  6. exp_send/send
    both want to send a string to the program. I haven’t found any difference yet.

  7. send_user The
    send_user command is used to output the following parameters to the standard output. The silent send and exp_send commands all output the parameters to the program.

  8. How
    to use the ./XX.sh directly on the mac to execute the above script does not work. Need to use expect XX.sh to execute correctly

  9. Debugging
    expect -d XX.sh output each execution process can be used for debugging when writing scripts

Create a user and password
#! / Usr / bin / the Expect
# timeout
the SET timeout 20
# open log
log_file test02.log
# Display information
LOG_USER 1
# define variables
the SET useradd [lindex $ argv 0]
the SET password [lindex $ argv 1]
spawn useradd ${useradd}
expect “]#” #Tracking
instruction
spawn passwd KaTeX parse error: Expected'EOF', got'#' at position 11: {useradd} #̲Capture prompt message expect {… {password}\r"; exp_continue}
"Re-enter the new password:"
{send "${password}\r";}
}

Guess you like

Origin blog.csdn.net/weixin_46355881/article/details/107699134