btobin0 has asked for the wisdom of the Perl Monks concerning the following question:

I have a Perl script that also uses an Expect script. When the script runs the file is copied but the information inside is not there. anyone hear of this before?
spawn sftp test@testserver.com expect -exact "password:" send "password" expect "sftp>" send "get /export/home/testfile/test /home/btobin0/damitsnotworking/ expect "sftp>" send "exit"
This is the perl code. works just fine for (8 .. 46){ system ("expect net"); < A bunch of Perl Code > sleep 300 }
I tried to send the file from the SFTP prompt manually and now I am now gettting the error: Couldn't open local file "/home/btobin0/damitsnotworking/" for writing: No such file or directory It does exist. How do I fix it?

Replies are listed 'Best First'.
Re: Perl and Expect Question
by shmem (Chancellor) on Mar 01, 2008 at 09:28 UTC
    Very likely the sftp prompt is the last thing printed on the screen. What does your "bunch of Perl Code" do?

    Try

    system ("expect net"); print "expect done\n";

    If you want to run expect within perl, use Expect.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Perl and Expect Question
by markhu (Initiate) on Mar 01, 2008 at 16:01 UTC
    I also encountered numerous quirks when trying to use "both." So eventually I gave up and re-wrote everything in pure perl. It turned out to be a good thing anyway since my Expect script was a "first draft" overripe for throwing away.
Re: Perl and Expect Question
by quester (Vicar) on Mar 01, 2008 at 19:58 UTC
    Unless you do something unusual in your Expect script, when it ends it will terminate its spawned telnet or ssh process. The session running SFTP will see this as a hangup (SIGHUP) and will probably exit. In general, I wouldn't expect think that anything will still be running when Expect returns. The only obvious exception is if you use nohup or screen in the session on the distant end, so it can continue running after the telnet or ssh session disconnects.

    Having also done it both ways, think shmem's suggestion to use the Perl Expect.pm module is very good advice.

    I wrote an TCL-based Expect script that was called from Perl. I used Proc::Simple to run several copies of the Expect script in parallel with the main Perl process. If you need very limited communications, you can tail a log file or create flag files when you need to check progress.

    After doing that and living with it for a few months, I wound up converting the whole thing to the Perl Expect.pm package.

    The biggest win is that Expect.pm actually works reliably, which is not true of many versions of TCL-based Expect. There is a discussion of the problem and a patched version of expect on the Rancid site. Even using Shrubbery Network's Expect and TCL, I still saw Expect processes that would hang once in a blue moon.

    It's also a lot easier to write complex logic in Perl than in TCL, IMHO. It may be that I'm just more familiar with Perl, though.

    Another win is that if your Perl script needs to ask the user for a password, it can pass it securely to a Expect.pm script as an ordinary subroutine parameter. Using TCL Expect, it is tempting to transfer the passwords using a temporary file or an environment variable. Neither of those approaches are particularly safe from prying eyes. You can have the Perl script read the Expect script, plug in the password into the Expect script as a TCL assignment statement, open a pipe to the Expect compiler, and write the Expect script. It's not pretty though, especially when you are trying to track down Expect bugs.