def update_share(share, cursor): print "Updating hash for %(proto)s://%(host)s:%(port)s" \ % {'proto': share['protocol'], 'port': str(share['port']), 'host': share['hostname']} try: hash = hashlib.md5() for line in open(share_save_path(share['protocol'], share['hostname'], share['port']), "rt"): hash.update(line) except: cursor.execute("UPDATE trees SET hash='' WHERE tree_id=%(i)s", {'i': share['tree_id']}) return cursor.execute("UPDATE trees SET hash=%(h)s WHERE tree_id=%(i)s", {'h': hash.hexdigest(), 'i': share['tree_id']}) if __name__ == "__main__": try: db = connectdb() except: print "Unable to connect to the database, exiting." sys.exit() shares = db.cursor() shares.execute(""" SELECT tree_id, protocol, hostname, port FROM shares """) for share in shares.fetchall(): update_share(share, shares) db.commit()
if len(sys.argv) < 2 or len(sys.argv) > 3 or 'help' in sys.argv or '-h' in sys.argv: print 'Usage %s [help|confighelp|showengines|showscantypes|shownetworks|showconfig [networkname]|runall|runnet networkname]' % sys.argv[0] sys.exit() if 'confighelp' in sys.argv: print ParseConfig.__doc__ sys.exit() lookup_engines = dict([(cl.__name__, cl) for cl in Lookup.__subclasses__()]) if 'showengines' in sys.argv: print 'Known lookup engines:' for eng in sorted(lookup_engines.keys()): print '%s\t%s' % (eng, lookup_engines[eng].__doc__) sys.exit() try: db = connectdb("lookup") except: print "I am unable to connect to the database, exiting." sys.exit(1) db.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) scantypes = get_scantypes(db) if 'showscantypes' in sys.argv: for proto in scantypes.iterkeys(): print 'Protocol "%s" scantypes (detection sequence %s):' % (proto, scantypes[proto].discovery) for (i, cmd) in scantypes[proto].iteritems(): print '%s\t%s' % (i, cmd) print '' sys.exit() if 'shownetworks' in sys.argv:
(hoststr, str(deleted), str(added), str(modified), scan_time, datetime.datetime.now() - start)) else: log("Scanning %s succeded. Database updated in non-patching mode (scan time %s, update time %s).", (hoststr, scan_time, datetime.datetime.now() - start)) def create_save_dir(): if os.path.isdir(shares_save_dir): return if os.path.isfile(shares_save_dir): raise NameError("%s should be a directory, not a file\n" % (shares_save_dir,)) log("%s directory doesn't exist, creating" % (shares_save_dir,)) os.mkdir(shares_save_dir) if __name__ == "__main__": try: db = connectdb("spider") except: log("Unable to connect to the database, exiting.") sys.exit() create_save_dir() shares = db.cursor() while True: db.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) shares.execute(""" SELECT share_id, tree_id, shares.protocol, hostname, port, scan_command FROM shares LEFT JOIN scantypes USING (scantype_id) WHERE state = 'online' AND (next_scan IS NULL OR next_scan < now()) ORDER BY next_scan NULLS FIRST LIMIT 1 """) if shares.rowcount == 0:
def getquoted(self): return str(self) class None_adapter(object): def __init__(self, obj): pass def __str__(self): return 'NULL' def getquoted(self): return 'NULL' psycopg2.extensions.register_adapter(list, list_adapter) psycopg2.extensions.register_adapter(type(None), None_adapter) if __name__ == "__main__": try: db = connectdb("pinger") except: print "I am unable to connect to the database, exiting." sys.exit() db.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) nscache = dns_cache() log("Starting pinger...") shares = 0 online = 0 start = datetime.datetime.now() for proto in default_ports.iteritems(): on, off = update_shares_state(db, "protocol='%s' AND port=0" % proto[0], proto[1]) shares += on + off