Skip to content

Instantly share code, notes, and snippets.

@flyte
Created January 19, 2016 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flyte/7d3936eb72c3570618d7 to your computer and use it in GitHub Desktop.
Save flyte/7d3936eb72c3570618d7 to your computer and use it in GitHub Desktop.
Port scan a host and save the results to a CouchDB document.
from datetime import datetime
from collections import OrderedDict
import couchdbkit as cdb
import argparse
import nmap
import sys
db = None
p = argparse.ArgumentParser()
args = OrderedDict([
("host", {}),
("--db_uri", {"default": "http://127.0.0.1:5984"})
])
for key, params in args.items():
p.add_argument(key, **params)
class HostScan(cdb.Document):
datetime = cdb.DateTimeProperty()
scan = cdb.DictProperty()
if __name__ == "__main__":
args = p.parse_args()
try:
print "Connecting to DB.."
s = cdb.Server(uri=args.db_uri)
print "Getting or creating 'snoopback' database.."
db = s.get_or_create_db("snoopback")
nm = nmap.PortScanner()
print "Scanning %s.." % args.host
nm.scan(args.host, arguments="-sV")
print "Saving results to db.."
HostScan.set_db(db)
scan = HostScan(
datetime=datetime.now(),
scan=nm[args.host]
)
scan.save()
except Exception, e:
print "Exception: %s" % e
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment