In Java, by calling Runtime , this class can execute other executable programs, return a process after execution, take advantage of the class of process we can get the echo of program execution, so call in Java the principle of the host detection of nmap is very clear. By passing the path to the function Nmap and the commands we need to execute
/** * call Nmap to scan * @param nmapdir nmap path * @param command Execute command * * @return perform echo * */public string getreturndata (String nmapdir,string command) {Process process = null ; Stringbuffer stringbuffer = new stringbuffer ();try {process = Runtime.getruntime (). EXEC (nmapdir + " " + command); SYSTEM.OUT.PRINTLN ("Please wait a moment ... "); Bufferedreader reader = new bufferedreader (New inputstreamreader ( Process.getinputstream (), "UTF-8")); String line = null;while ((Line = reader.readline ()) != null) { Stringbuffer.append (line + "\ n");}} catch (ioexception e) {e.printstacktrace ();} Return stringbuffer.tostring ();}
Test:
NmapTest1 nmapTest1 = new NmapTest1 (); String str = nmaptest1.getreturndata ("D:\\nmap\\nmap.exe", "-ss-p0-a-V-www.zifangsky.cn"); System.out.println (str);
return Result:
Please wait a moment ...
Starting Nmap 7.00 (https://nmap.org) at 2015-11-30 21:00? D1???
nse:loaded scripts for scanning.
Nse:script pre-scanning.
Initiating NSE at 21:00
Completed NSE at 21:00, 0.00s elapsed
Initiating NSE at 21:00
Completed NSE at 21:00, 0.00s elapsed
Initiating Parallel DNS resolution of 1 host. At 21:01
Completed Parallel DNS resolution of 1 host. At 21:01, 0.32s elapsed
Initiating SYN Stealth Scan at 21:01
Scanning www.zifangsky.cn (121.42.81.9) [Ports]
Discovered open port 21/tcp on 121.42.81.9
Completed SYN Stealth Scan at 21:01, 9.01s elapsed (all ports)
Initiating Service scan at 21:01
Scanning 1 service on www.zifangsky.cn (121.42.81.9)
Completed service scan at 21:01, 9.10s elapsed (1 service on 1 host)
Initiating OS Detection (try #1) against www.zifangsky.cn (121.42.81.9)
Initiating Traceroute at 21:01
Completed Traceroute at 21:01, 9.06s elapsed
Initiating Parallel DNS resolution of 1 host. At 21:01
Completed Parallel DNS resolution of 1 host. At 21:01, 16.50s elapsed
Nse:script scanning 121.42.81.9.
Initiating NSE at 21:01
Completed NSE at 21:02, 13.32s elapsed
Initiating NSE at 21:02
Completed NSE at 21:02, 0.00s elapsed
Nmap Scan Report for www.zifangsky.cn (121.42.81.9)
Host is up (0.047s latency).
Not shown:999 filtered ports
PORT State SERVICE VERSION
21/TCP Open FTP vsftpd (before 2.0.8) or wu-ftpd
Warning:osscan results May is unreliable because we could not find at least 1 open and 1 closed port
Device type:specialized| Wap
Running:ipxe 1.X, Linux 2.4.x|2.6.x
OS cpe:cpe:/o:ipxe:ipxe:1.0.0%2b cpe:/o:linux:linux_kernel:2.4.20 cpe:/o:linux:linux_kernel:2.6.22
OS details:ipxe 1.0.0+, Tomato 1.28 (Linux 2.4.20), Tomato firmware (Linux 2.6.22)
Service Info:Host:www.net.cn
TRACEROUTE (using port 21/tcp)
HOP RTT ADDRESS
1 3.00 MS 192.168.0.1
2 ... 30
Nse:script post-scanning.
Initiating NSE at 21:02
Completed NSE at 21:02, 0.00s elapsed
Initiating NSE at 21:02
Completed NSE at 21:02, 0.00s elapsed
Read Data Files From:d:\nmap
OS and Service detection performed. Incorrect results at https://nmap.org/submit/.
Nmap done:1 IP Address (1 host up) scanned in 73.93 seconds
Raw packets sent:2158 (97.246KB) | Rcvd:33 (2.050KB)
(PS: Make an advertisement, more original article, in my personal blog site: http://www.zifangsky.cn)
This article is from "Zifangsky's personal blog" blog, make sure to keep this source http://983836259.blog.51cto.com/7311475/1719243
Batch Scan prototype in Java call Nmap for host detection