]>
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 | ||
fadc1af0 MT |
61 | # Get AS |
62 | get_as = subparsers.add_parser("get-as", | |
63 | help=_("Get information about one or multiple Autonomous Systems"), | |
64 | ) | |
65 | get_as.add_argument("asn", nargs="+") | |
66 | get_as.set_defaults(func=self.handle_get_as) | |
67 | ||
5118a4b8 MT |
68 | return parser.parse_args() |
69 | ||
70 | def run(self): | |
71 | # Parse command line arguments | |
72 | args = self.parse_cli() | |
73 | ||
74 | # Callback function must be defined | |
75 | assert args.func, "Callback function not defined" | |
76 | ||
77 | # Call function | |
78 | ret = args.func(args) | |
79 | ||
80 | # Return with exit code | |
81 | if ret: | |
82 | sys.exit(ret) | |
83 | ||
84 | # Otherwise just exit | |
85 | sys.exit(0) | |
86 | ||
87 | def handle_lookup(self, ns): | |
88 | ret = 0 | |
89 | ||
90 | for address in ns.address: | |
91 | try: | |
92 | n = self.db.lookup(address) | |
93 | except ValueError: | |
94 | sys.stderr.write(_("Invalid IP address: %s") % address) | |
95 | ||
96 | args = { | |
97 | "address" : address, | |
98 | "network" : n, | |
99 | } | |
100 | ||
101 | # Nothing found? | |
102 | if not n: | |
103 | print(_("Nothing found for %(address)s") % args) | |
104 | ret = 1 | |
105 | continue | |
106 | ||
107 | # Try to retrieve the AS if we have an AS number | |
108 | if n.asn: | |
109 | a = self.db.get_as(n.asn) | |
110 | ||
111 | # If we have found an AS we will print it in the message | |
112 | if a: | |
113 | args.update({ | |
114 | "as" : a, | |
115 | }) | |
116 | ||
117 | print(_("%(address)s belongs to %(network)s which is a part of %(as)s") % args) | |
118 | continue | |
119 | ||
120 | print(_("%(address)s belongs to %(network)s") % args) | |
121 | ||
122 | return ret | |
123 | ||
fadc1af0 MT |
124 | def handle_get_as(self, ns): |
125 | """ | |
126 | Gets information about Autonomous Systems | |
127 | """ | |
128 | ret = 0 | |
129 | ||
130 | for asn in ns.asn: | |
131 | try: | |
132 | asn = int(asn) | |
133 | except ValueError: | |
134 | sys.stderr.write("Invalid ASN: %s" %asn) | |
135 | ret = 1 | |
136 | continue | |
137 | ||
138 | # Fetch AS from database | |
139 | a = self.db.get_as(asn) | |
140 | ||
141 | # Nothing found | |
142 | if not a: | |
143 | print(_("Could not find AS%s") % asn) | |
144 | ret = 1 | |
145 | continue | |
146 | ||
147 | print(_("AS%(asn)s belongs to %(name)s") % { "asn" : a.number, "name" : a.name }) | |
148 | ||
149 | return ret | |
5118a4b8 MT |
150 | |
151 | def main(): | |
152 | # Run the command line interface | |
153 | c = CLI() | |
154 | c.run() | |
155 | ||
156 | main() |