netmap

A set of wrapper classes for programmatically accessing the command line tool nmap.

Usage no npm install needed!

<script type="module">
  import netmap from 'https://cdn.skypack.dev/netmap';
</script>

README

NetMap

A wrapper for programmatically accessing the command line tool Nmap. You must have Nmap installed on your device, but as long as it is, anything described below will work.

How to Use

The import exposes the classes Subnet and Host. Every method that yields data returns a Promise.

Subnet

This is for scanning all IPs in a subnet.

Interface

  • constructor(addr[, options = {bits = 24, outputFile = null}])

  • setIP(addr): Sets the start IP address of the range. This method is recommended for setting this property, as it will verify that the new address is in a valid IPv4 format.

  • setBits(bits): Sets the number of bits for the IP range. This method is recommended for setting this property, as it will verify that the new value is valid.

  • getHosts([options = {outputFile = null}]): Finds all hosts in the subnet without checking any ports.

  • scanForPorts(ports[, options = {outputFile = null, version = false, udp = false}]): Scans all "up" hosts in the subnet and returns the hosts' status for the input ports. The ports argument can be a single port or an array. The outputFile option may be used to specify an override output file to use instead of the output file specified in the constructor. If no output file is specified in either the constructor OR this method, the data will not be written to any file. The version option will attempt to perform version detection, and the udp option will attempt to form UDP connections.

  • scanForOpenPorts(ports[, options = {outputFile = null, all = false, version = false, udp = false}]): Scans all "up" hosts in the subnet and returns only the hosts with the input ports open. The ports argument can be a single port or an array. If the all option is set to true, only the hosts with all the input ports open will be returned.

Data Format

The general format for the responses from the scanForPorts and scanForOpenPorts methods is

[
  {
    "ip": "x.x.x.x",
    "name": "some-device",
    "status": "up",
    "ports": [
      {
        "port": 515,
        "open": true,
        "protocol": "tcp",
        "serviceName": "printer",
        "portDetails": ""
      }
      ...
    ]
  },
  ...
]

For getHosts, the format is

[
  {
    "ip": "x.x.x.x",
    "name": "some-device"
  },
  ...
]

Example

const { Subnet } = require('netmap');

const subnet = new Subnet('192.168.86.0', { outputFile: './data.json' });

subnet.scanForOpenPorts([515, 9100]) // `nmap -sV 192.168.86.0/24 -p 515,9100 -oG - | grep -e open`
    .then(d => ...)
    .catch(console.error);

Host

This is for scanning a single host.

Interface

  • constructor(addr[options = {outputFile = null}])

  • scan([options = {outputFile = null, version = false, udp = false, flags = []}]): Returns open ports on the host in the same format as the ports property of a single host from the subnet scans. The flags option is an array of flags to be appended onto the Nmap call, each separated by a space.

  • scanCommon([options = {outputFile = null, flags = []}]): Checks the 100 most common ports and returns open ports on the host in the same format as the ports property of a single host from the subnet scans.

  • scanPorts(ports[, options = {outputFile = null, version = false, udp = false, flags = []}]): Returns port data for each input port. The ports argument can be a single port or an array.

Example

const { Host } = require('netmap');

const host = new Host('192.168.86.54');

host.scanPorts([515, 9100]) // `nmap -sT 192.168.86.54 -p 515,9100`
  .then(d => ...)
  .catch(console.error);