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 command for shell learning

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

Share

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

This article mainly consists of two parts: an example of the use of the expect command and an explanation of the example.

1. Example of using expect command

IP=192.168.1.18 PASS=passwd expect-c "settimeout 60; spawn ssh root@192.168.0.1 expect {\" * yes/no*\ "{send\" yes\ r\ "; exp_continue}\" * password*\ "{send\" $PASS\ r\ " }} expect eof "

two。 For an example, explain the use of the expect command

The command after the (1)-c flag is executed before any script and should be referenced to prevent it from being destroyed by shell.

(2) set timeout 60: sets the timeout time in seconds, and timeout-1 never times out.

(3) spawn is the initial command of expect, used to start a process, and then all expect operations are carried out in this process, if there is no spawn statement, the entire expect can not continue, of course, if you really do not want the spawn process, it does not matter, although there is no way to implement alone, but this script can interact with any process that calls it.

(4) expect is used to wait for a matching output, and once a match is made, subsequent actions are performed. In this example, the expect is attached to the spawn command, and when the command after the spawn is executed, the expect command matches the keyword (yes/no) after the command is executed, and if so, the action in the following {} is executed.

(5) the matching of expect can be regarded as a loop, and the statement is usually exited after the matching, but if there is an exp_continue, the matching can be repeated continuously. In this example, when you encounter "yes/no", send out "yes", then continue with expect, when prompted for a password, send out the password, and then exit.

(6) eof must be matched. After the end of the spawn process, eof; will be sent to expect. If it does not match, sometimes it can run, such as how many seconds after sleep to spawn the next command, but do not rely on this behavior, it is possible today, tomorrow can not be used. However, if the process that does not require spawn ends, you cannot expect eof, as in the following example:

#! / usr/bin/expect set timeout 30 spawn ssh 10.192.224.224 expect "password:" send "mypassword\ n" expect "* $" send "mkdirtmpdir\ n" expect "* $

Ssh doesn't actually end itself when you do it manually unless you exit;, so you can only expect bash the prompt, of course, the machine name, etc., so you can create a directory remotely.

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

Network Security

Wechat

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

12
Report