Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of Expect Automation Interactive Application example

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report