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