def route(args): db = get_db(args) quarantine = ip.ip_network(args.quarantine) routes = set([]) for host in db.hosts.find({}, {"v4routes":1}): for route in host.get("v4routes", []): network = ip.ip_network(route["network"]) if network.network_address in quarantine: routes.add(network) routes = list(routes) routes.sort() ## remove supernets... this is a bit weird... def supernets(n): s = n.supernet() if s == n: return yield s for s in supernets(s): yield s ## hella ineffient prune = set([]) for r in routes: ## except interface routes... if r.prefixlen == 32: continue for s in supernets(r): prune.add(s) for p in prune: if p in routes: routes.remove(p) addrs = set([]) for addr in db.v4addr.find(): addrs.add(ip.ip_address(addr["address"])) addrs = list(addrs) addrs.sort() for r in routes: print r for addr in addrs: if addr in r: hinfo = gethostbyv4addr(db, addr) for host in hinfo: print "\t%-16s%s" % (addr, host["name"])
def annotate(args): db = get_db(args) hostname = args.hostname[0] authinfo = db.authinfo.find_one({"name": hostname}) if authinfo is None or authinfo.get("login") is None: log.error("Do not have a username for this host, interrogate it first, please") return hinfo = gethostbyv4addr(db, hostname) if hinfo is None: hinfo = gethostbymacaddr(db, hostname) if hinfo is None: log.error("Interrogate this host first, please") return elif len(hinfo) == 1: hinfo = hinfo[0] elif len(hinfo) > 1: log.error("Several matching hosts found:") for h in hinfo: log.error(" %s" % h["ident"]) log.error("Try using the identifier instead.") annotations = db.annotations.find_one({ "ident": hinfo["ident"]}) if annotations is None: annotations = { "ident": hinfo["ident"] } if args.json: value = json.loads(args.value[0]) else: value = args.value[0] if args.key[0] in annotations and not value: del annotations[args.key[0]] else: annotations[args.key[0]] = value db.annotations.save(annotations) merge_host(db, hinfo["ident"])
def config(args): db = get_db(args) hostname = args.hostname[0] authinfo = db.authinfo.find_one({"name": hostname}) if authinfo is None or authinfo.get("login") is None: log.error("Do not have a username for this host, interrogate it first, please") return hinfo = gethostbyv4addr(db, hostname) if hinfo is None: hinfo = gethostbymacaddr(db, hostname) if hinfo is None: log.error("Interrogate this host first, please") return elif len(hinfo) == 1: hinfo = hinfo[0] elif len(hinfo) > 1: log.error("Several matching hosts found:") for h in hinfo: log.error(" %s" % h["ident"]) log.error("Try using the identifier instead.") import os, os.path def trymkdir(d): try: os.stat(d) except OSError: os.makedirs(d) if isinstance(args.path, list): cfgpath = args.path[0] else: cfgpath = args.path trymkdir(cfgpath) ### XXXX should be in the database! dontuse = [ip.ip_address(a) for a in ("10.10.10.10", "10.127.127.10", "10.123.123.123")] mgmtnet = ip.ip_network("10.0.0.0/8") def getaddr(hinfo): for iface in hinfo.get("interfaces", []): for ifa in [ip.ip_interface(a) for a in iface.get("v4addr", [])]: if ifa.ip in mgmtnet and ifa.ip not in dontuse: return ifa.ip from rlogin import Rcmd import pexpect if hinfo.get("flavour") is None: log.warning("[%(name)s] Couldn't determine which OS variant to use for backing up, sorry." % hinfo) elif hinfo["flavour"].lower() == "openwrt" or hinfo["flavour"] == "NanoBSD": cfgpath = os.path.join(cfgpath, hinfo["ident"]) trymkdir(cfgpath) c = Rcmd(host=getaddr(hinfo), path=cfgpath, username=authinfo["login"][0][0], password=authinfo["login"][0][1], cmd="sh -c 'ssh %(username)s@%(host)s tar -cf - /etc | (cd %(path)s; tar -xf -)'", timeout=300) try: c.run() except pexpect.EOF: pass elif hinfo["flavour"] == "AirOS": cfgpath = os.path.join(cfgpath, hinfo["ident"]) + ".cfg" c = Rcmd(host=getaddr(hinfo), path=cfgpath, username=authinfo["login"][0][0], password=authinfo["login"][0][1], cmd="scp -q -r %(username)s@%(host)s:/tmp/system.cfg %(path)s", timeout=300) try: c.run() except pexpect.EOF: pass else: log.warning("[%(name)s] don't know how to back up the config of a %(flavour)s host" % hinfo) return log.info("[%(name)s] done." % hinfo)
def hprint(db, host): print "=" * 80 print "%(name)s" % host print "=" * 80 print if host.get("sysdesc") is not None: print "\t%(sysdesc)s" % host if host.get("model") is not None: print "\t%(model)s" % host host.setdefault("opsys", "Unknown OS") host.setdefault("osver", "Unknown Version") host.setdefault("flavour", "Generic") host.setdefault("release", "") host.setdefault("machine", "Unknown Architecture") print "\t%(flavour)s %(release)s %(opsys)s %(osver)s %(machine)s" % host host.setdefault("build", "") print "\t%(build)s" % host print print "Interfaces:" ifaces = host.get("interfaces", []) ifaces.sort(lambda x,y: cmp(x["ifindex"], y["ifindex"])) for iface in ifaces: iface.setdefault("mac", "") print " %(ifindex)s\t%(name)s" % iface if "ssid" in iface: print "\t Wireless:\n\t\t%(ssid)s %(freq)s" % iface addrs = iface.get("v4addr") if addrs is not None: print "\t IPv4 Addresses:\n\t\t" + " ".join(addrs) neighbours = iface.get("arp") if neighbours is not None: neighbours.sort(lambda x,y: cmp(ip.ip_address(x["v4addr"]), ip.ip_address(y["v4addr"]))) print "\t ARP Table:" for neighbour in neighbours: n = gethostbymacaddr(db, neighbour["mac"]) if n is not None: neighbour["name"] = " (%(name)s)" % n else: neighbour["name"] = "" print "\t\t%(mac)s - %(v4addr)-16s%(name)s" % neighbour bridges = host.get("bridges") if bridges is not None: print print "Ethernet bridges:" for bridge in bridges: print " %(name)s" % bridge print "\tMembers: " + " ".join(bridge.get("members", [])) router = host.get("router") if router is not None: ospf = router.get("ospf") if ospf is not None: print print "OSPF Neighbours:" neighbours = ospf["neighbours"] neighbours.sort(lambda x,y: cmp(ip.ip_address(x["routerid"]),ip.ip_address(y["routerid"]))) for neighbour in neighbours: ns = gethostbyv4addr(db, neighbour["v4addr"]) if ns is not None: neighbour["name"] = " (%s)" % ",".join(n["name"] for n in ns) else: neighbour["name"] = "" print "\t%(ifname)8s %(routerid)-16s - %(v4addr)-16s%(name)s" % neighbour