[Level 1] Install autoexpect

$ autoexpect -f ./auto.exp
autoexpect started, file is ./auto.exp
$ ssh stanley@localhost
The authenticity of host 'localhost (127.0.0.1)' can't be established.
RSA key fingerprint is 36:12:a4:49:26:fe:18:0d:77:bf:c7:e8:36:86:b7:f2.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.
stanley@localhost's password: 
Welcome to Ubuntu 11.04 (GNU/Linux 2.6.38-10-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

*** /dev/sda5 will be checked for errors at next reboot ***

Last login: Thu Jul 28 12:17:50 2011 from stanley-ubuntu
$ ls
apache-cassandra-0.8.1-SNAPSHOT.jar  Public
auto.exp                             Rock
Desktop                              script.exp
Documents                            scripts
Downloads                            scripts_20110302.tgz
eclipse                              Self_assesment_201103.pdf
English_Survey.pdf                   SUR-1-Vesu_Piplod_NewCityLigh_1.png
examples.desktop                     Templates
minicom.log                          Videos
Music                                vmEsxi4.1_customize.docx
Perforce                             workspace
Pictures
$ pwd
/home/stanley
$ exit
logout
Connection to localhost closed.
$ exit
exit
autoexpect done, file is ./auto.exp
$ ls -al ./auto.exp
-rwxr-xr-x 1 stanley stanley 4004 2011-08-31 09:44 ./auto.exp
$ cat ./auto.exp
#!/usr/bin/expect -f
#
# This Expect script was generated by autoexpect on Wed Aug 31 09:44:02 2011
# 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;$\$ "
send -- "ssh stanely@"
expect -exact [K"
send -- ""
expect -exact [K"
send -- ""
expect -exact [K"
send -- ""
expect -exact [K"
send -- "ley@localhost\r"
expect -exact "ley@localhost\r
The authenticity of host 'localhost (127.0.0.1)' can't be established.\r
RSA key fingerprint is 36:12:a4:49:26:fe:18:0d:77:bf:c7:e8:36:86:b7:f2.\r
Are you sure you want to continue connecting (yes/no)? "
send -- "yes\r"
expect -exact "yes\r
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.\r\r
stanley@localhost's password: "
send -- "MyPassword\r"
expect -exact "\r
Welcome to Ubuntu 11.04 (GNU/Linux 2.6.38-10-generic x86_64)\r
\r
 * Documentation:  https://help.ubuntu.com/\r
\r
*** /dev/sda5 will be checked for errors at next reboot ***\r
\r
Last login: Thu Jul 28 12:17:50 2011 from stanley-ubuntu\r\r
]0;$\$ "
send -- "ls\r"
expect -exact "ls\r
[0m[01;31mapache-cassandra-0.8.1-SNAPSHOT.jar[0m  [01;34mPublic[0m\r
[01;32mauto.exp[0m                             [01;34mRock[0m\r
[01;34mDesktop[0m                              [01;32mscript.exp[0m\r
[01;34mDocuments[0m                            [01;34mscripts[0m\r
[01;34mDownloads[0m                            [01;31mscripts_20110302.tgz[0m\r
[01;34meclipse[0m                              Self_assesment_201103.pdf\r
English_Survey.pdf                   [01;35mSUR-1-Vesu_Piplod_NewCityLigh_1.png[0m\r
examples.desktop                     [01;34mTemplates[0m\r
minicom.log                          [01;34mVideos[0m\r
[01;34mMusic[0m                                vmEsxi4.1_customize.docx\r
[01;34mPerforce[0m                             [01;34mworkspace[0m\r
[01;34mPictures[0m\r
]0;$\$ "
send -- "pwd\r"
expect -exact "pwd\r
/home/stanley\r
]0;$\$ "
send -- "exit\r"
expect -exact "exit\r
logout\r
Connection to localhost closed.\r\r
]0;$\$ "
send -- "exit\r"
expect eof
$
Wish this helps. regards, Stanley Huang

Comments

Popular posts from this blog

[Level 2] Shell Interactive With Zenity Episode 5 -- list

[Level 2] iif in Python