gwimong's blog Software Engineer

Expect Tools 활용 - expect_autoexpect

2020-03-17

소개

: generate an Expect script from watching a session 자동으로 Expect script를 생성합니다.[원문]

사용법

  • 아무런 매개 변수가 없으면 쉘을 기반으로 상호작용합니다.
    #expect_autoexpect
    

    autoexpect 실행 후 해당 쉘 종료 시 expect script가 생성됩니다.

  • 매개변수로 상호작용 할 프로그램 이름과 인자를 전달합니다.
    #expect_autoexpect [Target Program]
    

    autoexpect 실행 후 해당 프로그램 종료 시 expect script가 생성됩니다.

예시

실행 예제

autoexpect_example

생성 파일

#!/usr/bin/expect -f
#
# This Expect script was generated by autoexpect on Tue Mar 17 11:29:00 2020
# Expect and autoexpect were both written by Don Libes, NIST.
#
# Note that autoexpect does not guarantee a working script.  It
# necessarily has to guess about certain things.  Two reasons a script
# might fail are:
#
# 1) timing - A surprising number of programs (rn, ksh, zsh, telnet,
# etc.) and devices discard or ignore keystrokes that arrive "too
# quickly" after prompts.  If you find your new script hanging up at
# one spot, try adding a short sleep just before the previous send.
# Setting "force_conservative" to 1 (see below) makes Expect do this
# automatically - pausing briefly before sending each character.  This
# pacifies every program I know of.  The -c flag makes the script do
# this in the first place.  The -C flag allows you to define a
# character to toggle this mode off and on.

set force_conservative 0  ;# set to 1 to force conservative mode even if
			  ;# script wasn't run conservatively originally
if {$force_conservative} {
	set send_slow {1 .1}
	proc send {ignore arg} {
		sleep .1
		exp_send -s -- $arg
	}
}

#
# 2) differing output - Some programs produce different output each time
# they run.  The "date" command is an obvious example.  Another is
# ftp, if it produces throughput statistics at the end of a file
# transfer.  If this causes a problem, delete these patterns or replace
# them with wildcards.  An alternative is to use the -p flag (for
# "prompt") which makes Expect only look for the last line of output
# (i.e., the prompt).  The -P flag allows you to define a character to
# toggle this mode off and on.
#
# Read the man page for more info.
#
# -Don


set timeout -1
spawn $env(SHELL)
match_max 100000
expect -exact "\]0;gmpark@DESKTOP-IMN1RJ9: ~\[01;32mgmpark@DESKTOP-IMN1RJ9\[00m:\[01;34m~\[00m\$ "
send -- "ls -al\r"
expect -exact "ls -al\r
total 100\r
drwxr-xr-x 1 gmpark gmpark   512 Mar 17 11:29 \[0m\[01;34m.\[0m\r
drwxr-xr-x 1 root   root     512 Sep 26 14:16 \[01;34m..\[0m\r
-rw-r--r-- 1 gmpark gmpark 15654 Mar 16 20:12 .bash_history\r
-rw-r--r-- 1 gmpark gmpark   220 Sep 26 14:16 .bash_logout\r
-rw-r--r-- 1 gmpark gmpark  3772 Nov 15 14:39 .bashrc\r
drwxr-xr-x 1 gmpark gmpark   512 Mar 16 16:25 \[01;34m.cache\[0m\r
drwxr-xr-x 1 gmpark gmpark   512 Mar 16 15:22 \[01;34m.cpan\[0m\r
drwxr-xr-x 1 gmpark gmpark   512 Nov  5 18:41 \[01;34m.gem\[0m\r
-rw-r--r-- 1 gmpark gmpark   129 Mar 16 16:25 .gitconfig\r
drwxr-xr-x 1 gmpark gmpark   512 Nov  5 19:06 \[01;34m.local\[0m\r
-rw-r--r-- 1 gmpark gmpark   807 Sep 26 14:16 .profile\r
drwxr-xr-x 1 gmpark gmpark   512 Oct 14 17:14 \[01;34m.ssh\[0m\r
-rw-r--r-- 1 gmpark gmpark     0 Nov  5 18:08 .sudo_as_admin_successful\r
drwxr-xr-x 1 gmpark gmpark   512 Nov 13 18:29 \[01;34m.vim\[0m\r
-rw-r--r-- 1 gmpark gmpark 34711 Mar 17 11:09 .viminfo\r
-rw-r--r-- 1 gmpark gmpark  9990 Nov 15 17:54 2019-11-14-type-of-certification.md\r
drwxr-xr-x 1 gmpark gmpark   512 Mar 16 15:04 \[01;34mexampleCode\[0m\r
drwxr-xr-x 1 gmpark gmpark   512 Mar 17 10:24 \[01;34mgwimong.github.io\[0m\r
-rwxr-xr-x 1 gmpark gmpark     0 Mar 17 11:29 \[01;32mscript.exp\[0m\r
\]0;gmpark@DESKTOP-IMN1RJ9: ~\[01;32mgmpark@DESKTOP-IMN1RJ9\[00m:\[01;34m~\[00m\$ "
send -- ""
expect eof

Comments

Content