in reply to Re: How do I get different files into an array
in thread How do I get different files into an array
Hello james28909,
Thank you for your suggestion I put it in my script but I am missing something.
"my" variable $file masks earlier declaration in same scope at test-a- +filehandle.pl line 17. syntax error at test-a-filehandle.pl line 19, near "<$file> {" Execution of test-a-filehandle.pl aborted due to compilation errors.
This is how it is placed in my script.
#!/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; my $file = "/home/amagana/scripts/lists/list_xx"; my @servers; open (my $file, '<', $ARGV[0]) or die! $!; while(<$file> { push (@servers, $_); #push each line of the file to array } print "$_\n" for(@servers); for my $server (@servers) { # do your thing with $server change_password($server); } sub change_password { my $system = shift; my $ssh = Expect->new('ssh amagana@' . $system); $ssh->debug(1); $ssh->log_file("$filename"); my $my_header = $header; $my_header =~ s/system/$system/; $ssh->print_log_file($my_header); $ssh->expect ( $timeout, [ qr/Password:/], [ qr/Are you sure you want to continue connecting \(yes\/no\)?/] ); 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"); } $ssh->expect(60, '$'); $ssh->send("su - root\n"); $ssh->expect(60, 'Password:'); $ssh->send("rootpassword\n"); $ssh->expect(60, '#'); $ssh->send("hostname\n"); $ssh->expect(60, '#'); $ssh->send("uptime\n"); $ssh->expect(60, '#'); $ssh->send("passwd amagana\n"); $ssh->expect(60, 'New UNIX Password:'); #linux #$ssh->expect(60, 'New Password:'); #solaris $ssh->send("mynewpassword\n"); $ssh->expect(60, 'Retype new UNIX password:'); #linux #$ssh->expect(60, 'Re-enter new Password:'); #solaris $ssh->send("mynewpassword\n"); $ssh->expect(60, '#'); $ssh->send("exit\n"); $ssh->expect(60, '$'); $ssh->send("exit\n"); $ssh->close(); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: How do I get different files into an array
by james28909 (Deacon) on Apr 14, 2015 at 20:25 UTC | |
by amagana (Acolyte) on Apr 14, 2015 at 22:26 UTC |