ubuntu

Expect 自动交互实现

Environment Setup

Expect是一个用来处理交互的命令。借助Expect,我们可以将交互过程写在一个脚本上,使之自动化完成。形象的说,ssh登录,ftp登录等都符合交互的定义。因此,程序的安装,远程服务器的控制等,都可以使用expect来实现自动化

Ubuntu Install

Use below command to install this command on Ubuntu

$sudo apt install expect

$ sudo apt install expect

Usage

Command

Expect中常用命令:

  • send:用于向进程发送字符串
  • expect:从进程接收字符串
  • spawn:启动新的进程
  • interact:允许用户交互
  • autoexpect : 记录执行的过程使其自动化。

send

send命令接收一个字符串参数,并将该参数发送到进程。

expect1.1> send “hello world\n”

hello world

expect

expect命令和send命令正好相反,expect通常是用来等待一个进程的反馈。expect可以接收一个字符串参数,也可以接收正则表达式参数。和上文的send命令结合,

简单来讲,就是等到标准输入,然后执行指定命令。


spawn命令

上文的所有demo都是和标准输入输出进行交互,但是我们跟希望他可以和某一个进程进行交互。spawm命令就是用来启动新的进程的。spawn后的send和expect命令都是和spawn打开的进程进行交互的。结合上文的send和expect命令我们可以看一下更复杂的程序段了。

set timeout -1
spawn ftp ftp.test.com      //打开新的进程,该进程用户连接远程ftp服务器
expect "Name"               //进程返回Name时
send "user\r"               //向进程输入anonymous\r
expect "Password:"          //进程返回Password:时
send "123456\r"             //向进程输入don@libes.com\r
expect "ftp> "              //进程返回ftp>时
send "binary\r"             //向进程输入binary\r
expect "ftp> "              //进程返回ftp>时
send "get test.tar.gz\r"    //向进程输入get test.tar.gz\r

这段代码的作用是登录到ftp服务器ftp ftp.uu.net上,并以二进制的方式下载服务器上的文件test.tar.gz。程序中有详细的注释。


interact

到现在为止,我们已经可以结合spawn、expect、send自动化的完成很多任务了。但是,如何让人在适当的时候干预这个过程了。比如下载完ftp文件时,仍然可以停留在ftp命令行状态,以便手动的执行后续命令。interact可以达到这些目的。下面的demo在自动登录ftp后,允许用户交互。

spawn ftp ftp.test.com
expect "Name"
send "user\r"
expect "Password:"
send "123456\r"
interact

语法: 模式-动作

expect最常用的语法是来自tcl语言的模式-动作。这种语法极其灵活,下面我们就各种语法分别说明。

单一分支模式语法:

expect "hi" {send "You said hi"}

匹配到hi后,会输出”you said hi”

多分支模式语法:

expect "hi" { send "You said hi\n" } \
"hello" { send "Hello yourself\n" } \
"bye" { send "That was unexpected\n" }

匹配到hi,hello,bye任意一个字符串时,执行相应的输出。等同于如下写法:

expect {
"hi" { send "You said hi\n"}
"hello" { send "Hello yourself\n"}
"bye" { send "That was unexpected\n"}
}

Example

SDK Install

#!/usr/bin/expect -f
#############################################################################################
# This script is using for installing the keywriter                                         #    
# @Author : Fredy Zhang                                                                     #                                                       #               
# @date   :2022-02-18 
# @update : fredy  V1                                                                       # 
##############################################################################################

# ---Args
# 1 install package name
set Pkg_PATH      [lindexargv 0]
# 2 install package 
set Install_PATH  [lindexargv 1]

set Pkg_Name       [exec echo Install_PATH | cut -d / -f 6 ]


set timeout 600
# set time [exec date +%Y%m%d]
# set PSDKLA [exec echo "SJ_PATH_PSDKRA"]
# set run_command env(HOME)
set UserNameenv(USER)
puts  "--- Pkg_PATH : Pkg_PATH" 
puts  "--- Install_PATH      :Install_PATH" 
puts  "--- Pkg_Name          : Pkg_Name" 
puts  "--- UserName          :UserName" 
spawn Pkg_PATH

set count 100;
while {count > 0 } {   
    expect {
        "Please choose an option" { send "13\r"; exp_continue }
        "Enter] to continue:" { send "\r"; exp_continue }
        "Do you accept this license?" { send "y\r"; exp_continue }
        "/home/UserName/Pkg_Name" { send "Install_PATH\r"; exp_continue }
        "Do you want to continue?" { send "Y\r"; exp_continue }
        "View Readme file?" { send "Y\r"; exp_continue }
       eof { }
       timeout { puts "timeout waiting for response" ; close ; exit }
   }
   set times [ exprcount-1];
}

Remote SSH

#!/usr/bin/expect -f
#############################################################################################
# This script is using for remote ssh                                        #    
# @Author : Fredy Zhang                                                                     #                                                               #               
# @date   :2022-03-19                                                                      #
# @update : fredy  V1                                                                       # 
##############################################################################################

# ---Args
# 1 install package name
set SERVER_IP      [lindexargv 0]
# 2 install package 
set REMOTE_IP    [lindexargv 1]

set timeout 600
set time [exec date +%Y_%m_%d]
# set PSDKLA [exec echo "SJ_PATH_PSDKRA"]
# set run_commandenv(HOME)
set UserName env(USER)
puts  "--- SERVER_IP :SERVER_IP" 
puts  "--- REMOTE_IP : REMOTE_IP" 
puts  "--- time                :time" 
puts  "--- UserName  --        : UserName" 

spawn ssh fredy@SERVER_IP

expect "password:"
send "***\r";

expect "Last login:"
send "scp /home/UserName/atlassian/share/backups/backup-time.zip fredy@REMOTE_IP:/home/fredy/backup \r";

expect "fredy@REMOTE_IP's password:"
send "*****\r";

expect "100%"
send "exit\r "

expect eof

Reference

  1. https://www.geeksforgeeks.org/expect-command-in-linux-with-examples/
  2. https://www.thegeekstuff.com/2011/01/expect-expressions-loops-conditions/
  3. https://www.cnblogs.com/dion-90/articles/8570601.html
  4. https://linux.die.net/man/1/expect

发表评论

您的电子邮箱地址不会被公开。