]>
git.ipfire.org Git - people/ms/libloc.git/blob - src/python/location-query.in
260f4bd88abe0102629d98006c0f338377551c7e
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 ###############################################################################
24 # Load our location module
33 self
.db
= location
.Database("@databasedir@/database.db")
36 parser
= argparse
.ArgumentParser(
37 description
=_("Location Database Command Line Interface"),
39 subparsers
= parser
.add_subparsers()
41 # Global configuration flags
42 parser
.add_argument("--debug", action
="store_true",
43 help=_("Enable debug output"))
45 # lookup an IP address
46 lookup
= subparsers
.add_parser("lookup",
47 help=_("Lookup one or multiple IP addresses"),
49 lookup
.add_argument("address", nargs
="+")
50 lookup
.set_defaults(func
=self
.handle_lookup
)
52 return parser
.parse_args()
55 # Parse command line arguments
56 args
= self
.parse_cli()
58 # Callback function must be defined
59 assert args
.func
, "Callback function not defined"
64 # Return with exit code
71 def handle_lookup(self
, ns
):
74 for address
in ns
.address
:
76 n
= self
.db
.lookup(address
)
78 sys
.stderr
.write(_("Invalid IP address: %s") % address
)
87 print(_("Nothing found for %(address)s") % args
)
91 # Try to retrieve the AS if we have an AS number
93 a
= self
.db
.get_as(n
.asn
)
95 # If we have found an AS we will print it in the message
101 print(_("%(address)s belongs to %(network)s which is a part of %(as)s") % args
)
104 print(_("%(address)s belongs to %(network)s") % args
)
110 # Run the command line interface