包含示例的脚本教程

Expect is a scripting language used to automate interactive shells. For example if we want to automate Cisco devices over a shell by using some expect scripting. Expect scripting generally works on specified strings. Most used expect commands are like below.

Expect是一种用于自动执行交互式Shell的脚本语言。 例如,如果我们想通过使用一些期望脚本来通过外壳使Cisco设备自动化。 Expect脚本通常可以在指定的字符串上运行。 最常用的Expect命令如下所示。

  • send to send strings to the process

    send向过程发送字符串

  • expect wait for the specific string from the process

    expect等待过程中的特定字符串

  • spawn to start new command

    spawn以启动新命令

期望脚本安装(Expect Script Installation)

Expect have a shell to interpret and execute expect commands. Most Linux distributions do not installs by default this package.

Expect具有解释和执行Expect命令的外壳。 默认情况下,大多数Linux发行版都不安装此软件包。

Fedora,Cent OS,Red Hat: (Fedora, Cent OS, Red Hat:)

$ sudo yum install expect

Ubuntu,Debian,Kali,Mint: (Ubuntu, Debian, Kali, Mint:)

$ apt install expect
Expect Script Installation
Expect Script Installation
期望脚本安装

句法(Syntax)

We will use following syntax for expect command.

我们将对expect命令使用以下语法。

expect [ -dDinN ] [ -c cmds ] [ [ -[f|b] ] cmdfile ] [ args ]

用法 (Usage)

Expect generally reads scripts from a script file. Now we will create a simple script which can be used to login a Cisco device. Our script file is like below.

Expect通常会从脚本文件中读取脚本。 现在,我们将创建一个可用于登录Cisco设备的简单脚本。 我们的脚本文件如下所示。

#!/usr/bin/expect 
 
expect "poftut" 
 
send "com"
  • #!/usr/bin/expect line is used to set expect as interpreter for this script. This can be omitted if we are providing this script file directly to the expect interpreter but using this will make script file more readable.

    #!/usr/bin/expect行用于将Expect设置为此脚本的解释器。 如果我们直接将此脚本文件提供给期望的解释器,则可以省略此方法,但是使用此脚本将使脚本文件更具可读性。

  • expect "poftut" line used to express we expect script poftut to continue. If poftut text is not provided the script file will not jump to next line.

    expect "poftut"行用来表示我们希望脚本poftut继续。 如果未提供poftut文本,则脚本文件将不会跳至下一行。

  • send "com" will send to remote device or current output the com string.

    send "com"将发送到远程设备或当前输出的com字符串。

We can test our script in the current bash by providing the script file name like exp1.sc

我们可以通过提供脚本文件名(例如exp1.sc在当前bash中测试脚本。

$ expect exp1.sc
Expect Usage
Expect Usage
预期用量

超时(Timeout)

There is a timeout for performance issues. Timeout is used to set maximum time to be wait for a connection or response. If the timeout value is reach the next step will be evaluated. Timeout value can be set with set timeout command. In this example we will set time out 5 second.

性能问题超时。 超时用于设置等待连接或响应的最长时间。 如果达到超时值,则将评估下一步。 可以使用set timeout命令设置超时值。 在此示例中,我们将超时设置为5秒。

#!/usr/bin/expect 
 
set timeout 5 
 
expect "poftut" 
 
send "com"

We do not enter any value for this expect script and after 5 second next step is evaluated.

我们没有为此期望脚本输入任何值,并且在5秒后评估了下一步。

$ expect exp1.sc
Timeout
Timeout
超时

产生外部过程(Spawn External Process)

Now we may want to use external tools like ssh to connect remote hosts and run expect scripts on the remote. Now we will create a ssh connection to the remote host and enter a password .

现在,我们可能要使用ssh之类的外部工具来连接远程主机并在远程计算机上运行Expect脚本。 现在,我们将创建到远程主机的ssh连接并输入密码。

#!/usr/bin/expect 
 
set timeout 5 
 
spawn ssh localhost 
 
expect "[email protected]'s password: " 
 
send "123456" 
 
expect "[email protected]:~$" 
 
send "uname -a"
  • spawn ssh localhost will connect to the localhost with ssh protocol.

    spawn ssh localhost将通过ssh协议连接到localhost。

  • expect "[email protected]'s password: " will expect for the specified text.

    expect " [email protected] 's password: "来输入指定的文本。

  • send "123456" is used to send password for the ssh connection

    send "123456"用于发送ssh连接的密码

LEARN MORE  How To Split, Create and Close Tmux Screen In Linux?
了解更多如何在Linux中拆分,创建和关闭Tmux屏幕?

翻译自: https://www.poftut.com/expect-scripting-tutorial-examples/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值