Gradian has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Expect script to pull data from multiple servers...
by almut (Canon) on Nov 18, 2008 at 23:49 UTC
    I am having troubles figuring out how to get the output from the commands that are being executed

    Maybe Ok, so now how do I get what was read on the handle?

    (though, if there's truly lots of output, I'm not sure if you might run into limits of the accumulating buffer's size...)

Re: Expect script to pull data from multiple servers...
by ig (Vicar) on Nov 19, 2008 at 03:29 UTC

    Expect documentation says:

    $object->log_file("filename" | $filehandle | \&coderef | undef) Log session to a file. All characters send to or received from the spa +wned process are written to the file. Normally appends to the logfile +, but you can pass an additional mode of "w" to truncate the file upo +n open():

    You have

    $exp->log_file ("output.log", "w");

    If you remove the second argument ("w"), then it should append to the file. So you can try:

    $exp->log_file ("output.log");
      This worked great, Thanks a ton. Something so small right in my face.. Thanks again ig