Re: How do I get different files into an array
by james28909 (Deacon) on Apr 14, 2015 at 00:20 UTC
|
use strict;
use warnings;
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);
| [reply] [d/l] |
|
|
Thank you james28909,
That worked perfectly!, The script stops and says su: incorrect password, these are Linux boxes at when it sends the passwords during the expect/send of su - root.
Is it because I will have to escape the special characters when it starts to send the strings with special characters, I did not have to do this with Solaris boxes?
Is there a way to just alway ignore special characters only during the send part of expect?
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 conne
+cting \(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 specia
+l 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 s
+cript 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 }
| [reply] [d/l] |
|
|
"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();
}
| [reply] [d/l] [select] |
|
|
open (my $file, '<', $ARGV[0]) or die $!;
to:
open (my $file, '<', "/home/amagana/scripts/lists/list_xx") or die $!;
then delete:
my $file = "/home/amagana/scripts/lists/list_xx"
| [reply] [d/l] [select] |
|
|
Re: How do I get different files into an array
by ww (Archbishop) on Apr 14, 2015 at 00:32 UTC
|
#! /usr/bin/perl -w
use 5.018;
#1123337
my @arr;
my @files=("Die2.txt", "Die1.txt", "Die1.bak",
); # assumes that you have code to ID the fil
+es
# you want and collect them into an array
for (@files) {
open (my $InFile, '<', $_) or die "cannot open $_, $!";
while (<$InFile>) {
my $var = $_;
push @arr, $var;
}
}
say @arr;
UPDATED: with the notes below.
And a note: If the way you phrased "... how I can get my already working script's array to read the contents of different files" is merely because English is not your native language it's not a big deal because I see I'm not the only Monk to decipher it (well, I hope we've done so accurately).
But if that phrasing accurately reflects your understanding of arrays, note that neither that array or any other can "read the contents" of anything. You have to explicitly tell Perl that you want data PUSHed onto an array... and that means you have to collect the data first -- by the method shown here, or by that in the prior ( ++ ) reply.
You could, of course, read about the record-input separator ($/, the $INPUT_RECORD_SEPARATOR ( aka "the field record separator" at Perl.com )and use that instead of the while loop above; which would spare you the need for Ln 19. Setting $/ = undef; will cause your read to slurp the entire file.
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
ww's solution is actually better than mine, the best way for you to do multiple files is to figure out a solution as to how you can create a list of files you need, wether it be all files in a dir or a pre-specified list. It sounds to me that your best bet is going to be to step back for a minute and think about how you can get a file list first. then parse each file with a for loop. Good Luck :)
| [reply] |
|
|
use strict;
use warnings;
open the first of the files w/server names;
read contents to a $var;
push $var to a new array (namely @all_server_names)
rinse, repeat untill all files have been read and their content pushed
Convert the above to code;
execute
| [reply] [d/l] |
|
|
My script stops and says su: incorrect password, these are Linux boxes at when it sends the passwords during the expect/send of su - root.
Is it because I will have to escape the special characters when it starts to send the strings with special characters, I did not have to do this with Solaris boxes?
Is there a way to just alway ignore special characters only during the send part of expect?
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 conne
+cting \(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 specia
+l 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 s
+cript 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 }
| [reply] [d/l] |