Here Document interaction free
Cross free mutual definition
Here Document overview:
Use I/O redirection to provide a list of commands to interactive programs or commands, such as ftp, cat, or read commands
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
Syntax format
Syntax format
command << sign .... Input content ...... sign
Precautions for use of Here Document:
The tag can use any legal character (the common character is EOF)
The mark at the end must be written in the top grid, and there must be no characters (including spaces) in front of it
There must also be no characters (including spaces) after the tag at the end
Spaces before and after the opening mark are omitted
example
1. Output the input command line directly
2. Save the input content to a file
3. Input and save the additional content to the file
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, in addition to the conventional method, you can also use Here Document. If the written content contains variables, the variables should be replaced with the actual values before writing the file, and the writing is completed in combination with the cat command
Example:
1.When writing a file, the variable will be replaced with the actual value first, and then combined cat The command completes writing 2.Support variable replacement When writing a file, the variable will be replaced with the actual value first, and then combined cat The command completes writing [root@localhost ~]# vim kk.sh #!/bin/bash doc_file="yy.txt" i="0.0" cat > $doc_file << EOF kuai le $i EOF echo this is $i [root@localhost ~]# chmod +x kk.sh [root@localhost ~]# ./kk.sh this is 0.0 [root@localhost ~]# cat yy.txt kuai le 0.0
3. Assign a value to a variable as a whole, and then print the variable value through echo command
[root@localhost ~]# vim kk.sh #!/bin/bash ar="So Happy" arr=$(cat << EOF to be on holiday $ar EOF ) echo $arr [root@localhost ~]# chmod +x kk.sh [root@localhost ~]# ./kk.sh to be on holiday So Happy
Format control
Turn off the function of variable replacement and output the characters as they are without any modification or replacement
Example:
1. Turn off variable replacement: add a single quotation mark to the tag to turn off variable replacement
[root@localhost ~]# vim kk.sh #!/bin/bash i="hello" kk=$(cat <<'EOF' $i EOF ) echo $kk [root@localhost ~]# chmod +x kk.sh [root@localhost ~]# ./kk.sh $i
2. Remove the tab character before each line
Add '-' before the mark in the first line, which means to suppress the role of the first tab in each line
[root@localhost ~]# vim kk.sh #!/bin/bash cat <<EOF hello world EOF cat <<-EOF hello world EOF [root@localhost ~]# chmod +x kk.sh [root@localhost ~]# ./kk.sh hello world hello world
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 "#". The introduction of Here Document solves the problem of multi line annotation.
":" (colon) 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 ~]# vim kk.sh #!/bin/bash a=123456 : <<-EOF this is test line echo $a EOF echo "0.0" [root@localhost ~]# chmod +x kk.sh [root@localhost ~]# ./kk.sh 0.0
Expect
Expect definition
A tool based on tcl
For automatic control and testing
Solve interaction related problems in shell scripts
Expect installation
rpm -q expect rpm -q tcl yum install -y expect
Expect related commands
script interpreter
The expect script first introduces a file to indicate what kind of shell is used
#!/usr/bin/expect
spawn
Spawn is usually followed by a Linux execution command, which means to start a session, start a process, and track the subsequent interaction information, for example: spawn passwd root
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 time;
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
send
Send a string to the process to simulate the user's input;
This command cannot automatically enter and line feed. Generally, it needs to add \ r (enter) or \ n
Terminator
1.expect eof
Indicates the end of interaction, waiting for the end of execution, returning to the original user, and spawn corresponding For example, switch to root Users, expect By default, the script waits for 10 minutes s When the command is executed, it will stay 10 by default s After, it automatically switches back to the original user
2.interact
1.After execution, keep the interactive state and hand over the control to the console. It will stay at the target terminal without returning to the original terminal. At this time, it can be operated manually, interact Life after death.It doesn't work; such as interact Add after exit,It won't quit root User. And if not interact Then it will exit after login, rather than stay on the remote terminal. use interact Will remain at the terminal without returning to the original terminal;
set
expect The default timeout is 10 seconds set The command can set the session timeout. If the timeout is not limited, it should be set to-1 example: set time out 30 exp_continue exp_ continue Similar to in a control statement continue sentence. Express permission expect Continue to execute the instruction downward send_users Indicates echo commands and echo identical 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
set hostname [lindex $argv 0]
Equivalent to hostname=
set password [lindex $argv 1]
Equivalent to passswd=
example
1.ssh has no interactive login to the remote server
[root@localhost ~]# vim kk.sh #!/usr/bin/expect spawn ssh root@192.168.146.10 expect { "password:" { send "123456\r"; } } interact [root@localhost ~]# chmod +x kk.sh [root@localhost ~]# ./kk.sh spawn ssh root@192.168.146.10 root@192.168.146.10's password: Last login: Mon Feb 14 12:12:35 2022 from 192.168.146.20 [root@localhost ~]#
2. Operate on the remote login server and then exit
[root@localhost ~]# vim kk.sh #!/usr/bin/expect spawn ssh root@192.168.146.10 expect { "password:" { send "123456\r"; } } expect "#" send "ls\r" send "ifconfig ens33\r" send "exit\r" expect eof [root@localhost ~]# chmod +x kk.sh [root@localhost ~]# ./kk.sh spawn ssh root@192.168.146.10 root@192.168.146.10's password: Last login: Mon Feb 14 16:42:02 2022 from 192.168.146.20 [root@localhost ~]# ls anaconda-ks.cfg initial-setup-ks.cfg [root@localhost ~]# ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.146.10 netmask 255.255.255.0 broadcast 192.168.146.255 inet6 fe80::33b3:2fac:9c4b:5e89 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:01:3e:a5 txqueuelen 1000 (Ethernet) RX packets 210547 bytes 310074122 (295.7 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 98024 bytes 5939781 (5.6 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@localhost ~]# exit Log out Connection to 192.168.146.10 closed.
3. The second way to write SSH free interactive login
[root@localhost ~]# vim kkk.sh #!/usr/bin/expect set timeout 10 log_file kkk.log log_user 1 set ip [lindex $argv 0] set password [lindex $argv 1] spawn ssh root@${ip} expect { "(yes/no)" {send "yes\r"; exp_continue} "*password" {send "$password\r"} } interact [root@localhost ~]# chmod +x kkk.sh [root@localhost ~]# ./kkk.sh 192.168.146.10 123456 spawn ssh root@192.168.146.10 root@192.168.146.10's password: Last login: Mon Feb 14 21:57:36 2022 from 192.168.146.20 [root@localhost ~]#
[root@localhost ~]# vim yong.sh #!/bin/bash username=$1 useradd $username /usr/bin/expect <<-EOF spawn passwd $username expect { "password" {send "123456\r";exp_continue} "New password" {send "123456\r";} } EOF [root@localhost ~]# chmod +x yong.sh [root@localhost ~]# ./yong.sh nan spawn passwd nan Change user nan Your password. 123456 New password: Invalid password: password is less than 8 characters Re enter the new password: passwd: All authentication tokens have been successfully updated.
[root@localhost ~]# vim ftp.sh #!/usr/bin/expect -f set timeout 10 spawn ftp 192.168.146.10 expect "Name*" send "ftp\r" expect "Passwd:*" send "\r" expect "ftp>*" interact [root@localhost ~]# chmod +x ftp.sh [root@localhost ~]# ./ftp.sh spawn ftp 192.168.146.10 Connected to 192.168.146.10 (192.168.146.10). 220 (vsFTPd 3.0.2) Name (192.168.146.10:root): ftp 331 Please specify the password. Password: 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. ftp>
summary
1. Usage of here document
2.Expect basic commands
3.Expect usage