1
0
Fork 0
mirror of https://github.com/perlbot/perlbuut synced 2025-06-07 10:45:40 -04:00

Merge branch 'master' of github.com:perlbot/perlbuut

This commit is contained in:
Ryan Voots 2017-05-13 00:06:57 -04:00
commit 47f3972a51
2 changed files with 48 additions and 0 deletions

1
asndb/README.md Normal file
View file

@ -0,0 +1 @@
ASN db comes from https://iptoasn.com/

47
asndb/mkasn.pl Executable file
View file

@ -0,0 +1,47 @@
#!/usr/bin/env perl
use strict;
use warnings;
use autodie;
use DBI;
sub fmtip($) {
my $ip = shift;
sprintf "%03d.%03d.%03d.%03d", split(/\./, $ip);
}
open(my $tsv, "<", "ip2asn-v4.tsv");
my $dbh = DBI->connect("dbi:SQLite:dbname=../var/asn.db", "", "", {RaiseError => 1});
$dbh->do("DROP TABLE IF EXISTS asn;");
$dbh->do(q{CREATE TABLE asn (
start varchar(15) NOT NULL,
end varchar(15) NOT NULL,
asn INTEGER NOT NULL,
country TEXT,
desc TEXT
);
CREATE INDEX asn_start ON asn (start);
CREATE INDEX asn_end ON asn (end);
CREATE INDEX ON asn_asn ON asn (asn);});
$dbh->begin_work;
my $insert_sth = $dbh->prepare("INSERT INTO asn (start, end, asn, country, desc) VALUES (?, ?, ?, ?, ?);");
while (my $line = <$tsv>) {
chomp $line;
my ($start, $end, $asn, $country, $desc) = split /\t/, $line;
next if $asn eq 0;
printf "%s - %s\n", fmtip($start), fmtip($end);
$insert_sth->execute(fmtip $start, fmtip $end, $asn, $country, $desc);
}
#$dbh->commit();
#$dbh->begin_work();
for my $ip (0..255) {
my $rv = $dbh->do("DELETE FROM asn WHERE start <= ? AND end >= ?", {}, fmtip "10.$ip.0.0", fmtip "10.$ip.0.0");
$rv+=$dbh->do("DELETE FROM asn WHERE start <= ? AND end >= ?", {}, fmtip "172.$ip.0.0", fmtip "172.$ip.0.0") if $ip >= 16 || $ip <= 32;
$rv+=$dbh->do("DELETE FROM asn WHERE start <= ? AND end >= ?", {}, fmtip "192.168.$ip.0", fmtip "192.168.$ip.0");
print "Removed $rv for $ip\n";
}
$dbh->commit();