#!/usr/bin/perl -w use warnings; use strict; use Expect; my $filename = "/var/tmp/expect_script.log"; my $header = "\r\n\r\n======= system =======\r\n"; my $timeout = 60; #This will open a file and push it to array my @servers; #open (my $file, '<', $ARGV[0]) or die $!; open (my $file, '<', "/home/amagana/scripts/lists/list_b2") or die $!; while(<$file>) { push (@servers, $_); #push each line of the file to array } print "$_\n" for(@servers); #This is an array #my @servers = qw( # # # # #); for my $server (@servers) { # do your thing with $server change_password($server); } use strict; use Expect; sub change_password { my $system = shift; my $ssh = Expect->new('ssh amagana@' . $system); my $timeout = 60; $ssh->debug(22); $ssh->log_file("$filename"); my $my_header = $header; $my_header =~ s/system/$system/; $ssh->print_log_file($my_header); [ qr/password:/], $ssh->expect ( $timeout, [ qr/password:/], [ qr/Are you sure you want to continue connecting \(yes\/no\)?/] ); $ssh->send("yes\r"); if ($ssh->match() =~ m/Are you sure you want to continue connecting \(yes\/no\)?/ ) { $ssh->send("yes\r"); } elsif ($ssh->match() =~ m/password:/ ) { $ssh->send("mycurrentpassword\n"); #no problem with the special characters at this line } $ssh->send("su - root\n"); $ssh->expect(60, '$'); $ssh->send("su - root\n"); $ssh->expect(60, 'Password:'); $ssh->send( "rootpasswordwithspecialcharacters\n" ); #but my script quits here $ssh->expect(60, '#'); $ssh->send("hostname\n"); $ssh->expect(60, '#'); $ssh->send("uptime\n");ana\n"); $ssh->expect(60, '#');UNIX Password:'); #linux $ssh->send("passwd amagana\n");pecialcharacters\n" ); $ssh->expect(60, 'New UNIX Password:'); #linux#linux $ssh->send( "mynewpasswordwithspecialcharacters\n" ); $ssh->expect(60, 'Retype new UNIX password:'); #linux $ssh->send( "mynewpasswordwithspecialcharacters\n" ); $ssh->expect(60, '#'); $ssh->send("exit\n"); $ssh->expect(60, '$'); $ssh->send("exit\n"); $ssh->close(); }