Hi.
Here is an
expect script that connects and then checks for a file
t2. Perhaps this will help until someone more knowledgeable stops by:
Code:
#!/usr/bin/expect --
# @(#) e4 Demonstrate running commands on remote computer with expect.
# Identify local computer, version of expect.
puts " Local computer is [eval exec uname -n]"
puts " Version of expect is [exp_version]."
puts ""
# Read login-name, computer-name, password from file.
set file "data1"
if { ! [file exists $file] } {
puts " File $file not found -- exiting."
exit 1
}
set input [open $file "r"]
set line [gets $input]
set list [split $line]
set login [lindex $list 0]
set box [lindex $list 1]
set password [lindex $list 2]
puts " login is $login, intended remote computer is $box"
set timeout 10
send_user " spawning: ssh $login@$box\n"
spawn ssh $login@$box
expect \[pP]assword:*
send "$password\r"
# send_user "(Got string Password*, sent password $password.)\n"
expect *$box*
# send "ls\r"
send {
bash <<EOF
echo " Hi from a here document on computer $(uname -n)."
if [ ! -f t2 ]
then
echo " There is no file t2, creating it."
touch t2
else
echo " File t2 exists, using it."
fi
ls -l t2
rm t2
EOF
}
expect *$box*
send "exit\r"
expect "logout*"
Producing:
Code:
% ./e4
Local computer is leap
Version of expect is 5.42.1.
login is vanilla, intended remote computer is vm-lenny
spawning: ssh vanilla@vm-lenny
spawn ssh vanilla@vm-lenny
vanilla@vm-lenny's password:
Linux vm-lenny 2.6.26-2-686 #1 SMP Thu Mar 26 01:08:11 UTC 2009 i686
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Apr 23 09:37:28 2009 from leap
bash <<EOF
echo " Hi from a here document on computer $(uname -n)."
if [ ! -f t2 ]
then
echo " There is no file t2, creating it."
touch t2
else
echo " File t2 exists, using it."
fi
ls -l t2
rm t2
EOF
vanilla@vm-lenny:~$
vanilla@vm-lenny:~$ bash <<EOF
> echo " Hi from a here document on computer $(uname -n)."
> if [ ! -f t2 ]
> then
> echo " There is no file t2, creating it."
> touch t2
> else
> echo " File t2 exists, using it."
> fi
> ls -l t2
> rm t2
> EOF
Hi from a here document on computer vm-lenny.
There is no file t2, creating it.
-rw-r--r-- 1 vanilla vanilla 0 2009-04-23 10:13 t2
vanilla@vm-lenny:~$ exit
logout
If you are going to use expect extensively, I suggest the book below ... cheers, drl
Quote:
Title: Exploring Expect
Subtitle: A Tcl-based Toolkit for Automating Interactive Programs
Author: Don Libes
Edition: First
Date: December 1994
Publisher: O'Reilly
ISBN: 1-56592-090-2
Pages: 602
Categories: scripting, interacting, automating, system administration
Comments: 3.5 stars (25 reviews) Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more
Comments: elderly book, but still useful
|