Project

ruby-nmap

0.16
A long-lived project that still receives updates
A Ruby API to nmap, the exploration tool and security / port scanner. Allows automating nmap and parsing nmap XML files.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0

Runtime

 Project Readme

ruby-nmap

CI Code Climate Gem Version

Description

A Ruby API to nmap, the exploration tool and security / port scanner. Allows automating nmap and parsing nmap XML files.

Features

  • Provides a Ruby API for automating nmap.
  • Provides a Parser for enumerating nmap XML scan files.
  • Supports the full Nmap XML DTD.

Examples

Run Nmap from Ruby:

require 'nmap/command'

Nmap::Command.run do |nmap|
  nmap.connect_scan   = true
  nmap.service_scan   = true
  nmap.output_xml     = 'scan.xml'
  nmap.verbose        = true

  nmap.ports   = [20, 21, 22, 23, 25, 80, 110, 443, 512, 522, 8080, 1080]
  nmap.targets = '192.168.1.*'
end

Run sudo nmap from Ruby:

require 'nmap/command'

Nmap::Command.sudo do |nmap|
  nmap.syn_scan       = true
  nmap.os_fingerprint = true
  nmap.service_scan   = true
  nmap.output_xml     = 'scan.xml'
  nmap.verbose        = true

  nmap.ports   = [20, 21, 22, 23, 25, 80, 110, 443, 512, 522, 8080, 1080]
  nmap.targets = '192.168.1.*'
end

Parse Nmap XML scan files:

require 'nmap/xml'

Nmap::XML.open('scan.xml') do |xml|
  xml.each_host do |host|
    puts "[#{host.ip}]"

    host.each_port do |port|
      puts "  #{port.number}/#{port.protocol}\t#{port.state}\t#{port.service}"
    end
  end
end

Print NSE script output from an XML scan file:

require 'nmap/xml'

Nmap::XML.open('nse.xml') do |xml|
  xml.each_host do |host|
    puts "[#{host.ip}]"

    host.scripts.each do |name,output|
      output.each_line { |line| puts "  #{line}" }
    end

    host.each_port do |port|
      puts "  [#{port.number}/#{port.protocol}]"

      port.scripts.each do |id,script|
        puts "    [#{id}]"

        script.output.each_line { |line| puts "      #{line}" }
      end
    end
  end
end

Requirements

Install

  • Debian / Ubuntu:
$ sudo apt install nmap
  • Fedora / RedHat:
$ sudo dnf install nmap
  • Homebrew:
$ brew install nmap
$ gem install ruby-nmap

License

See {file:LICENSE.txt} for license information.