#!/usr/bin/perl -w use strict; use DBI; my $username = undef; if (defined($ARGV[0])) { if ($ARGV[0] =~ /^[\w-]{1,8}$/) { $username = $ARGV[0]; } else { die("Wrong username format"); } } open(CONF, ") { if (/^user=(\S+)/) { $user = $1; } elsif (/^pass=(\S+)/) { $pass = $1; } } close(CONF); if (!defined($user) || !defined($pass)) { die("Wrong MySQL conf file\n"); } my $dsn = "DBI:mysql:database=HCCLogIP;host=localhost;port=3306"; my $dbh = DBI->connect($dsn, $user, $pass); my $sth; if (defined($username)) { $sth = $dbh->prepare(" SELECT `ip`, `username`, DATE_FORMAT(`timestamp`, '%Y/%m/%d %H:%i:%s') AS `date` FROM `HCCLogIP` WHERE `username` = ? ORDER BY `timestamp` DESC "); $sth->execute($username); } else { $sth = $dbh->prepare(" SELECT `ip`, `username`, DATE_FORMAT(`timestamp`, '%Y/%m/%d %H:%i:%s') AS `date` FROM `HCCLogIP` ORDER BY `timestamp` DESC "); $sth->execute(); } if ($sth->rows) { while (my $row = $sth->fetchrow_hashref()) { printf("%s - %s - %s\n", $row->{'date'}, $row->{'username'}, $row->{'ip'}); } } else { print "No entries found\n"; } $sth->finish(); $dbh->disconnect(); exit;