return (PyObject*)self;
}
+static PyObject* Database_iterate_all(DatabaseObject* self, enum loc_database_enumerator_mode what) {
+ struct loc_database_enumerator* enumerator;
+
+ int r = loc_database_enumerator_new(&enumerator, self->db, what);
+ if (r) {
+ PyErr_SetFromErrno(PyExc_SystemError);
+ return NULL;
+ }
+
+ PyObject* obj = new_database_enumerator(&DatabaseEnumeratorType, enumerator);
+ loc_database_enumerator_unref(enumerator);
+
+ return obj;
+}
+
+static PyObject* Database_ases(DatabaseObject* self) {
+ return Database_iterate_all(self, LOC_DB_ENUMERATE_ASES);
+}
+
static PyObject* Database_search_as(DatabaseObject* self, PyObject* args) {
const char* string = NULL;
return obj;
}
+static PyObject* Database_networks(DatabaseObject* self) {
+ return Database_iterate_all(self, LOC_DB_ENUMERATE_NETWORKS);
+}
+
static PyObject* Database_search_networks(DatabaseObject* self, PyObject* args, PyObject* kwargs) {
char* kwlist[] = { "country_code", "asn", "flags", "family", NULL };
const char* country_code = NULL;
};
static struct PyGetSetDef Database_getsetters[] = {
+ {
+ "ases",
+ (getter)Database_ases,
+ NULL,
+ NULL,
+ NULL,
+ },
{
"created_at",
(getter)Database_get_created_at,
NULL,
NULL,
},
+ {
+ "networks",
+ (getter)Database_networks,
+ NULL,
+ NULL,
+ NULL,
+ },
{
"vendor",
(getter)Database_get_vendor,
import os
import socket
import sys
+import time
# Load our location module
import location
lookup.add_argument("address", nargs="+")
lookup.set_defaults(func=self.handle_lookup)
+ # Dump the whole database
+ dump = subparsers.add_parser("dump",
+ help=_("Dump the entire database"),
+ )
+ dump.add_argument("output", nargs="?", type=argparse.FileType("w"))
+ dump.set_defaults(func=self.handle_dump)
+
# Get AS
get_as = subparsers.add_parser("get-as",
help=_("Get information about one or multiple Autonomous Systems"),
return ret
+ def handle_dump(self, db, ns):
+ # Use output file or write to stdout
+ f = ns.output or sys.stdout
+
+ # Write metadata
+ f.write("#\n# Location Database Export\n#\n")
+
+ f.write("# Generated: %s\n" % time.strftime(
+ "%a, %d %b %Y %H:%M:%S GMT", time.gmtime(db.created_at),
+ ))
+
+ if db.vendor:
+ f.write("# Vendor: %s\n" % db.vendor)
+
+ if db.license:
+ f.write("# License: %s\n" % db.license)
+
+ f.write("#\n")
+
+ if db.description:
+ for line in db.description.splitlines():
+ f.write("# %s\n" % line)
+
+ f.write("#\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)
+
+ # Iterate over all networks
+ for n in db.networks:
+ f.write("\n")
+ f.write("net: %s\n" % n)
+
+ if n.country_code:
+ f.write("country: %s\n" % n.country_code)
+
+ if n.asn:
+ f.write("autnum: %s\n" % n.asn)
+
def handle_get_as(self, db, ns):
"""
Gets information about Autonomous Systems