Here Document and expect of Shell programming are interactive free

Keywords: C++ Linux shell

introduction

Today, let's introduce the interactive free tools in the shell, which are used to realize automatic interactive tasks without human intervention. It can bring you convenience and make it easier for you to complete your work.

1, Here Document interaction free

1. General

  • Use I/O redirection to provide a list of commands to interactive programs or commands, such as ftp, cat, or read commands.
  • 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.

2. Syntax format

command <<sign
...
content         #Between tags is incoming content 
...
sign

Note:

  • Tags can use any legal character (usually EOF)
  • The mark at the end must be written in the top grid and cannot be preceded by any characters
  • There must be no characters (including spaces) after the tag at the end
  • Spaces before and after the opening mark are omitted

3. Application examples

  • Output the input directly from the command line
[root@localhost /home]#cat <<EOF
> hello 
> moto
> EOF
hello 
moto
  • Save input to file
[root@localhost /home]#cat >name<<EOF
> zhangsan
> lisi
> wangwu
> EOF
[root@localhost /home]#cat name
zhangsan
lisi
wangwu
  • Append input save to file
[root@localhost /home]#cat >>name<<EOF
> xiaoming
> EOF
[root@localhost /home]#cat name 
zhangsan
lisi
wangwu
xiaoming
  • Count the number of rows
[root@localhost /home]#wc -l <<EOF
> test1
> test2
> test3
> EOF
3
  • Assign values to variables
#The input is received and printed through the read command. The input value is the part between the two EOF tags as the value of variable a
[root@localhost /home]#read a <<EOF
> 8
> EOF
[root@localhost /home]#echo $a
8
  • No interaction setting password
[root@localhost /home]#useradd laowang
[root@localhost /home]#passwd laowang <<EOF
> 123456
> 123456
> EOF
 Change user laowang Your password.
New password: invalid password: password is less than 8 characters
 Re enter the new password: passwd: All authentication tokens have been successfully updated.
  • Variable replacement
[root@localhost /home]#vim test.sh
#!/bin/bash
doc_file="gkd.txt"
i="nanjing"
cat > $doc_file <<EOF
welcome to $i
EOF

[root@localhost /home]#sh test.sh 
[root@localhost /home]#cat gkd.txt 
welcome to nanjing
  • Assign a value to the variable as a whole, and print the variable value with echo
[root@localhost /home]#vim test2.sh
#!/bin/bash
test="you are very good!"
mylife=$(cat <<EOF 
Rush!
It's over!!!
$test
EOF
)
echo $mylife

[root@localhost /home]#sh test2.sh 
Rush! It's over!!! you are very good!
  • Turn off variable replacement
#You can turn off variable substitution by placing single quotes on the tag
[root@localhost /home]#aa=$(cat <<'EOF'
> 10
> $a
> EOF
> )
[root@localhost /home]#echo $aa
10 $a
[root@localhost /home]#echo $a
8
  • Remove the TAB character before each line
[root@localhost /home]#vim test3.sh
#!/bin/bash
cat <<EOF
        hello
EOF
cat <<EOF
        word
EOF

[root@localhost /home]#sh test3.sh 
	hello
	word

[root@localhost /home]#vim test3.sh     #Adding "-" before EOF can remove the tab at the beginning of the line
#!/bin/bash
cat <<-EOF
        hello
EOF
cat <<-EOF
        word
EOF

[root@localhost /home]#sh test3.sh     #After "-" is added, the displayed content will be displayed in the top grid
hello
word
  • multiline comment
  • Bash's default annotation is "#". This annotation method only supports single line annotation. In the work of Shell script, bash will ignore any string to the right of "#".
  • ":" represents an empty command that does nothing. The contents of the middle mark area will not be executed and will be ignored by bash, so the effect of batch annotation can be achieved.
[root@localhost /home]#vim test4.sh

#!/bin/bash
: <<EOF                              #A colon indicates a multiline comment, and the beginning of the tag will not be executed
hello?
who are you?
EOF
echo "What is the matter with you?"

[root@localhost /home]#sh test4.sh 
What is the matter with you?

2, Expect no interaction

1. General

  • expect is developed by Don Libes based on Tcl (Tool Command Language). It is mainly used in the scene of automatic interactive operation. It is a free programming tool to realize automatic interactive tasks without human intervention.
  • To put it bluntly, expect is a set of software used to realize automatic interaction.

2. Environmental installation

expect can't be explained by the shell, so if you want to execute it to complete your script, you need to install two software packages first. Of course, when using yum, we can execute this command:

[root@localhost /home]#yum install -y expect

3. Basic commands

3.1 script interpreter

  • The expect script first introduces a file to indicate which shell is used
#!/usr/bin/expect

3.2 spawn

  • spawn is usually followed by a Linux execution command to start a session, start a process, and track subsequent interaction information
spawn passwd root

3.3 expect

  • Judge whether the last input result contains the specified string. If so, it will be returned immediately. Otherwise, it will be returned after the timeout
  • Only the output of processes started by spawn can be captured
  • It is used to receive the output after the command is executed, and then match the expected string

3.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
expect "password" {send "abc123\r"}   
or
expect "password"
send "abc123\r"

#The send part of the same line must have {} line feed, and the send part does not need {}
expect          
"Password 1"{ send "abc123\r"}
"Password 2"{ send "123456\r"}
"Password 3"{ send "123123\r"}

#Expect supports multiple branches. As long as one of them is matched, execute the corresponding send statement and exit the expect statement

3.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 expect script waits for 10s by default. After executing the command, it stays for 10s by default and will automatically switch back to the original user.

  • interact
    ① After execution, keep the interactive state, hand over the control to the console, and stay at the target terminal. At this time, you can operate manually. The commands after interaction do not work
    ② For example, the interact ion will remain on the terminal and will not return to the original terminal. For example, if you switch to the root user, it will always be in the root user state
    ③ For example, when ssh goes to another server, it will always be on the target server terminal, rather than switching back to the original server

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

3.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
set timeout 20

3.7 exp_continue

  • exp_ After continue is attached to an expect judgment item, you can continue to match other items in the expect judgment statement after the item is matched

  • exp_continue is similar to the continue statement in the control statement, indicating that expect is allowed to continue to execute instructions downward

  • For example, it will judge whether there is yes/no or * password in the interactive output. If yes/no is matched, it will output yes and execute the judgment again; If * password is matched, output 123123 and end the expect statement. The operation is as follows:

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

3.8 send_ user

  • send_ user means echo command, which is equivalent to echo

3.9 receiving parameters

  • The expect script can accept the parameters passed from the bash command line and obtain them using [lindex $argv n].
  • Where n starts from 0 and represents the first, second and third... Parameters respectively.
set hostname [lindex $argv 0]   	#Equivalent to hostname= 
set password [lindex $argv 1]		#Equivalent to password=

#Expect is executed directly. You need to use the expect command to execute the script

4. Application examples

  • ssh no interactive login to the remote server
[root@localhost /home]#vim expect.sh

#!/usr/bin/expect                   #You need to use expect's own interpreter. Don't write it as bash, otherwise it won't be recognized
spawn ssh root@192.168.8.129        #Open a program, which is ssh Remote Login
expect {                            #Capture the content. When password appears, it will send the password to the program
         "password:"
        { send "123456\r"; }
}
interact                            #Interaction, otherwise it will exit the remote server directly

[root@localhost /home]#chmod +x expect.sh              #Need to add execution permission
[root@localhost /home]#./expect.sh  
spawn ssh root@192.168.8.129
root@192.168.8.129's password: 
Last login: Wed Sep 15 22:39:40 2021 from 192.168.8.132
  • The following script can be executed after performing an operation on the opposite server and then exiting
[root@localhost /home]#vim expect.sh

#!/usr/bin/expect
spawn ssh root@192.168.8.129
expect {
         "password:"
        { send "123456\r"; }
}
expect "#"                      #When captured#When
send "ls\r"                     #Execute ls command
send "ifconfig ens33\r"         #Execute ifconfig ens33 command
send "exit\r"                   #Log out after exit
expect eof                      #No interaction means the end of the expect program
 
[root@localhost /home]#chmod +x expect.sh 
[root@localhost /home]#./expect.sh 
spawn ssh root@192.168.8.129
root@192.168.8.129's password: 
Last login: Wed Sep 15 22:55:06 2021 from 192.168.8.132
[root@localhost ~]#ls
[root@localhost ~]#ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.8.129  netmask 255.255.255.0  broadcast 192.168.8.255
        inet6 fe80::2161:befa:6ffd:c44b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:01:e6:0a  txqueuelen 1000  (Ethernet)
    ......
[root@localhost ~]#exit
 Logout
Connection to 192.168.8.129 closed.
  • Reference location variable
[root@localhost /home]#vim expect1.sh 
#!/usr/bin/expect
set user root
set ip [lindex $argv 0]                #Set the first location variable to ip
set pass [lindex $argv 1]              #Set the second location variable as the login password
spawn ssh $user@$ip
expect {
        "password:"
        { send "$pass\r"; }
}
expect "#"
send "ls\r"
send "exit\r"
expect eof

[root@localhost /home]#chmod +x expect1.sh 
[root@localhost /home]#./expect1.sh 192.168.8.129 123456
spawn ssh root@192.168.8.129
root@192.168.8.129's password: 
Last login: Wed Sep 15 22:58:16 2021 from 192.168.8.132
[root@localhost ~]#ls
[root@localhost ~]#exit
 Logout
Connection to 192.168.8.129 closed.
  • Create user and set user password
[root@localhost /home]#vim user.sh

#!/bin/bash
username=$1
useradd $username
/usr/bin/expect <<-EOF
spawn passwd $username
expect {                                  #The obtained content and the sent content cannot be on the same line, otherwise the execution will not succeed
        "New password"
        { send "123456\r";exp_continue }
        "Re enter the new password"
        { send "123456\r"; }
}
EOF

[root@localhost /home]#chmod +x user.sh 
[root@localhost /home]#./user.sh gebilaowang
spawn passwd gebilaowang
 Change user gebilaowang Your password.
New password:
Invalid password: password is less than 8 characters
 Re enter the new password:
passwd: All authentication tokens have been successfully updated.

summary

  • With the command of expect dealing with interaction, the interaction process such as ssh login and ftp login can be written in a script to complete it automatically.
  • It is applicable to the environment where multiple servers need to perform the same operation, which can greatly improve the work efficiency of system managers. With it, you can even execute only one script to complete the construction and maintenance of distributed application system.

Posted by bluebutterflyofyourmind on Wed, 15 Sep 2021 16:44:14 -0700