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

Monks,

I have a relatively large script using Expect.pm to automate nightly processing and backups. Part of the script spawns the backup shell script as follows:
my $exp = new Expect; $exp = Expect->spawn('sudo sh /usr/local/scripts/pre_backup.sh') or die "Cannot spawn pre-backup: $!\n\n"; if ($exp->expect(undef, 'Password')) { $exp->send("$password\cM"); }
This works fine, it spits the password out to sudo, and sudo accepts it. The problem is, I then need to interact with the backup script (pre_backup.sh):
if ($exp->expect(undef, 'any key to continue')) { $exp->send("\cM"); }
...but Expect never sees the 'Press any key to continue' prompt from the pre_backup script, it is still looking for text from the sudo pty, not the second pty which sudo itself spawns.

My question is this: is there a way to use the same Expect object to interact with two different ptys, such as in this case? Is there a work-around that anyone can think of?

Thanks for any help,
scott.

Replies are listed 'Best First'.
Re: Expect.pm and sudo.
by robartes (Priest) on Oct 16, 2002 at 20:05 UTC
    You could play dirty and supply the input to your pre_backup script from its STDIN directly in the sudo command line:
    sudo sh /usr/local/scripts/pre_backup.sh <`echo "X"`
    Those are backticks, BTW.

    OK, this is not exactly what you asked, but in the age old spirit of TIMTOWTDI and getting around annoying limitations that is integral to Perl, it gets the job done (I hope).

    Note that, if your script expects input more than once, you'll have to modify the echo to take this into account.

    Caveat: I'm not sure how expect spawns its command lines or how sudo spawns its commands, but if a shell is involved anywhere in there, you'll have to get creative with those backticks and quotes to avoid said shell stomping all over it.

    CU
    Robartes-

    Update: Added caveat.

      Thanks very much for the help, robartes (++). Here's what I came up with, in case anyone should read this thread and want to know the (quick & dirty, at least) solution:

      sudo sh -c 'printf "\n" | sh /usr/local/scripts/pre_backup.sh'

      I'm still interested if there's an actual way to do it with the Expect.pm module (making this a bit more perl-relevant), so if anyone is clued, a heads-up would be fantastic.

      Thanks again,
      scott.
        printf "\n"? That's the same as a simple echo, no?

        Makeshifts last the longest.