1834149 Members
2240 Online
110064 Solutions
New Discussion

Re: Expect question

 
Geoff Wild
Honored Contributor

Expect question

One of my users is using an expect script to sftp a file offsite automatically.

Unfortunately, it doesn't work.

Here's the script:

#!/usr/local/bin/expect -f
#
# This Expect script was generated by autoexpect on Wed Dec 10 08:04:14 2003
# 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 1 ;# 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 sftp -v test.user1@sshtest.somewhere.com:mydir/
match_max 100000
expect -exact "test.user1@sshtest.somewhere.com's password: "
send -- "mypass\r"
expect eof


On the screen, it just sit's there at the spawn line...

From the command line, the sftp works...

Any ideas?

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
7 REPLIES 7
Michael Tully
Honored Contributor

Re: Expect question

Hi Geoff,

Here is something we use to pass a password to expect, without having to look. It is used for a startup program. I have slightly changed it to protect what we actually use it for, but still should work.
Cheers
Michael

Anyone for a Mutiny ?
Geoff Wild
Honored Contributor

Re: Expect question

Well - chalk this up to PEBKAC - problem exists between keyboard and chair. The user was double sourcing their .kshrc file - when I removed it - script worked fine.

Rgds...Geoff

Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Ratzie
Super Advisor

Re: Expect question

Run your expect with
#!/usr/local/bin/expect -d

With this it runs in debug, this sends all output to the screen, you can see all send and how and when the expect script rejected it.
Ratzie
Super Advisor

Re: Expect question

You may also want to remove the expect -exact to expect "blah$" and anchor the end of the line.

I have spent many nights troubleshooting expect scripts.
John Meissner
Esteemed Contributor

Re: Expect question

What I would recommend doing is having your user re-create the script using autoexpect. autoexpect will allow your user to log into the session one time and autoexpect will record everything into a file which will be used for the script then. after run all you need to do is pull out a few extraneous lines. the syntax for this is
autoexpect -f

All paths lead to destiny
Paul Johnston_4
Advisor

Re: Expect question

Hello Geoff,

This posting was awhile ago. Not sure if it still stands true or there is a better method.

We also have the same SFTP script which uses Expect.

I see in the responses you resolved this yourself chalking it up to PEBKAC.

Not sure if I understand you answer about
"
The user was double sourcing their .kshrc file - when I removed it - script worked fine. "

Can you explain ?

Also , is there a reason why you chose Expect to script the SFTP , other than this is a canned SFTP script and why re-invent the wheel thinking.

Thanks

Paul



Geoff Wild
Honored Contributor

Re: Expect question

Script was written by the users - so they didn't want it changed - they were sourcing their profile from another script prior to launching this one - when it was already sourced.
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.