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

Expect interaction-free programming in shell

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

Share

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

Interaction-free Expect of shell programming part I. preface

The core significance of ​ shell script is to simplify or even omit the avoidable manual operations based on shell commands, and gradually realize the whole process of automatic operation through a variety of control flow structures and regular expressions. From this, we can see that shell does not have the idea of object-oriented, similar to C language, after all, C language is the core language of the operating system or kernel.

​ therefore, there is no good or bad language, but everyone uses different habits and ways. In other words, it is not the language that is difficult or easy, but the thought and sudden inspiration.

II. Overview of Expect and installation 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 remotely log on to the server to operate, the login process is an interactive process, we need to enter yes/no password and other information. To simulate this input, you can use an Expect script.

Installation of Expect: yum install-y expect III. Basic commands

Send: sends a string to the process to simulate the user's input, but does not support line breaks. Generally, you need to add\ r

Expect: internal command

​ determines whether the specified string is included in the last output, returns if so, or waits for the timeout to return.

​ can only capture the output of processes started by spawn.

Spawn: start the process and track subsequent interactions.

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

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

The unit is stimeout-1 and never times out. By default, timeout is 10s.

Exp_continue-- allows expect to continue to execute instructions downward (critical, multiple interactions)

Send_user-- echo command, which is equivalent to echo

IV. Expect grammar

Expect [options] [- c cmds] [[- [f | b]] cmdfile] [args]

Option

-c: execute expect scripts from the command line, default expect is executed interactively

Example: expect-c 'expect "\ n" {send "pressed enter\ n"}

-d: you can output debugging information

Example: expect-d ssh.exp

The most commonly used syntax of expect (tcl language: pattern-Action)

Single branch pattern syntax:

When expect "hi" {send "You said hi\ n"} matches to hi, it outputs "you said hi" and wraps lines, or you can use\ r

Multi-branch pattern syntax:

Expect "hi" {send "You said hi\ n"}\ "hehe" {send "Hehe yourself\ n"}\ "bye" {send "Good bye\ n"}

When any string of hi,hello,bye is matched, the corresponding output is executed. The equivalent is as follows:

Expect {"hi" {send "You said hi\ n"} "hehe" {send "Hehe yourself\ n"} "bye" {send "Good bye\ n"}}

5. Example: 1) ssh interaction-free remote login [root@lokott ~] # yum install-y expect loaded plug-in: fastestmirror, langpacksLoading mirror speeds from cached hostfile...// omitted part [root@lokott shell] # cat a.sh # use which expect to view its location #! / usr/bin/expect# set timeout set timeout 20 log_file test.log log_user "variable definition set hostname [lindex $argv 0] set passwd [lindex $argv 1] # startup process Spawn Monitoring spawn ssh root@$hostname# matching condition expect {"(yes/no)" # exp_continue means to continue to match {send "yes\ r" downwards Exp_continue} "* password" {send "$passwd\ r"} # transfer permissions to console interact [root@lokott shell] #. / a.sh 192.168.68.129 123456 / / logging in to spawn ssh root@192.168.68.129The authenticity of host '192.168.68.129 (192.168.68.129)' can't be established.ECDSA key fingerprint is SHA256:k/6W9M/dgxVrbMgSx9nIFPGfVgUfLMoIb27ys9ZF+LM.ECDSA key fingerprint is MD5 for the first time 26:dd:06:b3:32:bd:d6:a3:2f:7c:66:7d:b9:c0:4b:c4.Are you sure you want to continue connecting (yes/no)? YesWarning: Permanently added '192.168.68.129' (ECDSA) to the list of known hosts.root@192.168.68.129's password: Wed Dec 4 10:24:33 2019 from 192.168.68.1 [root@localhost ~] # exit Logout Connection to 192.168.68.129 closed. [root@lokott shell] #. / a.sh 192.168.68.129 123456 / / the key required for the second login has been protected. Deposit spawn ssh root@192.168.68.129root@192.168.68.129's password: Last login: Wed Dec 4 10:29:42 2019 from 192.168.68.130 [root@localhost ~] # exit log out Connection to 192.168.68.129 closed.

The above demonstrates the operation process of directly using expect script to implement ssh remote login, while expect script can also be embedded in a normal script file. The following is a specific demonstration:

[root@lokott shell] # cat b.sh # embed expect execution to implement interactive login-free login #! / bin/bash# defines ordinary variables, and hostname=$1passwd=$2# embedding and writing expect script content is always valid in this shell script. Implementation of interaction-free specific content / 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