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

Hi -

I have a program which uses Expect.pm to telnet to a host and I have to convert it to use ssh instead. My problem is that ssh seems to use the screen and keyboard when prompting for and accepting passwords, so Expect's use of stdin and stdout doesn't work. SSH sends back a "Permission denied, please try again." message three times before giving up, but Expect never sees the password prompt.

I have to log in with a username and password and I can't use an empty passphrase instead

I also tried Net::SSH::Perl which managed to log on OK, but because the remote host is running ssh v1 I can't run multiple commands on one connect (another requirement)

Has anyone else been able to do this?

  • Comment on Using Expect.pm and username/password to ssh to a host

Replies are listed 'Best First'.
Re: Using Expect.pm and username/password to ssh to a host
by Juerd (Abbot) on May 10, 2004 at 14:07 UTC

    I can't run multiple commands on one connect (another requirement)

    Then use a single sh to run multiple.

    $ssh->cmd(q[sh -c 'first; second;'])

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      Hi Juerd -

      Its not unix at the other end (its Cisco IOS). Also, I have to interact with the commands I'm executing so I really need to be able to pattern match against the individual responses

      Thanks anyway

Re: Using Expect.pm and username/password to ssh to a host
by idsfa (Vicar) on May 10, 2004 at 15:13 UTC

    You say that empty pass phrase SSH identity keys are not allowed, but if you're going to hardcode in passwords anyway, perhaps you could simply automate loading the ssh key and then you could avoid having to Expect the ssh login.

    # Run your perl script inside of an ssh-agent(1) context $ENV{SSH_ASKPASS} = "/path/to/script/that/echos/password"; system("ssh-add id_filename </dev/null"); # Continue with your process

    Otherwise, assuming that you read the docs and are dead set on doing this anyway, I think you're looking for IPC::Open3.


    If anyone needs me I'll be in the Angry Dome.
      Unless I'm badly confused, I don't think IPC::Open3 will work. I assume Expect would try every method available to ipc::open3, plus a few more, and he's already mentioned that expect doesn't work.
Re: Using Expect.pm and username/password to ssh to a host
by TilRMan (Friar) on May 11, 2004 at 06:18 UTC

    Try updating your Expect module. I was able to reproduce your problem with the following code using Expect.pm 1.11, the default on Debian Woody. The problem went away with 1.15.

    #!/usr/bin/perl -w use strict; use Expect; # version 1.11! my $ssh = Expect->new('ssh tilrman@localhost'); $ssh->debug(1); $ssh->expect(60, q{tilrman@localhost's password:}); $ssh->send("********\n"); $ssh->expect(60, '$'); $ssh->send("exit\n"); $ssh->close();

    Strangely enough, if you add a single semicolon -- apparently causing Perl to hand off to the shell -- 1.11 works too:

    my $ssh = Expect->new('ssh tilrman@localhost;');