Get the App
SLTechnology News&Howtos  ›  Servers  › 

Analysis of Expect Automation Interactive Application example

Shulou Source: shulou.com Published: 2022-06-03 03:20:40 10月04日 Update

Expect is an automatic interaction suite, which is mainly used in the execution of commands and programs, the system is required to input a specified string in the form of interaction to achieve interactive communication.

Expect automatic interaction process:

Spawn starts the specified process-expect gets the specified keyword-send sends the specified character to the specified program-execute completion exit

Simple example 1:

Vim test.exp # # exp is the extension of expect script

#! / usr/bin/expect # # use the expect parser

Spawn ssh root@192.168.1.5 free # # execute ssh command with spawn execute free command after login

Expect "* password" # # matches the acquired string

Send "123456\ n" # # send the password to the system after matching.\ nIt's a newline.

Expect eof # # end of processing expect

Example 2:

#! / usr/bin/expect

Spawn ssh root@192.168.1.5 free

Expect {

"yes/no" {exp_send "yes\ r"; exp_continue}

"* password" {exp_send "123456\ r"}

}

Expect eof

Example 3:

#! / usr/bin/expect

Spawn test.sh

Expect {

"username" {exp_send "test\ r"; exp_continue}

"* password" {exp_send "123456\ r"; exp_continue}

"* mail*" {exp_send "lidao@163.com\ r"}

}

Expect eof

Summary of common commands in expect

The spawn interactive program starts with a command or a specified program

If the matching information is obtained by expect, the program action behind expect will be executed if the match is successful.

Send exp_send is used to send specified string information

Exp_continue needs to be used for multiple matches in expect.

Send_user is used to print out the equivalent of echo in shell.

Exit exits the expect script

Eof expect execution ends and exits

Set defines variables

Puts output variable

Set timeout sets the timeout

Example 4:

#! / usr/bin/expect

If {$argc! = 2} {

Puts "expect $argv0 ip command"

Exit

}

Set ip [lindex $argv 0]

Set cmd [lindex $argv 1]

Set password "123456"

Spawn ssh root@$ip $cmd

Expect {

"yes/no" {send "yes/r"; exp_continue}

"* password" {send "$password\ r"}

}

Expect eof

Expect test.exp 192.168.1.5 "free-m"

Instance 5:ssh key authentication

Generate secret key

Ssh-keygen-t dsa-P''- f ~ / .ssh/id_dsa > / dev/null 2 > & 1

Vim test.exp

#! / usr/bin/expect

If [$argc! = 2] {

Send_user "usage: expect test.exp file host\ n"

Exit

}

Set file [lindex $argv 0]

Set host [lindex $argv 1]

Set password "123456"

Spawn ssh-copy-id-I $file "- p 22 root@$host"

Expect {

"yes/no" {send "yes\ r"; exp_continue}

"* password" {send "$password\ r"}

}

Expect eof

Vim kk.sh Loop expect script

#! / bin/bash

For n in 2 3 4 5 6 7

Do

Expect test.exp ~ / .ssh/id_dsa.pub 192.168.1.roomn

Done

Expect is easy to operate, easy to use, easy to learn and use, but now it is not used much in work, and most of it is used to assist the execution of other program scripts.

Tags: Programs examples commands characters scripts strings information variables systems outputs applications automation success usage keywords actions packages passwords commonly used Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Information NVidia Apple Microsoft