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

How to use the expect command in shell script

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

Share

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

This article mainly explains "how to use the expect command in the shell script". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the expect command in the shell script.

Expect usage

Expect is a good command to complete a series of automated command operations. I have a deep experience of using it every time it is so troublesome to enter a password for ssh login. Most fixed command execution can also be operated and executed through expect, which is very convenient.

For the following example, we use expect to complete a series of operations, including reading real-time logs

#! / usr/bin/expectspawn ssh troot@127.0.0.1set timeout 30expect "password" send "123456" send "\ n" set timeout 5expect ": ~ >" send "sudo su -" send "\ n" expect "~ #" send "tail-200f / var/log/nginx/access.log" send "\ n" interact

Spawn is an internal expect command that can only be executed after entering the expect environment, so first make sure that the environment has expect installed.

Expect, my understanding is a bit similar to if's judgment.

Send is the key to be entered when performing a certain step

Interact guarantees that you will not log in. If you do not, you will exit immediately after execution.

For possible delays in execution, I use timeout to delay execution of commands

Use bash expect with #! / bin/bash/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

Internet Technology

Wechat

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

12
Report