Skip to content

Instantly share code, notes, and snippets.

@AmarJazzz
Created November 11, 2016 10:51
Show Gist options
  • Save AmarJazzz/83b24f7f8a6f70a619d768f64dccc76a to your computer and use it in GitHub Desktop.
Save AmarJazzz/83b24f7f8a6f70a619d768f64dccc76a to your computer and use it in GitHub Desktop.
Perl Log sample script
###########################
## LOG SCRIPT ##
## Written by :- AMAR ##
###########################
#!/usr/bin/perl
use Net::SSH::Expect;
use POSIX qw(strftime);
$server = "192.168.1.122";
$username = "amar";
$passwd = "Sample\@123";
my $now = strftime('%d-%m-%Y %H:%M:%S',localtime);
open ($Log,"+>>","/home/amar/logs/Sample.log");
print $Log ("$now ***********[START OF SCRIPT : DATABASE CHECK]***********\n");
print $Log ("$now Trying to connect to the Server : $server\n");
$ssh = Net::SSH::Expect->new(host => "$server",password => "$passwd",user => "$username",raw_pty => 1, timeout => 2);
$login_output=$ssh->login();
if ( $login_output !~ /Last login:/)
{
print "Failed to login into the Server : $server\n";
print $Log ("$now Failed to login into the Server : $server, Check the credentials\n");
}
else
{
print $Log ("$now Logged into the Server : $server\n");
## PERFORM UR ACTIVITY .....
print "Database check was successful\n";
print $Log ("$now DatabasE check was successful\n");
}
print $Log "$now *********[ DATABASE CHECK SCRIPT ENDED ]********\n";
@rementis
Copy link

rementis commented Oct 2, 2023

$now never changes. how do you know date and time of each logline?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment