Basic Examples
So, you want to just get a few basic examples to get started? That makes sense, and hopefully I can provide some basic but meaningful examples to show off how simple it is to work with Nmapr.
Object Block
If you want to create a Scanner
object, you scan use the Nmapr.scan
method with the :object
argument to ultimately return an object which you could run .to_cmd
on to get the command-line representation of the nmap command.
Nmapr.scan :object do
# code
end
Command Block
If you want to skip the whole :object
thing entirely, you may just be purely interested in getting the command-line representation of the nmap command to run. This requires you to provide the :cmd
symbol to help signify you want .to_cmd
to run under the hood for you.
Nmapr.scan :cmd do
# code
end
Run Block
If you want to go right ahead and run the scan in your block ( between do
and end
) then you would want to use the :run
argument to run that command. open3
is used under the hood and the stdout, stderr and status of the nmap command is returned within a Hash ( key: value pairing ) that can be captured.
Nmapr.scan :run do
# code
end
Capturing Results
If you run any of the previous three examples, you may want to capture the result or do something with the result. The Nmapr.scan
method actually does return something after all.
Object Block
If you want to create a Scanner
object, you scan use the Nmapr.scan
method with the :object
argument to ultimately return an object which you could run .to_cmd
on to get the command-line representation of the nmap command.
scanner = Nmapr.scan :object do
targets ["192.168.0.2", "192.168.0.3"]
ports :common
end
scanner.is_a? Nmapr::Scanner
# => true
scanner.to_cmd
# => "nmap 192.168.0.2,192.168.0.3 -p 1-1024"
# capture the command a string
cmd = scanner.to_cmd
Command Block
If you want to skip the whole :object
thing entirely, you may just be purely interested in getting the command-line representation of the nmap command to run. This requires you to provide the :cmd
symbol to help signify you want .to_cmd
to run under the hood for you.
cmd = Nmapr.scan :cmd do
targets "192.168.0.2-192.168.0.10"
ports :all
end
cmd
# => "nmap 192.168.0.2-192.168.0.10 -p *"
# print it to screen / terminal
puts cmd
# just print without capturing into another object
puts Nmapr.scan :cmd do
targets "192.168.0.2-192.168.0.10"
ports :all
end
# will print to the screen:
# nmap 192.168.0.2-192.168.0.10 -p *
Run Block
If you want to go right ahead and run the scan in your block ( between do
and end
) then you would want to use the :run
argument to run that command. open3
is used under the hood and the stdout, stderr and status of the nmap command is returned within a Hash ( key: value pairing ) that can be captured.
result = Nmapr.scan :run do
target "localhost"
ports 22, 8080
end
puts result[:stdout]
# Starting Nmap 7.40 ( https://nmap.org ) at 2017-07-16 14:37 EDT
# Nmap scan report for localhost (127.0.0.1)
# Host is up (0.00017s latency).
# Other addresses for localhost (not scanned): ::1
# PORT STATE SERVICE
# 22/tcp closed ssh
# 8080/tcp closed http-proxy
# Nmap done: 1 IP address (1 host up) scanned in 0.05 seconds
puts result[:stderr]
# if empty, then just a newline will be printed to the screen
puts result[:status]
# pid 30258 exit 0
result
# {:stdout=>
# "\n" +
# "Starting Nmap 7.40 ( https://nmap.org ) at 2017-07-16 14:37 EDT\n" +
# "Nmap scan report for localhost (127.0.0.1)\n" +
# "Host is up (0.00017s latency).\n" +
# "Other addresses for localhost (not scanned): ::1\n" +
# "PORT STATE SERVICE\n" +
# "22/tcp closed ssh\n" +
# "8080/tcp closed http-proxy\n" +
# "\n" +
# "Nmap done: 1 IP address (1 host up) scanned in 0.05 seconds\n",
# :stderr=>"",
# :status=>#<Process::Status: pid 30258 exit 0>}