Package org.nmap4j

Class Nmap4j

java.lang.Object
org.nmap4j.Nmap4j
All Implemented Interfaces:
INmap4j

public class Nmap4j extends Object implements INmap4j
This is the simplified way to execute and parse Nmap output. This is the easiest way to run Nmap from this API. Use of this class requires a little more knowledge of Nmap's flags in order to work.

Here is an example of how to use this class: Nmap4j nmap4j = new Nmap4j( "/usr/local" ) ; nmap4j.includeHosts( "192.168.1.1-255" ) ; nmap4j.excludeHosts( "192.168.1.110" ) ; nmap4j.addFlags( "-T3 -oX - -O -sV" ) ; nmap4j.execute() ; if( !nma4j.hasError() ) { NMapRun nmapRun = nmap4j.getResults() ; } else { System.out.println( nmap4j.getExecutionResults().getErrors() ) ; }

This block would need a try/catch because the execute() method throws two different exceptions.

  • Constructor Details

    • Nmap4j

      public Nmap4j(String path)
      Constructs this object with the path specified. This path needs to be the path to your Nmap binary. On many systems this will be something like "/usr/local". Additionally, this will also be the path to the data dir required by Nmap.
      Parameters:
      path -
  • Method Details

    • execute

      public void execute() throws NMapInitializationException, NMapExecutionException
      Executes the nmap scan with the parameters set. You should have called addFlags() with appropriate Nmap flags prior to executing the scan.
      Specified by:
      execute in interface INmap4j
      Throws:
      NMapInitializationException
      NMapExecutionException
    • addFlags

      public void addFlags(String flagSet)
      Add the appropriate flags to your scan. Call this method with all the flags you will want. For example, if you want to scan for hosts, OS information and service information you would pass "-sV -O -T4". This method will append "-oX -" if you did not supply it.
      Specified by:
      addFlags in interface INmap4j
      Parameters:
      flagSet -
    • includeHosts

      public void includeHosts(String hosts)
      Add a list of space delimited hosts that you want to scan. This list conforms to the requirements that Nmap sets forth.
      Specified by:
      includeHosts in interface INmap4j
      Parameters:
      hosts -
    • excludeHosts

      public void excludeHosts(String hosts)
      Add a list of space delimited hosts to exclude. Usually this is used when you specify a large included host list. This allows you specify broad ranges host addresses and exclude some hosts within that range.
      Specified by:
      excludeHosts in interface INmap4j
      Parameters:
      hosts -
    • getOutput

      public String getOutput()
      Returns the raw output of the execution.
      Specified by:
      getOutput in interface INmap4j
      Returns:
    • getResult

      public NMapRun getResult()
      This method returns an object tree representing the XML nodes.
      Specified by:
      getResult in interface INmap4j
      Returns:
    • hasError

      public boolean hasError()
      Checks the output for the word "ERROR" as Nmap will usually produce an error message that starts with ERROR though there are other scenarios. If the call to getResult() fails check the error output.
      Specified by:
      hasError in interface INmap4j
      Returns:
    • getExecutionResults

      public ExecutionResults getExecutionResults()
      Use this method to get the raw results of the execution. The ExecutionResults contains the raw output, the errors and the command that was executed.
      Specified by:
      getExecutionResults in interface INmap4j
      Returns: