Package org.nmap4j
Class Nmap4j
java.lang.Object
org.nmap4j.Nmap4j
- All Implemented Interfaces:
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Add the appropriate flags to your scan.void
excludeHosts
(String hosts) Add a list of space delimited hosts to exclude.void
execute()
Executes the nmap scan with the parameters set.Use this method to get the raw results of the execution.Returns the raw output of the execution.This method returns an object tree representing the XML nodes.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.void
includeHosts
(String hosts) Add a list of space delimited hosts that you want to scan.
-
Constructor Details
-
Nmap4j
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
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 interfaceINmap4j
- Throws:
NMapInitializationException
NMapExecutionException
-
addFlags
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. -
includeHosts
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 interfaceINmap4j
- Parameters:
hosts
-
-
excludeHosts
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 interfaceINmap4j
- Parameters:
hosts
-
-
getOutput
Returns the raw output of the execution. -
getResult
This method returns an object tree representing the XML nodes. -
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. -
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 interfaceINmap4j
- Returns:
-