lsq.pl


Code Index:


NAME

lsq.pl <user> <host>


DESCRIPTION

Display time elapsed since last Squid connection from <user> through <host>.


COPYRIGHT

Copyright 2010 Etienne LEMEE <coding AT etilem DOT net>

This piece of code is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


#!/usr/bin/perl -w

use strict;
use Socket;
use HTTP::Date;
use Time::Duration;

@ARGV > 0 or die << "USAGE";
usage: $0 <user> <host>
display time elapsed since last Squid connection from <user> through <host>.
USAGE

my ($name, $host) = @ARGV;
my $zone = ".etilem.net";

sub get_ip {
    my $n = shift;
    if (gethostbyname $n) {
        return inet_ntoa (inet_aton $n);
    } else {
        print "This domain name does not exist.";
        exit 1;
    }
}

sub get_last_conn {
    my ($h, $i) = @_;
    my $l = qx !ssh -l root $h grep $i /var/log/squid/access.log|tail -1|cut -d[ -f2|cut -d] -f1!;
    unless ($l) {
        print "No Squid record found.";
        exit 1;
    } else {
        return $l;
    }
}

sub get_duration {
    my ($h, $n) = @_;
    my $i = get_ip ($n);
    return duration (str2time (get_last_conn ($h, $i)) - time());
}

$host ||= "proxy";
$name .= $zone;
$host .= $zone;

printf "Last seen %s ago.", get_duration ($host, $name);