Link to home
Start Free TrialLog in
Avatar of Troush2009
Troush2009Flag for United States of America

asked on

How to write a new file with output from script

How to write a new file with output from script? My script logins into few remoter servers and runs few commands and I would like to take output and place into a log file with Name,time and date.
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

We need more details to give you an exact answer.  In general, Perl code would look something like this:
use POSIX;
open OUT, ">file" or die $!;
# do remote stuff and capture output (backticks, open pipe, etc)
print OUT $name, strftime('fmt_you_want', localtime), $remote_results;

Open in new window

Avatar of Troush2009

ASKER

Here is a example of my perl script.
Where would I place you example in this code?


#!/usr/bin/perl                                                                                                                                
use strict;                                                                                
use warnings;                                                                              
use Net::OpenSSH;                                                                          
$|=1;                                                                                      
my $user = 'root';                                                                                                                                        
my $password = 'rootroot';                                                                 
use vars qw ($cmd $host @HOSTS @CMDS); #                                                   
#                                                                                          
my @HOSTS= (                                                                               
                "server1",                                                       
                "server2",                                                       
                "server3",                                                                           
);                                                                                         
my @CMDS= (                                                                                
             "hostname",                                                                                                                     
             "service rsyslog status",                                                     
             "df -h",                                                                  
             "echo '***** *****'",                                                     
);                                                                                         
                                                                                           
foreach $host (@HOSTS[0..1]) {                                                             
chomp $host;                                                                               
#print "$host\n";                                                                          
my $ssh = Net::OpenSSH->new(host=>"$host", user=>"$user", port=>22, password=>"$password");
  foreach $cmd (@CMDS) {                                                                   
   chomp $cmd;                                                                             
   #print "$cmd\n";                                                                        
  $ssh->system($cmd);                                                                      
  }                                                                                        
}  

Open in new window

You'll have to change your ssh call.  This should work:
#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw(strftime);
use Net::OpenSSH;
$|=1;

my $user = 'root';
my $password = 'rootroot';
use vars qw ($cmd $host @HOSTS @CMDS); #

my @HOSTS= (
                "server1",
                "server2",
                "server3",
);
my @CMDS= (
             "hostname",
             "service rsyslog status",
             "df -h",
             "echo '***** *****'",
);

open OUT, ">>ssh_out.log" or die "could not write ssh_out.log for $cmd: $!";

foreach $host (@HOSTS[0..1]) {
  chomp $host;
  #print "$host\n";
  my $ssh = Net::OpenSSH->new(host=>"$host", user=>"$user", port=>22, password=>"$password");
  $ssh->error and die "failed to connect to $host: " . $ssh->error;
  foreach $cmd (@CMDS) {
    chomp $cmd;
    #print "$cmd\n";
    my ($rout, $pid) = $ssh->pipe_out($cmd) or die "$cmd failed: " . $ssh->error;
    print OUT join('  ', $host, strftime('%Y-%m-%d  %H:%M:%S', localtime), $cmd), ":\n";
    while (<$rout>) {
      print OUT $_;
    }
  }
}

close OUT;

Open in new window

I was able to get that to work but how would I go through a 2nd loop an copy it to same log file?
open OUT, ">>ssh_out.log" or die "could not write ssh_out.log for $cmd: $!";

foreach $host (@HOSTS[0..1]) {
  chomp $host;
  print "$host\n";
  my $ssh = Net::OpenSSH->new(host=>"$host", user=>"$user", port=>22, password=>"$password");
  $ssh->error and die "failed to connect to $host: " . $ssh->error;
  foreach $cmd (@CMDS) {
    chomp $cmd;
    #print "$cmd\n";
    my ($rout, $pid) = $ssh->pipe_out($cmd) or die "$cmd failed: " . $ssh->error;
    print OUT join('  ', $host, strftime('%Y-%m-%d  %H:%M:%S', localtime), $cmd), ":\n";
    while (<$rout>) {
      print OUT $_;
    }
  }
}

foreach $host (@HOSTS[2..9]) {
chomp $host;
#print "$host\n";
my $ssh = Net::OpenSSH->new(host=>"$host", user=>"$user", port=>22, password=>"$password");
$ssh->error and die "failed to connect to $host: " . $ssh->error;
  foreach $cmd (@CMDS[0..2, 9..10]) {
   chomp $cmd;
    my ($rout, $pid) = $ssh->pipe_out($cmd) or die "$cmd failed: " . $ssh->error;
    print OUT join('  ', $host, strftime('%Y-%m-%d  %H:%M:%S', localtime), $cmd), ":\n";
    while (<$rout>) {
      print OUT $_;
  }
 }
}
close OUT;

Open in new window

What do you mean?  The script appends to the existing log (>> instead of > in the open) and only opens it once for each run of the script (open is outside of all loops).  Basically logic looks like this:
open log file (append)
    connect to each host
        run each command on each host
            write output to log file (for each command on each host)

Open in new window

Sorry about last question but I understand., had a type issue.
         
                   How could I put same strtime on the log name?
                   Is it also possible to see the results on my screen as it getting copy to log file?
One more question, How would I place the logs in a specific directory?
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Wow, never thought about turning that HOSTS array into a hash.
Thanks works well.
Thanks!