Hello,

  I know this is a Perl question but is not more easier to log in with
ssh public key method and execute the commands inside the foreach and
don't do all the username/password code?

  Regards,

     Alvaro

On Mon, 2010-01-04 at 03:26 +0530, perl_haxor 123 wrote:
> Hi All,
> 
>            I'm new to perl and facing a problem........ I'm using a perl
> expect script to login to multiple machines (having same username and
> password) and execute the date command, initially i tested on one machine,
> it worked fine...........but when i use a foreach loop to test the same
> thing on two machines (the code is shown below), it didn't work.......it
> just worked on the first machine (10.10.10.1) ........can someone please
> help me out with this problem?......how can i use the perl expect script to
> login to the multiple machines (assuming they have same username and
> password) and execute commands?.. Kindly help me with this problem
> 
> 
> 
> #############################################################
> #!/usr/bin/perl -w
> use strict;
> use Expect;
> 
> 
> foreach ("10.10.10.1", "10.10.10.2") {
> print "Spawning to the $_\n";
> $exp->spawn("ssh -l username $_") or warn "unable to spawn: $!";
> 
> 
> 
> #This part checks for the password prompt, if it doesn't get the password
> prompt the errors are displayed.
> unless($exp->expect(10, '-re','.*password:\s*')){
> 
> if( $exp->before()) {
> print $exp->before();
> print "\n";
> }
> else {
> 
> print "Timed out\n";
> }
> next;
> }
> 
> 
> print "Sending password to the host\n";
> 
> $exp->send("password\r");
> print "\n";
> 
> 
> #This code will handle any errors after the password is sent
> 
> unless($exp->expect(10, '-re', '.*[#$>]')) {
> 
> 
> 
> if($exp->before() =~ /(Permission.*\s*.*)/) {
> print "\n";
> print "Invalid password\n";
> }
> else {
> 
> print "timed out\n";
> 
> }
> exit;
> 
> }
> 
> 
> 
> 
> print "sending date command\n";
> 
> $exp->send("date\r");
> $exp->expect(15, '-re', '.*[#$>]' );
> $exp->soft_close();
> 
> }
> #############################################################


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to