== COMMANDS
-'export --directory=DIR [--format=FORMAT] [--family=ipv6|ipv4] [ASN|CC ...]'::
+'export [--directory=DIR] [--format=FORMAT] [--family=ipv6|ipv4] [ASN|CC ...]'::
This command exports the whole database into the given directory.
+
The output can be filtered by only exporting a certain address family, or by passing
+
The output format can be chosen with the '--format' parameter. For possible formats,
please see below.
+ +
+ If the '--directory' option is omitted, the output will be written to stdout which
+ is useful when you want to load any custom exports straight into nftables or ipset.
'get-as ASN [ASN...]'::
This command returns the name of the owning organisation of the Autonomous
import math
import os
import socket
+import sys
+from .i18n import _
import _location
# Initialise logging
"""
Convenience function to open a file
"""
- f = open(filename, cls.mode)
+ if filename:
+ f = open(filename, cls.mode)
+ elif "b" in cls.mode:
+ f = io.BytesIO()
+ else:
+ f = io.StringIO()
return cls(f, *args, **kwargs)
"""
self._write_footer()
- # Close the file
- self.f.close()
+ # Flush all output
+ self.f.flush()
+
+ def print(self):
+ """
+ Prints the entire output line by line
+ """
+ if isinstance(self.f, io.BytesIO):
+ raise TypeError(_("Won't write binary output to stdout"))
+
+ # Go back to the beginning
+ self.f.seek(0)
+
+ # Iterate over everything line by line
+ for line in self.f:
+ sys.stdout.write(line)
class IpsetOutputWriter(OutputWriter):
self.db, self.writer = db, writer
def export(self, directory, families, countries, asns):
+ filename = None
+
for family in families:
log.debug("Exporting family %s" % family)
# Create writers for countries
for country_code in countries:
- filename = self._make_filename(
- directory, prefix=country_code, suffix=self.writer.suffix, family=family,
- )
+ if directory:
+ filename = self._make_filename(
+ directory,
+ prefix=country_code,
+ suffix=self.writer.suffix,
+ family=family,
+ )
- writers[country_code] = self.writer.open(filename, family, prefix="%s" % country_code)
+ writers[country_code] = self.writer.open(filename, family, prefix=country_code)
# Create writers for ASNs
for asn in asns:
- filename = self._make_filename(
- directory, "AS%s" % asn, suffix=self.writer.suffix, family=family,
- )
+ if directory:
+ filename = self._make_filename(
+ directory,
+ prefix="AS%s" % asn,
+ suffix=self.writer.suffix,
+ family=family,
+ )
writers[asn] = self.writer.open(filename, family, prefix="AS%s" % asn)
for writer in writers.values():
writer.finish()
+ # Print to stdout
+ if not directory:
+ for writer in writers.values():
+ writer.print()
+
def _make_filename(self, directory, prefix, suffix, family):
filename = "%s.%s%s" % (
prefix, suffix, "6" if family == socket.AF_INET6 else "4"
)
export.add_argument("--format", help=_("Output format"),
choices=location.export.formats.keys(), default="list")
- export.add_argument("--directory", help=_("Output directory"), required=True)
+ export.add_argument("--directory", help=_("Output directory"))
export.add_argument("--family",
help=_("Specify address family"), choices=("ipv6", "ipv4"),
)