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

What does Linux expect mean?

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

Share

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

This article will explain what Linux expect means for you in detail. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Expect can be seen everywhere

The first time I saw expect this command was the first time I participated in full online. It was a set of automatic deployment, MD5 comparison and release full online tools written by an awesome person in the company with Shell script. When there was nothing to do, I looked at some of the scripts and a lot of expect commands. I really don't understand the usage of this expect command, so I found time to summarize this article on the expect command.

Throw out a question first.

Now that there are two Linux hosts An and B, how to ssh from host A to host B, and then execute commands on host B, how to automate the whole process? You might use this method:

Ssh admin@10.220.20.15 "ls"

But this approach is clumsy, entering passwords every time, and can't execute some complex logic or commands. So how to realize the whole process automation? This is going to use the expect summarized in today's article.

What is expect?

Expect is a free programming tool for automated interactive tasks without human intervention. To put it bluntly, expect is a set of software used to achieve automatic interaction.

In practice, when we run commands, scripts or programs, these commands, scripts or programs need to enter some instructions to continue to run from the terminal, and these inputs need to be done manually. With expect, the standard input can be simulated and provided to the program according to the prompts of the program, so as to realize automatic interactive execution. This is "tweets"!

Expect Foundation

When using expect, you basically deal with the following four commands:

The send command takes a string argument and sends it to the process.

The expect command is the opposite of the send command. Expect is usually used to wait for feedback from a process. We send corresponding interactive commands based on the feedback from the process.

The spawn command is used to start a new process, and the send and expect commands after spawn interact with processes opened using spawn.

In fact, the interact command is not used very much, in general, we can complete our task well by using the spawn, send and expect commands; but in some special occasions, we still need to use the interact command, and the interact command is mainly used to exit automation and enter human interaction. For example, we use spawn, send and expect commands to complete the ftp login host and perform the task of downloading files, but we hope that after the file download is finished, we can still stay on the ftp command line so that subsequent commands can be executed manually. At this time, we can use the interact command to complete this task.

Practical code analysis

Expect is summarized on the, especially some commonly used commands are described in detail. Here are some commonly used expect scripts to specifically explain how to use expect to do some daily work.

#! / usr/tcl/bin/expectset timeout 30set host "101.200.241.109" set username "root" set password "123456" spawn ssh $username@$hostexpect "* password*" {send "$password\ r"} interact

This is a very simple expect sample code that demonstrates the basic use of expect.

#! / usr/tcl/bin/expect: use expect to interpret the script

Set timeout 30: sets the timeout in seconds. By default, it is 10 seconds.

Set host "101.200.241.109": set variables

Spawn ssh $username@$host:spawn is an expect internal command that can only be executed after entering the expect environment. If you don't install expect or execute it directly under the default SHELL, you can't find the spawn command. Its main function is to add a shell to the ssh running process to pass interactive instructions.

Expect "* password*": expect here is also an internal command of expect. This command means to determine whether the last output contains a string of "password", and if so, return it immediately; otherwise, you will return after waiting for a period of time. The waiting time here is 30 seconds set above.

Send "$password\ r": when the corresponding output is matched, the password is sent to the open ssh process to perform an interactive action

Interact: keep the interaction after the execution is complete, and hand over the control to the console. At this time, you can do it manually. Without this sentence, you will log out after the login is completed, instead of leaving it on the remote terminal.

This is the analysis of the above simple script, and in the above example, it involves a very important concept in expect-patterns-actions; that is, the meaning expressed by the above expect "* password*" {send "$password\ r"} code.

Mode-Action

Combine the code expect "* password*" {send "$password\ r"} to talk about "mode-action". To put it simply, if you match a pattern, you will perform the corresponding action; if you match the password string, you will enter the password. You may also see code like this:

Expect {"password" {send "$password\ r" exp_continue} eof {send "eof"}}

Exp_continue means circular matching, which usually exits the statement after matching, but if there is an exp_continue, you can continue to loop matching and enter multiple commands to simplify the writing.

Pass parameters

Most of the time, we need to pass parameters to the script, so now let's see how to use parameters in expect with the following code:

#! / usr/tcl/bin/expectif {$argc < 3} {puts "Usage:cmd" exit 1} set timeout-1set host [lindex $argv 0] set username [lindex $argv 1] set password [lindex $argv 2] spawn ssh $username@$hostexpect "* password*" {send "$password\ r"} interact

In expect,\ $argc represents the number of parameters, and the parameter values are stored in $argv, such as [lindex $argv 0] as the first parameter, and so on.

Summary

Being able to skillfully use Shell scripts at work can greatly improve work efficiency, if coupled with expect, then a lot of work can be carried out automatically, adding to the work. If you can Python, your vision will be wider, and then you will "dislike" expect.

About what Linux expect means to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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