Shell programming interaction free expect

Time:2020-2-26

CentOS-Logo

This article is mainly aboutshellScript interaction freeexpectUse.


Summary

ExpectIs built ontclBased on a tool,ExpectIt is a tool for automatic control and testing. Main solutionshellNon interactive issues in scripts.

install

  • Install before using this tool
yum install -y expect

Basic command

send

  • Send string to process to simulate user input

  • This command does not automatically return to the vehicle for line feed. It is usually added\r(return)

expect

  • expectAn internal command to determine whether the last output contains the specified string. If so, it will be returned immediately. Otherwise, it will be returned after the timeout.

  • Only byspawnOutput of the started process

spawn

  • Start the process and track subsequent interaction information

interact

  • Keep the interaction state after execution and give control to the console

timeout

  • Specify the time-out time, and continue to execute subsequent instructions after expiration

  • Unit: Second

  • timeout -1Never timeout

  • By default,timeoutyes10second

exp_continue

  • allowexpectContinue to execute command down

send_user

  • Echo command, equivalent toecho

$argv parameter array

  • expectScripts can accept frombashParameters passed. You can use the[lindex $argv n]Get,nfrom0Start, first, second, third… Parameter

Expect script

  • expectScript must beinteractorexpect eofEnd, perform automation tasks usuallyexpect eofThat’s enough.

  • expect eofIs waiting for the end flag. fromspawnAt the end of a initiated command, aeofMark,expect eofWaiting for this tag

Expect syntax

  • Single branch
expect "password:" {send "mypassword\r";}
  • Multiple branches
expect "aaa" {send"AAA\r"}
expect "aaa" {send"AAA\r"}
expect "aaa" {send"AAA\r"}

sendThe command does not have the function of return and line feed, which is usually added\ror\n

expect {
    "aaa" {send "AAA\r"}
    "bbb" {send "BBB\r"}
    "ccc" {send "CCC\r"}
}

Just configureaaaorbbborcccAny one of, execute the correspondingsendExit theexpectSentence

expect {
    "aaa" {send "AAA";exp_continue}
    "bbb" {send "BBB";exp_continue}
    "ccc" {send "CCC"}
}

exp_continueIndicates to continue the following matching, if it doesaaaAfter executionsendContinue to match down after statementbbb

Mode of execution

  • Basic grammatical structure
Spawn command
Expect "tips"
Send "instead of a manually entered string \ R"
  • Direct execution
#!/usr/bin/expect
#Timeout
set timeout 20
log_file test.log
log_user 1
#Parameter in
set hostname [lindex $argv 0]
set password [lindex $argv 1]
#Trace command
spawn ssh [email protected]$hostname
#Capture and match information without interactive execution
expect {
    "(yes/no)" {send "yes\r";exp_continue}
    "*password" {send "$password\r"}
}
#Control is delegated to the console
interact
[[email protected] ~]# yum install expect -y
[[email protected] ~]# vim ssh.sh
[[email protected] ~]# chmod +x ssh.sh 
[[email protected] ~]# ./ssh.sh 192.168.28.129 000000
spawn ssh [email protected]
The authenticity of host '192.168.28.129 (192.168.28.129)' can't be established.
ECDSA key fingerprint is SHA256:QmZtJT0piBUSkF9P3GfYf3uEogzBWs08sI7j0eBE/cI.
ECDSA key fingerprint is MD5:ef:e6:06:22:8a:0f:24:00:f8:af:a5:59:5b:a2:b8:b1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.28.129' (ECDSA) to the list of known hosts.
[email protected]'s password: 
Last login: Thu Oct 17 09:35:35 2019
[[email protected] ~]#
  • Embedded execution
#!/bin/bash
hostname=$1
password=$2
/usr/bin/expect <

-EOFFault tolerantTab tab

[[email protected] ~]# vim ssh.sh 
[[email protected] ~]# ./ssh.sh 192.168.28.129 000000
spawn ssh [email protected]
[email protected]'s password: 
Last login: Thu Oct 17 09:38:23 2019 from 192.168.28.128
[[email protected] ~]# exit
logout
Connection to 192.168.28.129 closed.
[[email protected] ~]#

Case 1 useradd

#!/bin/bash
username=$1
password=$2
useradd $username
/usr/bin/expect << EOF
spawn passwd $username
expect "New password:"
send "$password\r"
expect "Retype new password:"
send "$password\r"
expect eof
EOF
[[email protected] ~]# vim useradd.sh
[[email protected] ~]# chmod +x useradd.sh 
[[email protected] ~]# ./useradd.sh zhangsan 000000
spawn passwd zhangsan
Changing password for user zhangsan.
New password: 
BAD PASSWORD: The password is a palindrome
Retype new password: 
passwd: all authentication tokens updated successfully.

Case 2 SSH

#!/usr/bin/expect
#Timeout
set timeout 20
log_file test.log
log_user 1
#Parameter in
set hostname [lindex $argv 0]
set password [lindex $argv 1]
#Trace command
spawn ssh [email protected]$hostname
#Capture and match information without interactive execution
expect {
    "Connection refused" exit
    "Name or service not known" exit
    "(yes/no)" {send "yes\r";exp_continue}
    "*password" {send "$password\r"}
}
#Control is delegated to the console
interact
exit
[[email protected] ~]# systemctl stop sshd
[[email protected] ~]# ./ssh.sh 192.168.28.129 000000
spawn ssh [email protected]
ssh: connect to host 192.168.28.129 port 22: Connection refused
[[email protected] ~]#
[[email protected] ~]# systemctl start sshd
[[email protected] ~]# ./ssh.sh host02 000000
spawn ssh [email protected]
ssh: Could not resolve hostname host02: Name or service not known
[[email protected] ~]#
[[email protected] ~]# ./ssh.sh 192.168.28.129 000000
spawn ssh [email protected]
[email protected]'s password: 
Last login: Thu Oct 17 09:49:38 2019
[[email protected] ~]#