]> git.ipfire.org Git - people/ms/libloc.git/blob - src/python/location-query.in
python: Expose version of the module
[people/ms/libloc.git] / src / python / location-query.in
1 #!/usr/bin/python3
2 ###############################################################################
3 # #
4 # libloc - A library to determine the location of someone on the Internet #
5 # #
6 # Copyright (C) 2017 IPFire Development Team <info@ipfire.org> #
7 # #
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. #
12 # #
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. #
17 # #
18 ###############################################################################
19
20 import argparse
21 import sys
22 import syslog
23
24 # Load our location module
25 import location
26
27 # i18n
28 _ = lambda x: x
29
30 class CLI(object):
31 def __init__(self):
32 # Open database
33 self.db = location.Database("@databasedir@/database.db")
34
35 def parse_cli(self):
36 parser = argparse.ArgumentParser(
37 description=_("Location Database Command Line Interface"),
38 )
39 subparsers = parser.add_subparsers()
40
41 # Global configuration flags
42 parser.add_argument("--debug", action="store_true",
43 help=_("Enable debug output"))
44
45 # version
46 parser.add_argument("--version", action="version",
47 version="%%(prog)s %s" % location.__version__)
48
49 # lookup an IP address
50 lookup = subparsers.add_parser("lookup",
51 help=_("Lookup one or multiple IP addresses"),
52 )
53 lookup.add_argument("address", nargs="+")
54 lookup.set_defaults(func=self.handle_lookup)
55
56 return parser.parse_args()
57
58 def run(self):
59 # Parse command line arguments
60 args = self.parse_cli()
61
62 # Callback function must be defined
63 assert args.func, "Callback function not defined"
64
65 # Call function
66 ret = args.func(args)
67
68 # Return with exit code
69 if ret:
70 sys.exit(ret)
71
72 # Otherwise just exit
73 sys.exit(0)
74
75 def handle_lookup(self, ns):
76 ret = 0
77
78 for address in ns.address:
79 try:
80 n = self.db.lookup(address)
81 except ValueError:
82 sys.stderr.write(_("Invalid IP address: %s") % address)
83
84 args = {
85 "address" : address,
86 "network" : n,
87 }
88
89 # Nothing found?
90 if not n:
91 print(_("Nothing found for %(address)s") % args)
92 ret = 1
93 continue
94
95 # Try to retrieve the AS if we have an AS number
96 if n.asn:
97 a = self.db.get_as(n.asn)
98
99 # If we have found an AS we will print it in the message
100 if a:
101 args.update({
102 "as" : a,
103 })
104
105 print(_("%(address)s belongs to %(network)s which is a part of %(as)s") % args)
106 continue
107
108 print(_("%(address)s belongs to %(network)s") % args)
109
110 return ret
111
112
113 def main():
114 # Run the command line interface
115 c = CLI()
116 c.run()
117
118 main()