Hi,
a timeout of 1second is very little. ssh can sometimes take longer. Another thing is that the execution of sending the password should depend on a string having matched. It shouldn't be executed no matter what.
Also: always use strict; use warnings; ...
A small working example:
use strict;
use warnings;
use Expect;
my $timeout = 5;
my $command = '/usr/bin/ssh';
my $user='johndoe';
my $host='192.168.47.11';
my $password = 'godsexpower';
my $exp = Expect->spawn($command, "$user\@$host")
or die "Cannot spawn $command: $!\n";
$exp->expect($timeout, [ qr{password:} , sub {$exp->send("$password\n"
+)} ]);
$exp->expect($timeout, [ qr{\$ } , sub {$exp->send("ls\n")} ]);
$exp->expect($timeout, [ qr{\n*?\$ } , sub {print "got the output: '".
+ $exp->before()."'\n"} ]);
$exp->soft_close();
This example expects the prompt of a host to end on '$ ' (DollarSpace), you might have a diffrent prompt(especially if the HPux box uses csh, then it's usually '> ').
Hope this helps.
Cheers Roland
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.