Interaction free Shell programming

Keywords: Linux

catalogue

introduction

1, Here Document interaction free

1. Here Document overview

2. Basic syntax format

3. Special character "< <"

4. Precautions

2, Here Document general usage

1. Output the input directly from the command line

2. Save input to file

3. Append input save to file

4. Save input to file

5. Create yum source

6. Receive input and print through the read command

7. Set the password for the user through passwd

8. Replace variable with actual value

9. Assign the whole to a variable

10. Turn off variable replacement

11. The tag is preceded by '-' to suppress the effect of the first TAB in each row

12. ":" represents an empty command that does nothing

2, Expect

1. expect overview

2. Basic command

4, Expect general usage

1. ssh no interactive login to the remote server

2. If you want to perform an operation on the other server before exiting, you can execute the following script

3. Use the set keyword to define variables. Variable names and variable values are separated by spaces. Other uses are consistent with shell scripts

4. Create user and set user password

summary

introduction

In the process of writing shell scripts, some operations generally need to use the specified parameters. At this time, the use of interaction free can be fast and convenient.

1, Here Document interaction free

1. Here Document overview

(1) Use I/O redirection to provide a list of commands to interactive programs or commands, such as ftp, cat, or read commands.

(2) An alternative to standard input 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. Basic syntax format

command <<sign
......
(content)
......
sign

3. Special character "< <"

Before marking and commands, the purpose of this is to redirect the output of the command block to the stdin of the program or command. The marking shall be selected to ensure that it will not appear elsewhere and avoid confusion; The content between the two tags is treated as a file and used as standard input for the command. In addition, Here Document can also be used with non interactive programs and commands.

4. Precautions

(1) Any legal character can be used for marking;

(2) The mark at the end must be written in the top grid without any characters in front;

(3) There shall be no characters (including spaces) after the tag at the end;

(4) The space before and after the beginning mark is omitted.

2, Here Document general usage

1. Output the input directly from the command line

[root@localhost ~]# cat <<EOF> hello world
> EOF
hello world

[root@localhost ~]# cat <<EOF
> hello
> world
> EOF
hello
world

2. Save input to file

[root@localhost ~]# cat > name.txt <<EOF
> lisi
> zhangsan
> wangwu
> EOF
[root@localhost ~]# cat name.txt 
lisi
zhangsan
wangwu

3. Append input save to file

[root@localhost ~]# cat >> name.txt  <<EOF
> zhaosi
> liuda
> EOF
[root@localhost ~]# cat name.txt 
lisi
zhangsan
wangwu
zhaosi
liuda

4. Save input to file

[root@localhost file]# tee test1.txt <<EOF
> zhangsan
> lisi
> wangwu
> EOF
zhangsan
lisi
wangwu
[root@localhost file]# cat test1.txt 
zhangsan
lisi
wangwu

5. Create yum source

[root@localhost file]# cat >local.repo <<EOF
> [local]
> name=local
> baseurl=file:///mnt
> enbaled=1
> gpgcheck=0
> EOF
[root@localhost file]# cat local.repo 
[local]
name=local
baseurl=file:///mnt
enbaled=1
gpgcheck=0




[root@localhost file]# cat <<EOF > local1.repo 
> [local]
> name=local
> baseurl=file:///mnt
> enbaled=1
> gpgcheck=0
> EOF
[root@localhost file]# cat local.repo 
[local]
name=local
baseurl=file:///mnt
enbaled=1
gpgcheck=0

6. Receive input and print through the read command

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

7. Set the password for the user through passwd

[root@localhost file]# useradd lisi
[root@localhost file]# passwd lisi <<EOF
> 123456
> 123456
> EOF
 Change user lisi 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.

8. Replace variable with actual value

[root@localhost file]# aa=$(cat <<EOF
> $a
> EOF
> )
[root@localhost file]# echo $aa
123

[root@localhost file]# vim test1.sh
#!/bin/bash
doc_file="file1.txt"
i="file1"
cat > $doc_file <<EOF
 Please put the software into $i
EOF
[root@localhost file]# sh test1.sh 
[root@localhost file]# cat file1.txt 
Please put the software into file1

9. Assign the whole to a variable

[root@localhost file]# vim TX.sh
#!/bin/bash
ivar="Great! Beautyful!"
myvar=$(cat <<EOF     #(assign the whole Here Document to the variable)
this is Line 1.
tan are Sun, Moon and starts.
$ivar     #(variable replacement will be performed during output)
EOF
)
echo $myvar
[root@localhost file]# sh TX.sh 
this is Line 1. tan are Sun, Moon and starts. Great! Beautyful!

10. Turn off variable replacement

[root@localhost file]# aa=$(cat <<'EOF'
> 20
> $a
> EOF
> )
[root@localhost file]# echo $aa
20 $a
[root@localhost file]# echo $a
123

11. The tag is preceded by '-' to suppress the effect of the first TAB in each row

[root@localhost file]# vim TX2.sh
#!/bin/bash
cat <<EOF     
        hello world
EOF

cat <<-EOF
        hello world
EOF
[root@localhost file]# sh TX2.sh 
	hello world
hello world

12. ":" represents an empty command that does nothing

[root@localhost file]# vim TX3.sh
#!/bin/bash
:  cat <<-EOF
the second comment.
test line.
EOF
echo "Please download the software!"
[root@localhost file]# sh TX3.sh 
Please download the software!

2, Expect

1. expect overview

(1) A tool based on tcl language is often used for automatic control and testing to solve the problems related to interaction in shell scripts.

(2) expect is a program, so it needs to be installed in advance before it can be used.

[root@localhost file]# rpm -q expext
 Package not installed expext 
[root@localhost file]# rpm -q tcl
 Package not installed tcl 
[root@localhost file]# yum install expect  tcl -y
[root@localhost ~]# rpm -q expect
expect-5.45-14.el7_1.x86_64
[root@localhost ~]# rpm -q tcl
tcl-8.5.13-8.el7.x86_64
[root@localhost ~]# which expect 
/usr/bin/expect
[root@localhost ~]# which tcl
/usr/bin/which: no tcl in (/usr/local/sbin:/usr/local/bin:/usr/sb
in:/usr/bin:/root/bin)

2. Basic command

(1) Script interpreter

The expect script first introduces a file to indicate which shell is used. (example: #! / usr/bin/expect)

(2)spawn

Spawn is usually followed by a Linux execution command to start a session, start a 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 output of the process started by spawn can be captured; It is used to receive 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.

Mode 1:
expect "password"{ send "abc123\r"}  #The send part of the same line must have {}

Mode II:
expect"password"
send "abc123\r"     #The newline send part does not need to have {}

Mode III:
expect Support multiple branches
expect          #As long as one of the cases is matched, execute the corresponding send statement and exit the expect statement
"Password 1"{ send "abc123\r"}
"Password 2"{ send "123456\r"}
"Password 3"{ send "123123\r"}

(5) Terminator

①expect eof

It indicates that the interaction ends, waits for the execution to end, and returns to the original user, which corresponds to spawn. For example, when switching to root, the expect script defaults to wait for 10s. When the command is executed, it stays for 10s by default, and automatically switches back to the original user.

②interact

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

If you use interact, you will stay on the terminal and will not return to the original terminal. For example, if you switch to root, you will always be in the root user state; for example, if you ssh to another server, you will always be on the target server terminal and will not switch back to the original server.

Note: 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 timeout 30)

(7)exp_continue

After exp_continue is attached to an expect judgment item, 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 in the control statement. It means that expect is allowed to continue to execute instructions downward.

For example, the following example will judge whether yes/no or * password exists in the interactive output. If yes/no is matched, yes will be output and the judgment will be executed again; if * password is matched, abc123 will be output and the expect statement will be ended.

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

be careful:

① When exp continue is used, if a command such as passwd is followed to end the process after entering a password, expect eof should not be added to expect(}), because after the spawn process ends, eof will be sent to expect by default, resulting in an error in the execution of the following expect eof.

② Indicates that users are allowed to interact and maintain session connection all the time.

(8)send user

send_ user indicates echo command, which is equivalent to echo

(9) Receive 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 O and represents the first, second and third.. parameters respectively.

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

4, Expect general usage

1. ssh no interactive login to the remote server

[root@localhost /]# vim dome1.sh
#!/usr/bin/expect     #(you need to use expect's own interpreter here. Be careful not to write it as bash, otherwise it won't be recognized)

spawn ssh root@192.168.32.130     #(start a program, which is ssh Remote Login)

expect {
        "password:"
        { send "123456\r"; }     #(capture the content. When the password appears, it will send the password to the program. By default, there is no line feed, so you need to enter to feed the line. Multiple conditions need to be enclosed in curly brackets. Pay attention to the format!)
}

interact     #(interactive, otherwise it will exit the remote server directly)

:wq

[root@localhost /]# chmod +x dome1.sh 
[root@localhost /]# ./dome1.sh 
spawn ssh root@192.168.32.130
root@192.168.32.130's password: 
Last login: Wed Sep 15 12:22:52 2021 from 192.168.32.1

2. If you want to perform an operation on the other server before exiting, you can execute the following script

[root@localhost /]# vim dome2.sh
#!/usr/bin/expect

spawn ssh root@192.168.32.130

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"     #(exit and log out after executing exit)

expect eof     #(no interaction is required, which means that the expect program ends. If you don't write, the operation will not be executed and you can exit directly; if you don't write, you can't execute commands on the other machine by writing interact, and eof can be replaced
)

:wq

[root@localhost ~]# chmod +x dome2.sh 
[root@localhost ~]# ./dome2.sh 
spawn ssh root@192.168.32.130
root@192.168.32.130's password: 
Last login: Wed Sep 15 12:35:08 2021 from 192.168.32.128
[root@localhost ~]# ls
1 = 0                 letest.txt  web.sh       Public download
1.txt                 lstest.txt  wjf          Template music
2.txt                 test.txt    xxx.tar.bz2  Video desktop
anaconda-ks.cfg       tmp.tar.gz  xxx.tar.gz   picture
initial-setup-ks.cfg  WEBJK.sh    yum.conf     file
[root@localhost ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.32.130  netmask 255.255.255.0  broadcast 192
.168.32.255        inet6 fe80::c5b8:3bdf:ea88:c9a3  prefixlen 64  scopeid 0x
20<link>        ether 00:0c:29:80:36:1a  txqueuelen 1000  (Ethernet)
        RX packets 64  bytes 7167 (6.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 62  bytes 10322 (10.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 
0
[root@localhost ~]# exit
 Logout
Connection to 192.168.32.130 closed.

3. Use the set keyword to define variables. Variable names and variable values are separated by spaces. Other uses are consistent with shell scripts

[root@localhost /]# vim dome3.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 login password)

spawn ssh $user@$ip

expect {
        "password:"
        { send "$pass\r"; }

}

expect "#"
send "ls\r"
send "exit\r"

expect eof
                                                              
:wq  

[root@localhost /]# chmod +x dome3.sh 
[root@localhost /]# ./dome3.sh 192.168.32.130 123456     #(position variable needs to be added during execution)
spawn ssh root@192.168.32.130
root@192.168.32.130's password: 
Last login: Wed Sep 15 12:24:59 2021 from 192.168.32.128
[root@localhost ~]# ls
1 = 0                 letest.txt  web.sh       Public download
1.txt                 lstest.txt  wjf          Template music
2.txt                 test.txt    xxx.tar.bz2  Video desktop
anaconda-ks.cfg       tmp.tar.gz  xxx.tar.gz   picture
initial-setup-ks.cfg  WEBJK.sh    yum.conf     file
[root@localhost ~]# exit
 Logout
Connection to 192.168.32.130 closed.

4. Create user and set user password

[root@localhost ~]# vim dome4.sh
#!/bin/bash
username=$1

useradd $username

/usr/bin/expect <<-EOF

spawn passwd $username

expect {
        "New password"
        { send "123456\r";exp_continue }
        "Re enter the new password"
        { send "123456\r"; }
}
EOF

:wq

[root@localhost ~]# ./dome4.sh liwu
useradd: User“ liwu"Already exists
spawn passwd liwu
 Change user liwu 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

1. Interaction free is to provide a command list to an interactive program or command by using I/O redirection.
2. Interaction free is an alternative to standard input.
3. A tool based on tcl language is often used for automatic control and testing to solve the problems related to interaction in shell scripts.

Posted by B of W on Wed, 15 Sep 2021 12:08:52 -0700