Following the example on the Expect.pm POD, I thought I was on the right track. However, when this script gets to line 46, the command passwd seems to be printed several times over and over and over again on the target machine. Ideas?
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 }

In reply to Changing passwords using Expect.pm through ssh on a large number (75) of systems by linebacker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.