5, Interaction free shell programming

5, Interaction free shell programming

1, Here Document

(1) Here Document overview

  1. Provides a list of commands to interactive programs using I/O redirection

  2. An alternative to standard input

    Here Document is an alternative to standard input, which can help script developers not use temporary files to build input information, but directly produce a file in place and use it as standard input for commands. Here Document can be used with non interactive programs and commands

  3. Syntax format

command <<sign

...

...

sign

(2) Here Document usage considerations

  1. The tag can use any legal character (the common character is EOF)
  2. The ending mark must be written in the top grid, and there must be no characters (including spaces) in front of it
  3. There must be no characters (including spaces) after the tag at the end
  4. Spaces before and after the opening mark are omitted
[root@localhost ~]# cat >> test.txt <<EOF
     
> HELLO
> 233
> EOF
[root@localhost ~]# cat test.txt 

HELLO
233

(3) Here Document variable setting

Here Document also supports the use of variables. If variables are used between tags, the variable value will be replaced first.

If you want to write some content to a file, you can also use Here Document in addition to the conventional method.

If the contents to be written contain variables, the variables must be replaced with the actual values before writing the file, and the writing is completed in combination with the cat command.

Add a "" perfect reference to the referenced variable

[root@localhost ~]# ./eof.sh 
this is my day today i will good study this is good day

[root@localhost ~]# vim eof.sh
#!/bin/bash
var="today i will good study"
myvar=$(cat <<EOF
this is my day
$var
this is good day
EOF
)

echo "$myvar"
[root@localhost ~]# ./eof.sh 
this is my day 
today i will good study
this is good day


Adding "" to the tag or not to reference variables shows the original meaning

[root@localhost ~]# vim eof.sh
#!/bin/bash
var="today i will good study"
myvar=$(cat <<"EOF"
this is my day
$var
this is good day
EOF
)

echo "$myvar"
~                
[root@localhost ~]# ./eof.sh 
this is my day
$var
this is good day


Precede the tag with - the tab key is not displayed, but the spacebar is displayed

var="today i will good study"
myvar=$(cat <<-"EOF"
                 this is my day
        $var
                                this is good day
EOF
)

echo "$myvar"

[root@localhost ~]# ./eof.sh 
                 this is my day
$var
this is good day


2, Expect

(1) Expect installation

  • Mount CD
  • Make local yum source
  • Execute installation command
yum -y install expect
rpm -qa|grep expect
rpm -qa|grep tcl

(2) Definition

It is a tool based on tcl language. It is often used for automatic control and testing to solve the problems related to interaction in shell scripts

(3) Related commands

(1) Script interpreter

The expect script first introduces a file to indicate what kind of shell to use

#!/usr/bin/expect

(2)spawn

spawn is usually followed by a Linux execution command to open a session, process, and track subsequent interaction information

Example:

spawn passwd root

(3)expect

Judge whether the last output result contains the specified string. If so, it will be returned immediately. Otherwise, it will be returned after the timeout period; Only the process output started by swpan can be captured;

It is used to accept the output after the command is executed, and then match the expected string

(4)send

Send a string to the process to simulate the user's input: this command cannot automatically enter and wrap lines. Generally, it needs to add \ r (enter) or \ n

(5) Terminator

expect eof

Indicates the end of interaction, waits for the end of execution, and returns to the original user, corresponding to spawn

For example, when switching to the root user, the default waiting time of the expect script is 10s. When the king command is executed, it stays for 10s by default and automatically switches back to the original user

interact

After execution, maintain the interactive state and hand over the control to the console. It will stay at the target terminal instead of returning to the original terminal. At this time, you can operate manually. The command after interaction will no longer work. For example, adding exit after interaction will not exit the root user. If there is no interaction, it will exit after login, rather than stay on the remote terminal.

Using interact will remain on the terminal without returning to the original terminal. For example, switching to the root user will always be in the root user state; For example, ssh to another server will always be on the target server terminal without switching back to the original server.

It should be noted that only one of expect eof and interact can be selected.

(6)set

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

example:

set time out 30

(7) exp_continue

exp_continue indicates that expect is allowed to continue executing instructions downward

exp_ After continue is attached to an expect judgment option, it can continue to match other items in the expect judgment statement after the item is matched. exp_continue is similar to the continue statement of the control statement. Indicates that expect is allowed to continue executing commands downward.

(8)send_user

Indicates that the echo command is the same as echo

(9) Receive parameters

The expect script can accept passing parameters from the bash command line and obtain them using [lindex $argv n]. Where you start from 0 and represent the first, second and third... Parameters respectively

example:

set hostname  [lindex $argv 0]     amount to hostname=$1

set password [lindex $argv 1]        amount to passswd=$2

Interactive free ssh

#!/usr/bin/expect
set timeout 5
set hostname [lindex $argv 0]
set passwd [lindex $argv 1]

spawn ssh $hostname
expect {
"No route to host" exit
"Connection refused" exit
"(yes/no)?" {send "yes\n";exp_continue}
"password:" {send "${passwd}\n"}
}
interact
~                

[root@localhost ~]# ./ssh.sh 192.168.254.10 123123
spawn ssh 192.168.254.10
root@192.168.254.10's password: 
Last login: Thu Oct 28 10:53:15 2021 from 192.168.254.20

summary

Learn to use interaction free commands, so you don't need to participate in too much interaction in the script

Keywords: shell bash

Added by michaelphipps on Tue, 07 Dec 2021 10:07:58 +0200