default="@databasedir@/signing-key.pem", help=_("Public Signing Key"),
)
+ # Show the database version
+ version = subparsers.add_parser("version",
+ help=_("Show database version"))
+ version.set_defaults(func=self.handle_version)
+
# lookup an IP address
lookup = subparsers.add_parser("lookup",
help=_("Lookup one or multiple IP addresses"),
args.family = 0
# Call function
- ret = args.func(db, args)
+ try:
+ ret = args.func(db, args)
+
+ # Catch invalid inputs
+ except ValueError as e:
+ sys.stderr.write("%s\n" % e)
+ ret = 2
# Return with exit code
if ret:
# Otherwise just exit
sys.exit(0)
+ def handle_version(self, db, ns):
+ """
+ Print the version of the database
+ """
+ t = time.strftime(
+ "%a, %d %b %Y %H:%M:%S GMT", time.gmtime(db.created_at),
+ )
+
+ print(t)
+
def handle_lookup(self, db, ns):
ret = 0
# Print country
if network.country_code:
- print(format % (_("Country"), network.country_code))
+ country = db.get_country(network.country_code)
+
+ print(format % (
+ _("Country"),
+ country.name if country else network.country_code),
+ )
# Print AS information
if network.asn:
autonomous_system or "AS%s" % network.asn),
)
+ # Anonymous Proxy
+ if network.has_flag(location.NETWORK_FLAG_ANONYMOUS_PROXY):
+ print(format % (
+ _("Anonymous Proxy"), _("yes"),
+ ))
+
+ # Satellite Provider
+ if network.has_flag(location.NETWORK_FLAG_SATELLITE_PROVIDER):
+ print(format % (
+ _("Satellite Provider"), _("yes"),
+ ))
+
+ # Anycast
+ if network.has_flag(location.NETWORK_FLAG_ANYCAST):
+ print(format % (
+ _("Anycast"), _("yes"),
+ ))
+
return ret
def handle_dump(self, db, ns):
# Use output file or write to stdout
f = ns.output or sys.stdout
+ # Format everything like this
+ format = "%-24s %s\n"
+
# Write metadata
f.write("#\n# Location Database Export\n#\n")
# Iterate over all ASes
for a in db.ases:
f.write("\n")
- f.write("aut-num: AS%s\n" % a.number)
- f.write("name: %s\n" % a.name)
+ f.write(format % ("aut-num:", "AS%s" % a.number))
+ f.write(format % ("name:", a.name))
+
+ flags = {
+ location.NETWORK_FLAG_ANONYMOUS_PROXY : "is-anonymous-proxy:",
+ location.NETWORK_FLAG_SATELLITE_PROVIDER : "is-satellite-provider:",
+ location.NETWORK_FLAG_ANYCAST : "is-anycast:",
+ }
# Iterate over all networks
for n in db.networks:
f.write("\n")
- f.write("net: %s\n" % n)
+ f.write(format % ("net:", n))
if n.country_code:
- f.write("country: %s\n" % n.country_code)
+ f.write(format % ("country:", n.country_code))
if n.asn:
- f.write("autnum: %s\n" % n.asn)
+ f.write(format % ("aut-num:", n.asn))
+
+ # Print all flags
+ for flag in flags:
+ if n.has_flag(flag):
+ f.write(format % (flags[flag], "yes"))
def handle_get_as(self, db, ns):
"""
if ns.anycast:
flags |= location.NETWORK_FLAG_ANYCAST
+ if not flags:
+ raise ValueError(_("You must at least pass one flag"))
+
with self.__get_output_formatter(ns) as f:
for n in db.search_networks(flags=flags, family=ns.family):
f.network(n)