]>
git.ipfire.org Git - people/ms/libloc.git/blob - src/python/location-query.in
933024eb1c030adf822238f60571d4bf7c8d7f67
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
)
38 self
.db
= location
.Database("@databasedir@/database.db")
41 parser
= argparse
.ArgumentParser(
42 description
=_("Location Database Command Line Interface"),
44 subparsers
= parser
.add_subparsers()
46 # Global configuration flags
47 parser
.add_argument("--debug", action
="store_true",
48 help=_("Enable debug output"))
51 parser
.add_argument("--version", action
="version",
52 version
="%%(prog)s %s" % location
.__version
__)
54 # lookup an IP address
55 lookup
= subparsers
.add_parser("lookup",
56 help=_("Lookup one or multiple IP addresses"),
58 lookup
.add_argument("address", nargs
="+")
59 lookup
.set_defaults(func
=self
.handle_lookup
)
62 get_as
= subparsers
.add_parser("get-as",
63 help=_("Get information about one or multiple Autonomous Systems"),
65 get_as
.add_argument("asn", nargs
="+")
66 get_as
.set_defaults(func
=self
.handle_get_as
)
68 return parser
.parse_args()
71 # Parse command line arguments
72 args
= self
.parse_cli()
74 # Callback function must be defined
75 assert args
.func
, "Callback function not defined"
80 # Return with exit code
87 def handle_lookup(self
, ns
):
90 for address
in ns
.address
:
92 n
= self
.db
.lookup(address
)
94 sys
.stderr
.write(_("Invalid IP address: %s") % address
)
103 print(_("Nothing found for %(address)s") % args
)
107 # Try to retrieve the AS if we have an AS number
109 a
= self
.db
.get_as(n
.asn
)
111 # If we have found an AS we will print it in the message
117 print(_("%(address)s belongs to %(network)s which is a part of %(as)s") % args
)
120 print(_("%(address)s belongs to %(network)s") % args
)
124 def handle_get_as(self
, ns
):
126 Gets information about Autonomous Systems
134 sys
.stderr
.write("Invalid ASN: %s" %asn
)
138 # Fetch AS from database
139 a
= self
.db
.get_as(asn
)
143 print(_("Could not find AS%s") % asn
)
147 print(_("AS%(asn)s belongs to %(name)s") % { "asn" : a
.number
, "name" : a
.name
})
152 # Run the command line interface