1 #!/usr/bin/perl -w 2 3 use warnings; 4 use strict; 5 use Expect; 6 7 8 my $filename = "/var/tmp/expect_script.log"; 9 my $header = "\r\n\r\n======= system =======\r\n"; 10 my $timeout = 60; 11 12 13 #This will open a file and push it to array 14 my @servers; 15 16 #open (my $file, '<', $ARGV[0]) or die $!; 17 18 open (my $file, '<', "/home/amagana/scripts/lists/list_b2") or die $!; 19 20 while(<$file>) { 21 push (@servers, $_); #push each line of the file to array 22 } 23 24 print "$_\n" for(@servers); 25 26 27 #This is an array 28 #my @servers = qw( 29 # 30 # 31 # 32 # 33 #); 34 35 36 for my $server (@servers) { 37 # do your thing with $server 38 39 change_password($server); 40 41 } 42 43 44 sub change_password { 45 46 my $system = shift; 47 my $ssh = Expect->new('ssh amagana@' . $system); 48 49 50 $ssh->debug(22); 51 $ssh->log_file("$filename"); 52 my $my_header = $header; 53 $my_header =~ s/system/$system/; 54 $ssh->print_log_file($my_header); 55 56 $ssh->expect ( $timeout, 57 [ qr/password:/], 58 [ qr/Are you sure you want to continue connecting \(yes\/no\)?/] 59 60 ); 61 62 if ($ssh->match() =~ m/Are you sure you want to continue connecting \(yes\/no\)?/ ) { 63 $ssh->send("yes\r"); 64 } 65 66 elsif ($ssh->match() =~ m/password:/ ) { 67 68 $ssh->send("mycurrentpassword\n"); #no problem with the special characters at this line 69 70 } 71 72 $ssh->expect(60, '$'); 73 $ssh->send("su - root\n"); 74 $ssh->expect(60, 'Password:'); 75 $ssh->send( "rootpasswordwithspecialcharacters\n" ); #but my script quits here 76 $ssh->expect(60, '#'); 77 $ssh->send("hostname\n"); 78 $ssh->expect(60, '#'); 79 $ssh->send("uptime\n"); 80 $ssh->expect(60, '#'); 81 $ssh->send("passwd amagana\n"); 82 $ssh->expect(60, 'New UNIX Password:'); #linux 83 $ssh->send( "mynewpasswordwithspecialcharacters\n" ); 84 $ssh->expect(60, 'Retype new UNIX password:'); #linux 85 $ssh->send( "mynewpasswordwithspecialcharacters\n" ); 86 $ssh->expect(60, '#'); 87 $ssh->send("exit\n"); 88 $ssh->expect(60, '$'); 89 $ssh->send("exit\n"); 90 $ssh->close(); 91 }