Search:

Search all manuals
Search this manual
Manual
Membase and Python Development Tutorial
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
Membase and Python Development Tutorial
Chapter Sections
Chapters

10. Setting Up Network Scanner

First we'll get the network scanning portion of the Python script up and running. The following code does a basic scan for a specific set of ports on a given host and prints some of the results.

import nmap

def scan(host='127.0.0.1',range='22-80'):
  nm = nmap.PortScanner()
  nm.scan(host, range)
  return nm

nm = scan()

print "Hosts scanned: " + str(nm.all_hosts())
for host in nm.all_hosts():
  print "Host state: " + str(host) + ": " + nm[host].state()
  print "Protocols checked: " + str(nm[host].all_protocols())
  for proto in nm[host].all_protocols():
    print "Ports checked: " + str(nm[host][proto].keys())
    for port in nm[host][proto].keys():
      print "Port status: " + str(port) + ": " + str(nm[host][proto][port])

As you can see there are three different loops. In the example only a single host is checked and only tcp ports are considered and no services were found, as shown in the output below.

$ python nmap-tutorial-mini.py
Hosts scanned: [u'127.0.0.1']
Host state: 127.0.0.1: up
Protocols checked: []

After turning on the ssh service on the host, you can see the results of the script change:

$ python nmap-tutorial-mini.py
Hosts scanned: [u'127.0.0.1']
Host state: 127.0.0.1: up
Protocols checked: [u'tcp']
Ports checked: [22]
Port status: 22: {'state': u'open', 'reason': u'syn-ack', 'name': u'ssh'}

With the above information we have a variety of information tidbits to use in our logging application. You can download the script from: nmap-tutorial-mini.py