#!/usr/bin/perl # # $Id: bgp-as,v 1.2 2009/06/03 08:55:52 suter Exp $ # Copyright (C) 2008 Mark Suter # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see L. use strict; use warnings; use English qw( -no_match_vars ); use Getopt::Long; use Pod::Usage; use WWW::Mechanize; our ($VERSION) = '$Revision: 1.2 $' =~ m{ \$Revision: \s+ (\S+) }msx; ## no critic "RequireInterpolationOfMetachars" ## Process the command line my %opt = ( man => 0, help => 0, net => undef, table => 'Domestic' ); GetOptions( \%opt, 'man', 'help', 'net=s', 'table=s' ) or pod2usage(0); $opt{man} and pod2usage( -exitval => 0, -verbose => 2 ); $opt{help} and pod2usage(0); scalar @ARGV and pod2usage(1); defined $opt{net} or pod2usage(1); ## Ensure the key arguments are valid $opt{table} =~ m{ \A ( Domestic | International ) \Z }msix or die "BGP table must be either Domestic or International\n"; $opt{net} =~ m{ \A ( \d{1,3} . \d{1,3} . \d{1,3} . \d{1,3} (?: / \d{2} )? ) \Z }msix or die "Network must be an IP address with an optional prefix, for example, 192.0.2.0/24\n"; ## Ready our web browser my $mech = WWW::Mechanize->new( autocheck => 1, quiet => 1 ); $mech->agent_alias('Windows Mozilla'); $mech->env_proxy(); ## Get an answer $mech->get('http://looking-glass.optus.net.au/'); $mech->set_visible( 'bgp', $opt{net}, $opt{table} ); $mech->submit(); ## Print the content of the
if ( $mech->content =~ m{ \A .*? 
 \s* (.+?) \s* 
.* \Z }msix ) { print $1, "\n" or die "print: $OS_ERROR\n"; } else { print $mech->content or die "print: $OS_ERROR\n"; } __END__ =for stopwords bgp =head1 NAME bgp-as - Query the Optus Looking Glass =head1 USAGE bgp-as --net=192.0.2.0/24 =head1 REQUIRED ARGUMENTS =over 8 =item B<--net>=B Give the network to be looked up in the BGP tables, for example, 192.0.2.0/24. The prefix is optional. =back =head1 OPTIONS =over 8 =item B<--table>=B Choose either the 'Domestic' or 'International' table to query. =item B<--man> Print the manual page and exit. =item B<--help> Print a brief help message and exit. =back =head1 DESCRIPTION B uses http://looking-glass.optus.net.au/ to display the BGP table for the given network. =head1 DIAGNOSTICS If this script isn't working, try using the website directly. =head1 EXIT STATUS If B exits with a zero exit status and the correct output is on standard output. Nothing else is ever printed to standard output. B will exit with a non-zero exit status if there was a fatal error. Both fatal and non-fatal errors will cause output on standard error. =head1 CONFIGURATION If you need to access the Internet via a proxy, please ensure you have set the http_proxy environment variable. =head1 DEPENDENCIES Do B use this script in an automated way - here's the warning from the http://looking-glass.optus.net.au/ webpage: Optus Looking Glass is NOT to be used with ANY automated scripts. All queries must be entered manually. Abusers of this policy will be permanently banned from this server. Please respect their clearly stated wishes and just use this manually from the command line - don't force Optus to withdraw this valuable service to the Internet community =head1 INCOMPATIBILITIES As per the dependencies, this script is not compatible with any automated usage. =head1 EXAMPLES $ ~/bin/bgp-as --net 150.101.98.0/24 --table International % Network not in table $ ~/bin/bgp-as --net 150.101.98.0 --table International BGP routing table entry for 150.101.0.0/16, version 902222264 Paths: (2 available, best #1) Flag: 0x820 Not advertised to any peer 7473 2914 4739 4739 4739 203.208.191.157 from 203.202.143.19 (203.202.143.19) Origin IGP, metric 0, localpref 50, valid, internal, best Community: 7473:21045 7473:31415 7474:1403 Originator: 203.202.143.1, Cluster list: 0.0.0.1 7473 2914 4739 4739 4739 203.208.191.157 from 203.202.143.20 (203.202.143.20) Origin IGP, metric 0, localpref 50, valid, internal Community: 7473:21045 7473:31415 7474:1403 Originator: 203.202.143.1, Cluster list: 0.0.0.1 =head1 BUGS AND LIMITATIONS No known bugs - please email me with any problems. =head1 AUTHOR Mark Suter EFE =head1 LICENSE AND COPYRIGHT Copyright (C) 2008 Mark Suter EFE This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see L. =cut