-
Notifications
You must be signed in to change notification settings - Fork 0
/
filtergenSQL
executable file
·110 lines (95 loc) · 2.51 KB
/
filtergenSQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env perl
# route filter generator
#
# This script works by querying a mysql cluster
# for peers and their AS-SET or ASN.
# It then generates a list of valid prefixes for each peer
# and compares this to a previous version, looking for updates.
#
# If changes are found, it then emails a complete updated prefix list
# for the peer, also generating an XML config to be uploaded to the router.
#
# Files:
#
# filtergen - main code, run from cron
# config.pl - contains configuration info (edit to suit your needs)
#
# written by andy@shady.org
#
# Requires: asused (/usr/ports/net-mgmt/asused)
# define debug if required
$debug=1;
use Whois; # asused whois module
use DBI;
use lib "./lib";
use FilterGen;
getconfigs();
# Setup SQL stuff and get details
$dbh = DBI->connect("DBI:mysql:$sqldbase:$sqlhost", $sqluser, $sqlpass);
$c1 = "SELECT DISTINCT ASN, AS_SET from peer_details where Type='IXP'";
$cmd = $dbh->prepare($c1);
$cmd->execute;
$rows = $cmd->rows;
# open IRR DB connection only once
$whois = new Whois('Host' => 'whois.ripe.net');
die("Failed to create object") unless(ref($whois));
die($whois->GetError()) if($whois->GetError());
# Now step through SQL rows
# We have to do everyting within this loop
for ($i = 0; $i < $rows; $i++)
{
my %asnhash=();
my %assetlist=();
my %processed_assetlist=();
my %prefixes=();
my @addedprefixes=();
my @deletedprefixes=();
my @xmlprefixes=();
($asn,$asset) = $cmd->fetchrow_array;
if($asset ne "") # blank field
{
# populate as set array
$asn="AS$asn";
$assetlist{$asset}=$asset;
# now do the do on that as-set
# repopulate within the array
while (values(%assetlist) > 0)
{
my $key=();
foreach $key (keys (%assetlist))
{
if($debug)
{
print "$assetlist{$key} [Top Level]\n";
}
&queryASset($key);
$processed_assetlist{$key}=$key;
delete $assetlist{$key};
}
}
# now we should have a list of ASNs
# grab the prefixes for all AS-sets
&processASNlist(%asnhash);
}
else
{
# no as set, populate ASN array
$asn="AS$asn";
if($debug)
{
print("$asn [Top Level]\n");
}
&processSingleASN($asn);
}
# now we have a list of prefixes
# check for previous runs and diff
&checklast(%prefixes);
# Email the output and generate the XML template
&emailresult(@addedprefixes, @deletedprefixes);
&outputxml(@xmlprefixes);
undef(@prefixes);
undef(@addedprefixes);
undef(@deletedprefixes);
undef(@xmlprefixes);
}
$whois->Close();