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

I am running a Linux box attempting to utilize Expect in my Perl script. I have been attempting to get it to run
several commands to no avail. I've read the documentation and I have no idea what the issue is. Anyway
here's the beginning of my code and the failure.
use Expect; $Expect::Debug = 1; my $string= '/usr/bin/ssh x.x.x.x' my $exp=Expect->spawn($string) || die "error $!\n"

I have tried many other commands than ssh with the same error. All the dependencies are also installed.
Below is the output I receive from the debug. If I don't run the debug I just get my prompt back. Any help greatly appreciated.
spawned '/usr/bin/ssh x.x.x.x' spawn id(3) Pid: 3399 Tty: /dev/pts/1 Expect::spawn('Expect', '/usr/bin/ssh x.x.x.x') called at test.pl line + 4 Closing spawn id(3). Expect::hard_close('Expect=GLOB(0x82ba93c)') called at /usr/lib/perl5/site_perl/5.8.4/Expect.pm line 1575 Expect::DESTROY('Expect=GLOB(0x82ba93c)') called at test.pl line 0 eval {...} called at test.pl line 0 spawn id(3) closed. Pid 3399 of spawn id(3) terminated, Status: 0x01

Replies are listed 'Best First'.
Re: Expect Module problem
by Paladin (Vicar) on Jul 13, 2004 at 21:48 UTC
    Well, you aren't doing anything after spawning the process, so the script is ending and killing the process. Try the following:
    use Expect; $Expect::Debug = 1; my $string= '/usr/bin/ssh 127.0.0.1'; my $exp=Expect->spawn($string) || die "error $!\n"; $exp->expect(2, "password: "); $exp->send("XXXXXXXX\n"); $exp->expect(2,'$'); $exp->send("ls\n"); $exp->expect(2,'$');
    This works fine on my system.

    Update:

    If your goal is to interact with the ssh session after scripting the login, try adding:

    $exp->interact();
    after you have finished logging in.
Re: Expect Module problem
by pbeckingham (Parson) on Jul 14, 2004 at 00:15 UTC

    I suggest you use the following for your $string:

    username@x.x.x.x
    Because otherwise the remote user is expected to match the local user, which is not always the case, and certainly not something that you should rely upon.