Import old code, circa 2005

This commit is contained in:
2026-05-18 12:43:29 -04:00
commit 140d1eae64
3 changed files with 388 additions and 0 deletions

198
logger Executable file
View File

@@ -0,0 +1,198 @@
#!/usr/bin/perl -w
#extract lines in a given date range from an xchat logfile
#is a bit too hardcoded to be translated to other clients (gaim, etc)
#sorry! Future update will see this more modular.
use Getopt::Std;
use strict;
#extractDate(\%hash, $line)
#extract the contents of the date in line and put it in a hash
#(hash is keyed by month, day, hour, minute, second)
sub extractDate;
#buildRegex($line)
#build a regex from an input string of the form MM HH MM:SS:DD and return it
sub buildRegex;
my %args;
my $buffer;
#regexes for quick and easy pattern matching
my $startregex;
my $endregex;
#start and end date hashes for more complex range matching
my %startdate;
my %enddate;
# additional regex to check for in lines of output
my $addlRegex = ".*";
#Yeah this is a bit hackish. Whaddya want from me? This is, like, my second perl script.
my %monthvals = (
Jan => 0,
Feb => 1,
Mar => 2,
Apr => 3,
May => 4,
Jun => 5,
Jul => 6,
Aug => 7,
Sep => 8,
Oct => 9,
Nov => 10,
Dec => 11,
jan => 0,
feb => 1,
mar => 2,
apr => 3,
may => 4,
jun => 5,
jul => 6,
aug => 7,
sep => 8,
oct => 9,
nov => 10,
dec => 11,
);
my $help;
$help = <<EOT;
logger: filter X-Chat 2 log files by date
logger <options> inputfile
-h : this help
-s : Starting date
-e : Ending date
-r : Further restrict the search to lines matching this regex (search, not replace)
inputfile defaults to stdin if no file provided
Dates are expected in "Month Day Hour:Minute:Second" format. Months
are abbreviated to 3 letters, non-case sensitive. You must provide
AT LEAST one regex. (If one is missing, then it is assumed that both -s
and -e are equal.)
To search for all lines on the 13th of May:
logger -s "May 13 *:*:*" xchatlogfile
To search for all lines between the 13th of May and the 20th of June:
logger -s "May 13 *:*:*" -e "Jun 20 *:*:*" xchatlogfile
To search for all lines written between 5 and 10 pm in the entire log:
logger -s "* * 17:*:*" -e "* * 22:*:*" xchatlogfile
As above, but only match lines containing "somenick"
logger -s "* * 17:*:*" -e "* * 22:*:*" -r somenick xchatlogfile
EOT
getopt ( "hxs:e:f:r:", \%args );
# there's quite a bit of redundancy in the argument checking. Sue me.
if ( $args{h} ) {
print $help;
exit;
}
if ( $args{r} ) {
$addlRegex = $args{r};
}
if ( $args{s} ) {
$startregex = buildRegex($args{s});
extractDate(\%startdate, $args{s});
}
if ( $args{e} ) {
$endregex = buildRegex($args{e});
extractDate(\%enddate, $args{e});
}
if ( !$args{e} && !$args{s} ) {
print $help;
exit;
}
elsif ( !$args{e} || !$args{s} ) {
if ( $args{s} ) {
$endregex = $startregex;
extractDate(\%enddate, $args{s});
}
elsif ( $args{e} ) {
$startregex = $endregex;
extractDate(\%startdate, $args{e});
}
}
while ( <> ) {
my $toPrint;
if ( $_ =~ m/^\*\*\*\*/i || $_ =~ m/^\s*\n/i ) {
# this is a xchat log status line (effectively a comment for our purpose)
next;
}
if ( $_ =~ m/$startregex/i ||
$_ =~ m/$endregex/i ) {
$toPrint = $_;
}
else {
# not lucky enough for an exact match ... check the date
my %linedate;
extractDate(\%linedate, $_);
# check the dates to see if the date in the line is between startdate
# and end date
if ( !($enddate{month} eq "*") &&
$monthvals{$linedate{month}} <= $monthvals{$enddate{month}} &&
$monthvals{$linedate{month}} >= $monthvals{$startdate{month}} ) {
if ( !($enddate{day} eq "*") &&
$linedate{day} <= $enddate{day} &&
$linedate{day} >= $startdate{day} ) {
if ( !($enddate{hour} eq "*") &&
$linedate{hour} <= $enddate{hour} &&
$linedate{hour} >= $startdate{hour} ) {
if (!($enddate{minute} eq "*") &&
$linedate{minute} <= $enddate{minute} &&
$linedate{minute} >= $startdate{minute} ) {
if (!($enddate{second} eq "*") &&
$linedate{second} <= $enddate{second} &&
$linedate{second} >= $enddate{second} ) {
$toPrint = $_;
}
}
}
}
}
}
if ( $toPrint && ($toPrint =~ m/$addlRegex/) ) {
print $toPrint;
}
}
sub buildRegex {
my $orig = shift;
# make sure to not muck up the original
my $regex = $orig;
$regex =~ s/\s/\\s/g;
$regex =~ s/\*/\.\*/g;
return $regex;
}
sub extractDate {
my $finalDate = shift; #expects a reference to a hash
my $buff = shift; #expects a line of text from the logfile
#expects buff in the form MM DD HH:MM:SS
my @date = split " ", $buff;
my @time = split ":", $buff;
$time[0] =~ s/$date[0]\s$date[1]\s//;
$finalDate->{month} = $date[0];
$finalDate->{day} = $date[1];
$finalDate->{hour} = $time[0];
$finalDate->{minute} = $time[1];
my @splitline = split " ", $time[2];
$finalDate->{second} = $splitline[0];
}

94
portscan.pl Executable file
View File

@@ -0,0 +1,94 @@
#!/usr/bin/perl -w
#This could really stand to be improved, but it works for basic purposes
#right now of identifying open ports on the given host.
#The big improvement would be a hash containing basic jibberish to
#send to given ports (eg 22, 21, 25, 80, 110) to see if they're
#running standard services.
use IO::Socket;
use Getopt::Std;
use strict;
my %args;
my $socket;
my $host = "127.0.0.1";
my $lowport = 0;
my $highport = 65535;
my $wait = 10;
my $outfile;
my $jibberish = "\n";
my $usage = <<EOT;
portscan.pl : scan a range of ports on a given host and report active ports
-h : this help
-t : the target hostname or IP address (defaults to 127.0.0.1)
-m : minimum port number (defaults to 1)
-M : maximum port number (defaults to 65535)
-w : timeout time (wait this long to drop attempt - defaults to
10 seconds)
-f : write results to specified logfile instead of stdout
-v : be particularly verbose
-j : specify the jibberish to send to the ports found to elicit
a response
EOT
getopts("ht:m:M:w:vf:j:", \%args);
if ( defined $args{h} ) {
print $usage;
exit;
}
if ( defined $args{t} ) {
$host = $args{t};
}
if ( defined $args{m} ) {
$lowport = $args{m};
}
if ( defined $args{M} ) {
$highport = $args{M};
}
if ( defined $args{w} ) {
$wait = $args{w};
}
if ( defined $args{j} ) {
$jibberish = $args{j};
}
if ( !$host ) {
print $usage;
exit;
}
if ( defined $args{f} ) {
open($outfile, ">$args{f}")
or die "Couldn't open output file $args{f}";
}
foreach ( $lowport ... $highport ) {
my $response = "";
if ( $args{v} ) {
$response .= "trying $host port $_\n";
}
$socket = IO::Socket::INET->new(PeerAddr => $host,
PeerPort => $_,
Proto => "tcp",
Type => SOCK_STREAM,
Timeout => $wait)
or next;
$response .= "Got socket on $host port $_\n";
if ( $socket && $args{v} ) {
# got a connection, let's throw some jibberish at it and
# see what happens!
print $socket $jibberish;
my $response .= <$socket>;
close($socket);
}
if ( $outfile ) {
print $outfile $response;
}
else {
print $response;
}
}
if ( $outfile ) {
close($outfile);
}

96
updateHosts.pl Executable file
View File

@@ -0,0 +1,96 @@
#!/usr/bin/perl -w
# updateHosts.pl Andrew Kesterson andrew@aklabs.net
# update the system hosts file with Mike's ad blocking file
# patch it up to include the newest entries
# this obviously needs to run as root if you're working on /etc/hosts
# I'm still learning perl. Don't harsh me too much.
use strict;
use LWP::Simple;
use Socket;
use Getopt::Std;
use Time::localtime;
my $URL = "http://everythingisnt.com/hosts";
my $hostfile = "/etc/hosts";
my $buffer;
my %args;
my $updating = 0;
my $usage = <<EOT;
updateHosts.pl : update your hosts file from Mike's ad blocking
file at http://everythingisnt.com/hosts. New hosts are added,
nothing is removed.
-h : this help
-v : be verbose (default is to stay silent until an error occurs)
-u : specify alternate hosts URL. Must be a UNIX /etc/hosts file
(defaults to Mike's ad blocking file)
-f : specify alternate filename to patch (defaults to /etc/hosts)
EOT
getopts("hvu:f:", \%args);
if ( $args{h} ) {
print $usage;
exit;
}
if ( $args{u} ) {
$URL = $args{u};
}
if ( $args{f} ) {
$hostfile = $args{f};
}
unless (defined ( $buffer = get $URL ) ) {
die "Couldn't grab updated host file from $URL\n";
}
my @newHosts = split /\n/, $buffer;
open(hostsFile, ">>$hostfile") or die "Couldn't open $hostfile for writing.";
if (defined $args{v} ) {
print "Beginning update; due to calls to gethostbyname, the program might appear to hang while waiting to resolve.\n";
}
HOSTS: foreach my $host ( @newHosts ) {
$host =~ m/(127.0.0.1\s\w.*)/;
if ( defined $1 ) {
$host = $1;
$host =~ s/127.0.0.1\s//;
$host =~ s/\s.*//;
}
else {
# this should've returned us a hostname...
next;
}
my @addresses = gethostbyname($host);
# just checking to see if it already exists in etc-hosts as 127.0.0.1
@addresses = map { inet_ntoa($_) } @addresses[ 4 ... $#addresses];
foreach my $addr ( @addresses ) {
if ( $args{v} ) {
print "checking 127.0.0.1 against address for $host : $addr\n";
}
if ( $addr eq "127.0.0.1" ) {
# skip this one.
next HOSTS;
}
else {
if ( $args{v} ) {
print "adding $host\n";
}
if ( !$updating ) {
# add a comment with the date if this is the first entry
my $time = localtime;
print hostsFile "\n# Updates from ", $time->year+1900 ,
"-", $time->mon+1, "-", $time->mday, "\n";
$updating = 1;
}
print hostsFile "127.0.0.1\t", $host, "\n";
}
}
}
close hostsFile;