]>
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): | |
5118a4b8 MT |
36 | def parse_cli(self): |
37 | parser = argparse.ArgumentParser( | |
38 | description=_("Location Database Command Line Interface"), | |
39 | ) | |
40 | subparsers = parser.add_subparsers() | |
41 | ||
42 | # Global configuration flags | |
43 | parser.add_argument("--debug", action="store_true", | |
44 | help=_("Enable debug output")) | |
45 | ||
ddb184be MT |
46 | # version |
47 | parser.add_argument("--version", action="version", | |
48 | version="%%(prog)s %s" % location.__version__) | |
49 | ||
2538ed9a MT |
50 | # database |
51 | parser.add_argument("--database", "-d", | |
52 | default="@databasedir@/database.db", help=_("Path to database"), | |
53 | ) | |
54 | ||
5118a4b8 MT |
55 | # lookup an IP address |
56 | lookup = subparsers.add_parser("lookup", | |
57 | help=_("Lookup one or multiple IP addresses"), | |
58 | ) | |
59 | lookup.add_argument("address", nargs="+") | |
60 | lookup.set_defaults(func=self.handle_lookup) | |
61 | ||
fadc1af0 MT |
62 | # Get AS |
63 | get_as = subparsers.add_parser("get-as", | |
64 | help=_("Get information about one or multiple Autonomous Systems"), | |
65 | ) | |
66 | get_as.add_argument("asn", nargs="+") | |
67 | get_as.set_defaults(func=self.handle_get_as) | |
68 | ||
da3e360e MT |
69 | # Search for AS |
70 | search_as = subparsers.add_parser("search-as", | |
71 | help=_("Search for Autonomous Systems that match the string"), | |
72 | ) | |
73 | search_as.add_argument("query", nargs=1) | |
74 | search_as.set_defaults(func=self.handle_search_as) | |
75 | ||
5118a4b8 MT |
76 | return parser.parse_args() |
77 | ||
78 | def run(self): | |
79 | # Parse command line arguments | |
80 | args = self.parse_cli() | |
81 | ||
82 | # Callback function must be defined | |
83 | assert args.func, "Callback function not defined" | |
84 | ||
2538ed9a MT |
85 | # Open database |
86 | try: | |
87 | db = location.Database(args.database) | |
88 | except FileNotFoundError as e: | |
89 | sys.stderr.write("location-query: Could not open database %s: %s\n" \ | |
90 | % (args.database, e)) | |
91 | sys.exit(1) | |
92 | ||
5118a4b8 | 93 | # Call function |
2538ed9a | 94 | ret = args.func(db, args) |
5118a4b8 MT |
95 | |
96 | # Return with exit code | |
97 | if ret: | |
98 | sys.exit(ret) | |
99 | ||
100 | # Otherwise just exit | |
101 | sys.exit(0) | |
102 | ||
2538ed9a | 103 | def handle_lookup(self, db, ns): |
5118a4b8 MT |
104 | ret = 0 |
105 | ||
106 | for address in ns.address: | |
107 | try: | |
2538ed9a | 108 | n = db.lookup(address) |
5118a4b8 | 109 | except ValueError: |
9f2f5d13 | 110 | print(_("Invalid IP address: %s") % address, file=sys.stderr) |
5118a4b8 MT |
111 | |
112 | args = { | |
113 | "address" : address, | |
114 | "network" : n, | |
115 | } | |
116 | ||
117 | # Nothing found? | |
118 | if not n: | |
9f2f5d13 | 119 | print(_("Nothing found for %(address)s") % args, file=sys.stderr) |
5118a4b8 MT |
120 | ret = 1 |
121 | continue | |
122 | ||
123 | # Try to retrieve the AS if we have an AS number | |
124 | if n.asn: | |
2538ed9a | 125 | a = db.get_as(n.asn) |
5118a4b8 MT |
126 | |
127 | # If we have found an AS we will print it in the message | |
128 | if a: | |
129 | args.update({ | |
130 | "as" : a, | |
131 | }) | |
132 | ||
133 | print(_("%(address)s belongs to %(network)s which is a part of %(as)s") % args) | |
134 | continue | |
135 | ||
136 | print(_("%(address)s belongs to %(network)s") % args) | |
137 | ||
138 | return ret | |
139 | ||
2538ed9a | 140 | def handle_get_as(self, db, ns): |
fadc1af0 MT |
141 | """ |
142 | Gets information about Autonomous Systems | |
143 | """ | |
144 | ret = 0 | |
145 | ||
146 | for asn in ns.asn: | |
147 | try: | |
148 | asn = int(asn) | |
149 | except ValueError: | |
9f2f5d13 | 150 | print(_("Invalid ASN: %s") % asn, file=sys.stderr) |
fadc1af0 MT |
151 | ret = 1 |
152 | continue | |
153 | ||
154 | # Fetch AS from database | |
2538ed9a | 155 | a = db.get_as(asn) |
fadc1af0 MT |
156 | |
157 | # Nothing found | |
158 | if not a: | |
9f2f5d13 | 159 | print(_("Could not find AS%s") % asn, file=sys.stderr) |
fadc1af0 MT |
160 | ret = 1 |
161 | continue | |
162 | ||
163 | print(_("AS%(asn)s belongs to %(name)s") % { "asn" : a.number, "name" : a.name }) | |
164 | ||
165 | return ret | |
5118a4b8 | 166 | |
2538ed9a | 167 | def handle_search_as(self, db, ns): |
da3e360e MT |
168 | for query in ns.query: |
169 | # Print all matches ASes | |
2538ed9a | 170 | for a in db.search_as(query): |
da3e360e MT |
171 | print(a) |
172 | ||
5118a4b8 MT |
173 | def main(): |
174 | # Run the command line interface | |
175 | c = CLI() | |
176 | c.run() | |
177 | ||
178 | main() |