#!/usr/bin/perl
use strict;
use IO::Socket::INET;
use IO::File;
use Data::Dumper;
use POSIX qw(strftime);
use constant 'hostname' => '85.187.38.18';
use constant 'destport' => '80';
use constant 'protocol' => 'tcp';
use constant 'logfile' => '/var/log/netstatus.log';
use constant 'timeout' => 3; # seconds
{
my $sock = IO::Socket::INET->new(PeerAddr => hostname,
PeerPort => destport,
Proto => protocol,
Timeout => timeout);
if ($sock) {
write_result();
}
}
sub write_result
{
my $date = strftime "%b %e %Y", localtime;
my $fh = new IO::File logfile, "r+";
my @data = <$fh>;
my $matched = undef;
foreach my $line (@data) {
if ($line =~ /^\[$date\]/) {
print $fh '.';
$matched = 1;
}
}
if (not $matched) {
print $fh "\n[$date] .";
}
$fh->close;
}