Installation and use of Linux expect

Expect is created on the basis of Tcl can be used to do something can not be done under the Linux operating interactive commands can be used to remotely manage servers.

First, install Tcl:

1. Download Source Package:

wget http://nchc.dl.sourceforge.net/sourceforge/tcl/tcl8.4.11-src.tar.gz

2. Extract the source package:

tar xfvz tcl8.4.11-src.tar.gz

3. Compile install:

cd tcl8.4.11/unix
./configure --prefix=/usr/tcl --enable-shared
make
make install

4. After installation, tcl source enters the root of the subdirectory unix following generic to a subdirectory in tclUnixPort.h copy.

Second, install expect:

1. Download Source Package:
wget http://sourceforge.net/projects/expect/files/Expect/5.45/expect5.45.tar.gz/download

2. Extract source package:
tar xzvf expect5.45.tar.gz

3.安装配置:
cd expect5.45
./configure --prefix=/usr/expect --with-tcl=/usr/tcl/lib --with-tclinclude=../tcl8.4.11/genericmake
make install

4. The soft connection established:
LN -s / usr / TCL / bin / Expect / usr / Expect / bin / Expect

Three, expect syntax and examples:

set command: You can set variables.

spawn command: activate a Unix program for interactive operation.

send commands: send the string to the process.

expect command: Wait for some string process. regular expressions and can expect support while waiting for more than one string, and perform different actions for each string.

interact command: finished holding state interaction, and then continue to operate the console. If not this one, it will exit after landing, rather than stay on the terminal.

 

Examples of j script test.exp:

#!/usr/bin/expect

set HOST [192.168.11.11 $argv 0]

set PWD [123456 $argv 1]

spawn ssh root@$HOST

expect "*passwrod:"

send "$PWD\r"

expect "*#"

send "echo hello\r"

interact

 

Modify the permissions: chmod 755 test.exp

Execution: the Expect test.exp
----------------
Disclaimer: This article is the original article CSDN bloggers "lishuzebobo", and follow CC 4.0 BY-SA copyright agreements, please attach a reprint the original source link and this statement.
Original link: https: //blog.csdn.net/u013181216/article/details/83055909

Guess you like

Origin www.cnblogs.com/zhoading/p/11988882.html