2 ###############################################################################
4 # libloc - A library to determine the location of someone on the Internet #
6 # Copyright (C) 2017 IPFire Development Team <info@ipfire.org> #
8 # This library is free software; you can redistribute it and/or #
9 # modify it under the terms of the GNU Lesser General Public #
10 # License as published by the Free Software Foundation; either #
11 # version 2.1 of the License, or (at your option) any later version. #
13 # This library is distributed in the hope that it will be useful, #
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
16 # Lesser General Public License for more details. #
18 ###############################################################################
25 # Load our location module
29 def _(singular
, plural
=None, n
=None):
31 return gettext
.dngettext("libloc", singular
, plural
, n
)
33 return gettext
.dgettext("libloc", singular
)
37 parser
= argparse
.ArgumentParser(
38 description
=_("Location Database Command Line Interface"),
40 subparsers
= parser
.add_subparsers()
42 # Global configuration flags
43 parser
.add_argument("--debug", action
="store_true",
44 help=_("Enable debug output"))
47 parser
.add_argument("--version", action
="version",
48 version
="%%(prog)s %s" % location
.__version
__)
51 parser
.add_argument("--database", "-d",
52 default
="@databasedir@/database.db", help=_("Path to database"),
55 # lookup an IP address
56 lookup
= subparsers
.add_parser("lookup",
57 help=_("Lookup one or multiple IP addresses"),
59 lookup
.add_argument("address", nargs
="+")
60 lookup
.set_defaults(func
=self
.handle_lookup
)
63 get_as
= subparsers
.add_parser("get-as",
64 help=_("Get information about one or multiple Autonomous Systems"),
66 get_as
.add_argument("asn", nargs
="+")
67 get_as
.set_defaults(func
=self
.handle_get_as
)
70 search_as
= subparsers
.add_parser("search-as",
71 help=_("Search for Autonomous Systems that match the string"),
73 search_as
.add_argument("query", nargs
=1)
74 search_as
.set_defaults(func
=self
.handle_search_as
)
76 # List all networks in a country
77 search_as
= subparsers
.add_parser("list-networks-by-cc",
78 help=_("Lists all networks in a country"),
80 search_as
.add_argument("country_code", nargs
=1)
81 search_as
.set_defaults(func
=self
.handle_list_networks_by_cc
)
83 return parser
.parse_args()
86 # Parse command line arguments
87 args
= self
.parse_cli()
89 # Callback function must be defined
90 assert args
.func
, "Callback function not defined"
94 db
= location
.Database(args
.database
)
95 except FileNotFoundError
as e
:
96 sys
.stderr
.write("location-query: Could not open database %s: %s\n" \
101 ret
= args
.func(db
, args
)
103 # Return with exit code
107 # Otherwise just exit
110 def handle_lookup(self
, db
, ns
):
113 for address
in ns
.address
:
115 n
= db
.lookup(address
)
117 print(_("Invalid IP address: %s") % address
, file=sys
.stderr
)
126 print(_("Nothing found for %(address)s") % args
, file=sys
.stderr
)
130 # Try to retrieve the AS if we have an AS number
134 # If we have found an AS we will print it in the message
140 print(_("%(address)s belongs to %(network)s which is a part of %(as)s") % args
)
143 print(_("%(address)s belongs to %(network)s") % args
)
147 def handle_get_as(self
, db
, ns
):
149 Gets information about Autonomous Systems
157 print(_("Invalid ASN: %s") % asn
, file=sys
.stderr
)
161 # Fetch AS from database
166 print(_("Could not find AS%s") % asn
, file=sys
.stderr
)
170 print(_("AS%(asn)s belongs to %(name)s") % { "asn" : a
.number
, "name" : a
.name
})
174 def handle_search_as(self
, db
, ns
):
175 for query
in ns
.query
:
176 # Print all matches ASes
177 for a
in db
.search_as(query
):
180 def handle_list_networks_by_cc(self
, db
, ns
):
181 for country_code
in ns
.country_code
:
182 # Print all matching networks
183 for n
in db
.search_networks(country_code
=country_code
):
187 # Run the command line interface