From: Ismaël B. <apo...@ms...> - 2010-12-07 12:45:34
|
Hi, I'm new to Expect.pm, I've written a piece of code : I started with one that works but I want to add some complexity I want to use a loop in case the first password is not good : I'm connecting from a first machine (pivot) then ssh a second machine to do $cmd on a third machine Questions : 1/ Is it possible to use named sub inside expects ( I didn't see examples ..) ? 2/ What do I miss to make this piece of code works ? Here it is : // This login works ( I no need password) $ssh = Net::SSH::Perl -> new ( host => $host, user => $user, debug => 0, protocol => '2', port => 22, privileged => 0 ); $ssh->login($user); my $exp=new Expect; $exp->raw_pty(1); my $res="ssh $_ $cmd"; $exp->spawn($res) or print UNREACH "Cannot spawn $res : $! \n"; $exp->expect($timeout, [ qr/continue connecting .yes.no./ => sub _yes { $exp = shift; $exp->send("yes\n"); exp_continue; } ], [ qr/password:/ => sub _passwd{ $exp = shift; #$exp->send("master\n"); ## Comment : --> This is where I want to use other passwords until it works. I know I've got 3 max attempts but it suffices me foreach $passwd (@passwd) { print($passwd); $exp->send("$passwd\n"); $exp->expect($timeout, [ qr/password:/ => _password(); ] ) } exp_continue; } ], [ timeout => sub { print UNREACH "$host : Timeout \n"; } ] ); $exp->soft_close(); |