hostscan

NMAP Host Generator
git clone git@jamesshield.xyz:repos/hostscan.git
Log | Files | Refs | README | LICENSE

commit 3c30427e0352c5b4e43089b7892a399b00df9f86
parent 6096bbe513becb30fe99604629ea7f8bcf920fa1
Author: Gary Berger <gberger@brocade.com>
Date:   Mon, 22 May 2017 11:38:05 -0400

Updated README

Signed-off-by: Gary Berger <gberger@brocade.com>

Diffstat:
AREADME.md | 126+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mnmap-scan.py | 4++--
2 files changed, 128 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md @@ -0,0 +1,125 @@ +# README + +This is a simple script that performs an nmap scan on a destination address, parses the XML result into a Python Map and can transform the data for use in creating host files, Ansible host manifests or drive an inventory database using MongoDB. + + +We leverage DocOpt for command processing: + + +``` +Usage: nmap-scan [options] <network> + +Options: + --mongo-host MONGO_HOST Mongo Host(default: localhost) + --mongo-port MONGO_PORT Mongo Port(default: 3001) + --database DB Add records to mongo db + --collection COLLECTION Add records to mongo collection + -H, --hosts Print *nix hosts file format [default]. + -R, --raw Print raw collection database + -I, --insert Performs insert instead of in place update + -S, --snmp Make SNMP calls + -A, --ansible Make Ansible inventory + -D, --debug Print debug information + +``` + + +As an example to run a scan on a 24bit subnet you could run: +``` +nmap-scan.py -H 192.168.1.0/24 +``` + +``` +192.168.1.124 192-168-1-124 +192.168.1.113 192-168-1-113 +192.168.1.10 takagi +192.168.1.1 192-168-1-1 +192.168.1.101 192-168-1-101 +192.168.1.100 192-168-1-100 +192.168.1.168 192-168-1-168 +192.168.1.149 192-168-1-149 +192.168.1.115 192-168-1-115 +192.168.1.227 192-168-1-227 +192.168.1.209 192-168-1-209 +192.168.1.217 192-168-1-217 +``` + +Generate an Ansible hosts file + +``` +[hosts] +192-168-1-10 ansible_ssh_user=root ansible_ssh_pass=#password# +192-168-1-1 ansible_ssh_user=root ansible_ssh_pass=#password# +192-168-1-101 ansible_ssh_user=root ansible_ssh_pass=#password# +192-168-1-113 ansible_ssh_user=root ansible_ssh_pass=#password# +192-168-1-168 ansible_ssh_user=root ansible_ssh_pass=#password# +192-168-1-149 ansible_ssh_user=root ansible_ssh_pass=#password# +192-168-1-115 ansible_ssh_user=root ansible_ssh_pass=#password# +192-168-1-227 ansible_ssh_user=root ansible_ssh_pass=#password# +192-168-1-209 ansible_ssh_user=root ansible_ssh_pass=#password# +192-168-1-217 ansible_ssh_user=root ansible_ssh_pass=#password# +``` + + +More detail nmap scan: + +``` + '192-168-1-115': {'_id': '192-168-1-115', + 'ip': '192.168.1.115', + 'mac': None, + 'name': '192-168-1-115', + 'services': [{'portid': '80', + 'product': 'HP Photosmart 7510 series printer http config', + 'protocol': 'tcp', + 'service_name': 'http'}, + {'portid': '139', + 'product': None, + 'protocol': 'tcp', + 'service_name': 'netbios-ssn'}, + {'portid': '443', + 'product': 'HP Photosmart 7510 series printer http config', + 'protocol': 'tcp', + 'service_name': 'http', + 'ssl_cn': None, + 'ssl_orgname': None, + 'ssl_valid': None}], + 'vendor': None}, + '192-168-1-124': {'_id': '192-168-1-124', + 'ip': '192.168.1.124', + 'mac': None, + 'name': '192-168-1-124', + 'vendor': None}, + '192-168-1-149': {'_id': '192-168-1-149', + 'ip': '192.168.1.149', + 'mac': None, + 'name': '192-168-1-149', + 'vendor': None}, + '192-168-1-168': {'_id': '192-168-1-168', + 'ip': '192.168.1.168', + 'mac': None, + 'name': '192-168-1-168', + 'services': [{'portid': '111', + 'product': None, + 'protocol': 'tcp', + 'service_name': 'rpcbind'}], + 'vendor': None}, + '192-168-1-209': {'_id': '192-168-1-209', + 'ip': '192.168.1.209', + 'mac': None, + 'name': '192-168-1-209', + 'services': [{'portid': '111', + 'product': None, + 'protocol': 'tcp', + 'service_name': 'rpcbind'}], + 'vendor': None}, + '192-168-1-217': {'_id': '192-168-1-217', + 'ip': '192.168.1.217', + 'mac': None, + 'name': '192-168-1-217', + 'vendor': None}, + '192-168-1-227': {'_id': '192-168-1-227', + 'ip': '192.168.1.227', + 'mac': None, + 'name': '192-168-1-227', + 'vendor': None}} +```+ \ No newline at end of file diff --git a/nmap-scan.py b/nmap-scan.py @@ -37,7 +37,6 @@ def debug(msg, obj): print "DEBUG: {0} {1}\n".format(msg, pprint(obj)) def run_nmap(net): - print("Running network scan for network {}".format(net)) try: out = subprocess.check_output(["nmap", "-oX", "-" , "-R", "-p", "22-443", "-sV" , net]) except CalledProcessError: @@ -66,7 +65,7 @@ def parsexml(f): if debug: print("Run SNMPGET for {}".format(_ipaddress)) varBind = snmpget(_ipaddress) - hostname = host.find("./hostnames/hostname").get('name') if ET.iselement(host.find("./hostnames/hostname")) else _ipaddress + hostname = host.find("./hostnames/hostname").get('name') if ET.iselement(host.find("./hostnames/hostname")) else _ipaskey # Per Host _host[_ipaskey] = { '_id': _ipaskey, 'name': hostname, 'ip': _ipaddress, 'mac': _macaddress, 'vendor': _vendor} _services = {} @@ -143,6 +142,7 @@ def print_hosts(collection): def print_ansible(collection): + print "[hosts]" for i in collection: print "%s\tansible_ssh_user=%s\tansible_ssh_pass=%s" % (i, 'root', '#password#')