Troush2009
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.
ASKER
Here is a example of my perl script.
Where would I place you example in this code?
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);
}
}
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;
ASKER
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;
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)
ASKER
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?
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?
ASKER
One more question, How would I place the logs in a specific directory?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Wow, never thought about turning that HOSTS array into a hash.
Thanks works well.
Thanks works well.
ASKER
Thanks!
Open in new window