Hey guru's,
I've got a little problem that I am making into a large problem.. First I am trying to log into oh, about 2000 servers with the Expect.pm module, run a command that will output a great deal of data, then another command that will give me the OS information. I would like to write this data to a file on the server that I'm running the script we'll call this the host server. The issues I'm having are as follows:
the $exp-->log_file("output.log", "w") over writes itself after it logs into another server. I am having troubles figuring out how to get the output from the commands that are being executed to store into that file without over writing itself. Another option I thought of would be to store the command output on the remote server, the scp it back to the host server. Here's the code I'm working with:
#!/usr/bin/perl -w
use strict;
use Net::Ping;
use Net::SSH::Expect;
use IO::Prompt;
use Term::ReadKey;
$|=1; #flushes IO Writehandle
my $passwd = prompt("Please enter password:", -e => '*');
my $passwd1 = prompt("Please enter second password:", -e => '*');
open (HOSTS, "host_list") or die "can't find it! $!\n";
my @lines =<HOSTS>;
foreach my $line(@lines){
chomp($line);
my $p = Net::Ping->new("icmp");
unless($p->ping($line)){
print "$line <-- is unreachable please investig
+ate \n";
}else {
&log_in($line,$passwd,$passwd1);
}
$p->close();
}
close (HOSTS);
sub log_in{
my $timeout = 1;
my $cmd = 'dmidecode | grep -A 4 "System Information" | grep -A 3 "Pro
+duct Name:"';
my $cmd = 'ls -al';
my ($line,$passwd,$passwd1) = @_;
#Create expect object and spawn;
my $exp = new Expect() or die "Cannot spawn ssh command\n";
$exp->raw_pty(0);
#$exp->log_file ("output.log", "w");
$exp->spawn("ssh -l root $line") or die "Invalid data passed t
+o server... $!\n";
$exp->expect($timeout, ["[Tt]he" => sub {$_[0]->send("yes\n");
+}]);
$exp->expect($timeout, ["[Pp]assword" => sub { $_[0]->send("$p
+asswd\n"); }]);
$exp->expect($timeout, ["[Pp]ermission" => sub {$_[0]->send("$
+passwd1\n");}]);
$exp->expect($timeout, [ "[Rr]oot" => sub { $_[0]->send("$cmd\
+n"); } ]);
#sleep 2;
$exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send(
+"exit\n"); } ]);
}
Any suggestions would help. I'm running in circles with this and have tried a few different things with no success. Thank in advance guys.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.