From: Apollo J <apo...@ms...> - 2010-12-08 08:58:49
|
Hi, I manage to do in an other way using the module Net::SSH::Expect and it works.( Thanks for your reply ) foreach (keys (%sys)) { my ($out,$err,$exit)=""; my $cmd=''; if ($sys{$_} =~ /linux/i) { $cmd='service snmpd start'; } else { $cmd='/sbin/init.d/SnmpMaster start'; } print "**** $_ ****\n"; $ssh = Net::SSH::Expect -> new ( host => $injc, user => $user, #exp_debug => 2, #exp_internal => 1, raw_pty => 1 ); $ssh->run_ssh() or die "SSH process couldn't start: $!"; $ssh->send("ssh -o StrictHostKeyChecking=no $_ $cmd"); $ssh->waitfor('snmpd :[ OK ]',2,'-ex') and print $ssh->match() and next ; $ssh->waitfor('password:\s*\z',$timeout) or die "prompt 'password' not found after $timeout secondS"; # THE LOOP I WANTED foreach(@passwd) { $ssh->send($_); if (!$ssh->waitfor('password:\s*\z',2)) { last;} } print $ssh->peek() ; } > Date: Tue, 7 Dec 2010 10:30:02 -0800 > From: rlp...@di... > To: apo...@ms... > CC: exp...@li... > Subject: Re: [Expectperl-discuss] Using conditions in expect > > On Tue, Dec 07, 2010 at 01:45:28PM +0100, Ismaël BALLO wrote: > > > > 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 ..) ? > > Right near the top of the man page: > > # or multi-match on several spawned commands with callbacks, > # just like the Tcl version > $exp->expect($timeout, > [ qr/regex1/ => sub { my $exp = shift; > $exp->send("response\n"); > exp_continue; } ], > [ "regexp2" , \&callback, @cbparms ], > ); > > \&callback is a reference to a function. > > > 2/ What do I miss to make this piece of code works ? > > How is it failing? Can you turn on debug(2) and post the results? > What did the simple version that worked look like? > > Also, I'd suggest seperating out the subroutines, for readability. > > -Robin > > -- > http://singinst.org/ : Our last, best hope for a fantastic future. > Lojban (http://www.lojban.org/): The language in which "this parrot > is dead" is "ti poi spitaki cu morsi", but "this sentence is false" > is "na nei". My personal page: http://www.digitalkingdom.org/rlp/ |