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

Detailed explanation of Expect payment exemption of shell

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

Share

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

Expect interaction-free programming in SHell (1) Overview of Expect

(1) 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.

(2) 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.

(2) Expect installation

Execute installation command

Yum install expect-y

one

(3) order 1

Send: sends a string to the process to simulate the user's input

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

Ecpect

(1) 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.

(2) only the output of processes started by spawn can be captured.

Spawn: start the process and track subsequent interactions

Ingteract: maintain the interactive state after the execution is completed, and hand over the control to the console

Commonly used formats:

Spawn command

Spawn passwd zhangsan

Expect "prompt message"

Expect "enter password"

Send replaces the manually entered string\ r (enter)

Send "abc123\ r"

123456

(4) order 2

Timeout: specify the timeout period. If it expires, it will continue to execute subsequent instructions.

Unit is: seconds

Timeout-1 will never time out.

By default, timeout is 10 seconds

Exp_coutinue

Allow expect to continue to execute instructions down

Send_user

Echo command, equivalent to echo

(5) order 3

$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.

(6) Grammar and usage of Expect single branch syntax expect "password:" {send "mypassword\ r";} multi-branch pattern syntax

The Send command does not have the function of enter and line feed. Generally,\ r or\ n should be added.

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

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";}} 12345

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

Expect {"aaa" {send "AAA"; exp_continue} "bbb" {send "BBB"; exp_continue} "ccc" {send "CCC" }} 12345Expect execution mode (1) execute [root@localhost ~] # vim de.shroud directly. / / Expect start flag # timeout set timeout 60 log_file test.log log_user "parameter is passed in set hostname [lindex $argv 0] set passwd [lindex $argv 1] # tracking command spawn ssh root@$hostname # captures information and matches Interactive execution-free expect {"(yes/no)" {send "yes\ r" Exp_continue} "* password" {send "$passwd\ r"} # Control is handed over to the console to execute interact [root@localhost ~] # chmod + x de.sh [root@localhost ~] #. / de.sh 192.168.217.132 12345612345678910111213141516171819202122 embed the execution [root@localhost ~] # vim sa.shendbinandBash# location variable hostname=$1password=$2/usr/bin/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