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

Example Analysis of Expect interaction-free in Shell programming

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

Share

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

This article is about Shell programming Expect interaction-free example analysis, the editor feels very practical, so share with you to learn, I hope you can learn something after reading this article, say no more, follow the editor to have a look.

Overview of Expect

Expect is a tool based on tcl, and Expect is a tool for automatic control and testing. Mainly solve the problem of non-interactive in shell script. It is very helpful for large-scale Linux operation and maintenance.

In Linux operation and development, we often need to log on to the server remotely. The login process is an interactive process, and we may need to enter information such as yes/no,password. To simulate this input, you can use an Expect script.

Basic command send: sends a string to the process to simulate user input

This command cannot automatically enter to change the line, and usually add\ r (enter).

Expect

An internal command of expect to determine whether the specified string is included in the last output, and return immediately if so, otherwise it will be returned after waiting for the timeout

You can only capture the output of processes started by spawn

Spawn: start the process and track the subsequent interaction information interact: maintain the interactive state after the execution is completed, and give control to the console Timeout: specify the timeout period, and continue to execute subsequent instructions when it expires.

Unit is: seconds

Timeout-1 for never timeout

By default, timeout is 10 seconds

Exp_continue

Allow expect to continue to execute instructions down

Send_user

Echo command, equivalent to echo

$argv parameter array

The Expect script can accept parameters passed from bash, which can be obtained using [lindex $argv n], where n starts at 0 and represents the first, second, and third, respectively. Parameters.

Expect scripts must end with interact or expect eof, and expect eof is usually enough to perform automated tasks

Expect eof is waiting for the end flag. Commands initiated by spawn end with an eof tag that expect eof is waiting for.

Expect syntax single branch syntax

Expect "password:" {send "mypassword\ r";}

Multi-branch mode syntax send command does not have the function of carriage return and line feed, and usually adds\ r or\ n

Expect "aaa" {send "AAA\ r"}

Expect "aaa" {send "AAA\ r"}

Expect "aaa" {send "AAA\ r"}

As long as any one of aaa or bbb or ccc is matched, execute the corresponding send statement and exit the expect statement

Expect {

"aaa" {send "AAA\ r"}

"bbb" {send "BBB\ r"}

"ccc" {send "CCC\ r"}

}

Exp_continue means to continue the following matching. If you match the aaa, you will continue to match the bbb downward after executing the send statement.

Expect {

"aaa" {send "AAA\ r"; exp_continue}

"bbb" {send "BBB\ r"; exp_continue}

"ccc" {send "CCC\ r"}

}

The-re parameter indicates that the match regular expression Expect execution is executed directly.

SSH login

First landing

Normal login

The connection is rejected, maybe the ssh is not open, or the port number is wrong, or the firewall is restricted.

There is no connection address.

#! / usr/bin/expect / / Expect binary file path # timeout set timeout 20 / / 20 second wait time log_file test.log / / log file log_user 1 / / log user # parameter passed in set hostname [lindex $argv 0] / / append parameter 0 Statistics variables and load the first position parameter set password [lindex $argv 1] / / append parameter 1 Statistics variable and load the second position parameter # tracking command spawn ssh root@$hostname / / tracking command # capture information and match interactive execution-free expect {/ / capture prompt message "Connection refused" exit / / exit "service not konwn" exit when you capture the rejected connection information / / exit "(yes/no)" / / capture yes or no parameter {send "yes\ r" if the service is enabled. Exp_continue} / / enter yes and continue to execute "* password" / / capture parameter {send "$password\ r"} / / enter password parameter} # Control to console interact / / Control transfer console artificial input [root@localhost ~] #. / expect.sh 192.168.109.132 baby520./spawn ssh root@192.168.109.132root@192.168.109.132's password: Last login: Thu Oct 10 16:52:48 2019 from 192.168.109.10 [root@localhost ~] # Expect execution #! / bin/bash user=$1password=$2# non-interactive command outside expect useradd $user# to start interactive expect

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