linebacker has asked for the wisdom of the Perl Monks concerning the following question:
1 #!/usr/bin/perl -w 2 use strict; 3 use Expect; 4 5 my $timeout = '5'; 6 my $username = 'luser'; 7 my $newpass = '0ldp@sswd!'; 8 my $oldpass = 'n4p@sswd!'; 9 10 open(HOSTS,"hosts.txt") 11 or die "Error while opening file:$!\n"; 12 chomp( my @hosts = <HOSTS> ); 13 close(HOSTS) 14 or die "Error while closing file:$!\n"; 15 foreach my $newhost (@hosts) { 16 my $exp = Expect->spawn("ssh -l $username $newhost") 17 or die "Cannot spawn ssh: $!\n";; 18 my $spawn_ok; 19 # $exp->exp_internal(1); 20 $exp->expect($timeout, 21 [ 22 'password: $', 23 sub { 24 my $fh = shift; 25 print $fh "$oldpass\n"; 26 exp_continue; 27 } 28 ], 29 [ 30 "$username\]", 31 sub { 32 my $fh = shift; 33 print $fh "passwd\n"; 34 exp_continue; 35 } 36 ], 37 [ 38 'password:', 39 sub { 40 my $fh = shift; 41 print $fh "$oldpass\n"; 42 $timeout, 43 exp_continue; 44 } 45 ], 46 [ 47 'password:', 48 sub { 49 my $fh = shift; 50 print $fh "$newpass\n"; 51 exp_continue; 52 } 53 ], 54 [ 55 'password:', 56 sub { 57 my $fh = shift; 58 print $fh "$newpass\n"; 59 exp_continue; 60 } 61 ], 62 [ 63 eof => 64 sub { 65 if ($spawn_ok) { 66 die "ERROR: premature EOF in login.\n"; 67 } else { 68 die "ERROR: could not spawn ssh.\n"; 69 } 70 } 71 ], 72 [ 73 timeout => 74 sub { 75 die "Timeout.\n"; 76 } 77 ], 78 '-re', qr'[#>:] $', #' wait for shell prompt, t +hen exit expect 79 ); 80 }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Changing passwords using Expect.pm through ssh on a large number (75) of systems
by ferrency (Deacon) on Jul 11, 2002 at 18:55 UTC | |
|
Re: Changing passwords using Expect.pm through ssh on a large number (75) of systems
by sschneid (Deacon) on Jul 11, 2002 at 19:24 UTC | |
by linebacker (Scribe) on Jul 11, 2002 at 22:25 UTC | |
by sschneid (Deacon) on Jul 11, 2002 at 22:55 UTC | |
by linebacker (Scribe) on Jul 12, 2002 at 02:36 UTC |