#!/usr/bin/perl
use strict;
use Net::SSH::Expect;
use Data::Dumper;
# for hidden password
use Term::ReadKey;
# flush buffer to output so all is not lost if
# you need to cancel script before it finishes
$| = 1;
my ($user, $pwd) = &get_login;
my $ntp_command = " grep server /etc/ntp.conf | grep -v '#' ";
# use this for solaris
# my $ntp_command = " grep server /etc/inet/ntp.conf | grep -v '^\#' ";
my $infile = $ARGV[0] || &get_filename("Input");
my $outfile = $ARGV[1] || &get_filename("Output");
my $outfile2 = $outfile . "_2";
chomp $infile;
chomp $outfile;
open (INPUT, $infile) || die "Unable to open $infile " . $! . "\n";
open (OUTPUT, ">>$outfile") || die "Unable to open $outfile " . $! . "\n";
my $current_time_command = ' date';
my $ntp_command = " grep server /etc/ntp.conf | grep -v '#' ";
# use this for solaris
# my $ntp_command = " grep server /etc/inet/ntp.conf | grep -v '^\#' ";
while () {
chomp (my $line = $_);
# ignore lines that begin with comment character
next if ($line =~ /^\s*#/);
# ignore empty lines
next if ($line !~ /\w/);
my ($hozt, $ipaddr) = split('\s+',$line);
print "Working on $hozt\n";
print OUTPUT "$hozt\n";
my $login_output;
my $ssh = Net::SSH::Expect->new (
host => $hozt,
password => $pwd,
user => $user,
raw_pty => 1,
timeout => 18
);
eval {
$login_output = $ssh->login(7);
} or do {
print OUTPUT "login failed\n\n";
print "login failed on $hozt\n\n";
next;
};
$ssh->exec("stty raw -echo");
$ssh->send(" $current_time_command ");
chomp (my $hostdate = $ssh->read_line());
chomp (my $localdate = `date`);
print OUTPUT " compare timestamps:\n";
print OUTPUT " correct time = $localdate\n";
print OUTPUT " time on host = $hostdate\n";
$ssh->send(" $ntp_command ");
chomp (my $ntpservers = $ssh->read_all());
print OUTPUT "$ntpservers\n";
}
close INPUT;
close OUTPUT;
`dos2unix $outfile`;
print "See results in $outfile\n";
exit (0);
# *********************************
sub get_filename {
my $filetype = shift;
print "$filetype filename? ";
my $filename = ;
return $filename;
}
# *********************************
sub get_login {
print "Username: ";
chomp (my $user = );
print "Hidden password: ";
ReadMode('noecho'); # don't echo
chomp (my $pwd = );
ReadMode(0); # back to normal
print "\n";
return ($user,$pwd);
}
# *********************************