问题描述如下:
用NMAP工具扫描多个IP,得到它们的端口号、操作系统等等信息,现在我感兴趣的是它们的操作系统信息,现在如何把扫描到的各个IP对应的操作系统信息给提取出来??
希望能提取到的信息格式如下(类似格式也行,只要能区分清楚):
IP:XX.XX.XX.XX
OS:XXXXXXXXIP:XX.XX.XX.XX
OS:XXXXXXXXIP:XX.XX.XX.XX
OS:XXXXXXXX有时候扫描不到操作系统信息那么OS一栏可为空
希望高手能给我一段代码,自己水平比较次,谢谢了!
由于我一次只能给100分,帮我解决问题的我会再开一个帖子给你100分!文本内容如下(我把关键信息用红色字体表示):
# Nmap 4.53 scan initiated Thu Nov 06 10:16:56 2008 as: nmap -sS -O -v -oN baohui 192.168.6.20-23 
Initiating OS detection (try #1) against 3 hosts
Retrying OS detection (try #2) against 2 hosts
Interesting ports on 192.168.6.21:
Not shown: 1709 filtered ports
PORT     STATE SERVICE
80/tcp   open  http
139/tcp  open  netbios-ssn
443/tcp  open  https
445/tcp  open  microsoft-ds
3389/tcp open  ms-term-serv
MAC Address: 00:1B:B9:85:FE:95 (Elitegroup Computer System Co.)
Warning: OSScan results may be unreliable because we could not find at least 1 open and 1 closed port
Device type: general purpose
Running: Microsoft Windows XP
OS details: Microsoft Windows XP SP2
Network Distance: 1 hop
TCP Sequence Prediction: Difficulty=263 (Good luck!)
IP ID Sequence Generation: IncrementalInteresting ports on 192.168.6.22:
Not shown: 1709 closed ports
PORT     STATE SERVICE
135/tcp  open  msrpc
139/tcp  open  netbios-ssn
445/tcp  open  microsoft-ds
990/tcp  open  ftps
1025/tcp open  NFS-or-IIS
MAC Address: 00:1A:A0:3A:76:AC (Dell)
No OS matches for host
Network Distance: 1 hopInteresting ports on 192.168.6.23:
Not shown: 1704 closed ports
PORT     STATE SERVICE
21/tcp   open  ftp
80/tcp   open  http
135/tcp  open  msrpc
139/tcp  open  netbios-ssn
443/tcp  open  https
445/tcp  open  microsoft-ds
912/tcp  open  unknown
990/tcp  open  ftps
1025/tcp open  NFS-or-IIS
1026/tcp open  LSA-or-nterm
MAC Address: 00:1E:68:5C:19:E6 (Quanta Computer)
No OS matches for host
Network Distance: 1 hopRead data files from: E:\nmap-4.53
OS detection performed. Please report any incorrect results at http://insecure.org/nmap/submit/ .
# Nmap done at Thu Nov 06 10:17:13 2008 -- 4 IP addresses (3 hosts up) scanned in 16.938 seconds

解决方案 »

  1.   

    如果数据格式按你所述(一条port信息之后始终对应一个OS 提示信息)参考下面
    public Hastable getPort_OS() throws IOException{
    File file= new File("d:\\existFile.txt");
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);
    String portSign = "Interesting ports on";
    String osSign1 = "OS details:";
    String osSign2 = "No OS matches for host";
    Hashtable port_OS = new Hashtable();
    String temp = br.readLine();
    String port ="";
    String os ="";
    while(temp!=null){
    temp = temp.trim();
    fileName.add(temp);
    if(temp.indexOf(portSign)!=-1){
    port = temp.replaceAll(portSign, "").trim();
    }
    else if((temp.indexOf(osSign1)!=-1)||(temp.indexOf(osSign2)!=-1)){
    os = temp;
    port_OS.put(port, os);
    }
    temp= br.readLine();
    }
    return port_OS;
    }Hashtable 中 key 是port,value 是OS信息,至于遍历Hashtable,就不用写了吧
      

  2.   

    1楼的高手,能不能把完整的代码给我呀,因为Hashtable我从没用过,从头学时间来不及啦
    赶着要呢……
    分数可以给多点的……
    谢谢
      

  3.   


    package com.train.first;import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;public class Test
    {
    public static void main(String[] args)
    {
    BufferedReader reader = null;
    List<Host> list = new ArrayList<Host>();
    Host host = null;

    try
    {
    reader = new BufferedReader(new FileReader("test.txt"));
    String line = null;

    while (null != (line = reader.readLine()))
    {
    if (-1 != line.indexOf("Interesting ports on"))
    {
    if (null != host)
    {
    list.add(host);
    }

    String ip = line.replace("Interesting ports on", "").replace(":", "").trim();
    host = new Host(ip);
    }

    if (-1 != line.indexOf("OS details:"))
    {
    String os = line.replace("OS details:", "").trim();
    host.setOs(os);
    }
    }

    list.add(host);
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }
    finally
    {
    if (null != reader)
    {
    try
    {
    reader.close();
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }
    }
    }

    for (int i = 0; i < list.size(); i++)
    {
    System.out.println(list.get(i));
    }
    }

    }class Host
    {
    private String ip; private String os;

    public Host(String ip)
    {
    this.ip = ip;
    } public String getIp()
    {
    return ip;
    } public void setIp(String ip)
    {
    this.ip = ip;
    } public String getOs()
    {
    return os;
    } public void setOs(String os)
    {
    this.os = os;
    }

    @Override
    public String toString()
    {
    ip = null == ip ? "" : ip;
    os = null == os ? "" : os;
    return "IP:" + ip + System.getProperty("line.separator") + "OS:" + os;
    }}
      

  4.   

    import java.io.InputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.List;public class LoggerReader {

    private String content = null;

    private List<String> ipList = new ArrayList<String>();

    private List<String> osList = new ArrayList<String>();

    private static final String INTERESTING_IP = "Interesting ports on ";

    private static final String NO_OS_MATCH = "No OS matches for host";

    private static final String OS_DETAIL = "OS details:";

    public void readByteByFileStream(String url) {
    try {
    readByteByStream(new FileInputStream(url));
    } catch(IOException e) {
    e.printStackTrace();
    }
    }

    public void readByteByStream(InputStream in) throws IOException {
    byte[] b = new byte[in.available()];
    in.read(b);
    content = new String(b);
    in.close();
    }

    public void readByteByUrlStream(String url) throws IOException {
    try {
    readByteByStream(new URL(url).openStream());
    } catch(IOException e) {
    e.printStackTrace();
    }
    }
    public void toLogger() {
    for (int i = 0; i < ipList.size(); i++) {
    System.out.println("IP:" + ipList.get(i));
    System.out.println("OS:" + osList.get(i));
    }
    }

    public void parse() {
    for (int index = 0; index < content.length() && 
    content.indexOf(INTERESTING_IP, index) != -1; index++) {
    int ipStartIndex = content.indexOf(INTERESTING_IP, index);
    int ipEndIndex = content.indexOf(":", ipStartIndex + INTERESTING_IP.length());
    ipList.add(content.substring(ipStartIndex + INTERESTING_IP.length(), ipEndIndex));
    index = ipEndIndex;

    int ipNextStartIndex = content.indexOf(INTERESTING_IP, index);
    int osStartIndex = content.indexOf(OS_DETAIL, index);
    if (osStartIndex > ipNextStartIndex || osStartIndex == -1) {
    osList.add(NO_OS_MATCH);
    } else {
    int osEndIndex = content.indexOf(":", osStartIndex + OS_DETAIL.length());
    osList.add(content.substring(osStartIndex + OS_DETAIL.length() + 1, osEndIndex));
    }
    }
    }
    public static void main(String[] args) {

    LoggerReader reader = new LoggerReader();
    reader.readByteByFileStream("e:\\a.txt");
    reader.parse();
    reader.toLogger();
    }
    }
      

  5.   


    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Hashtable;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;public class ParsePort_OS {

    private String fileName = "yourFileName.txt";
    public static void main(String args[]) {
    ParsePort_OS ppo = new ParsePort_OS();
    try {
    ppo.printPort_OS();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } public void printPort_OS() throws IOException {
    Hashtable ht = getPort_OS();
    Set set = ht.entrySet();
    Iterator it = set.iterator();
    while (it.hasNext()) {
    Map.Entry me = (Map.Entry) it.next();
    String port = (String) me.getKey();
    String os = (String) me.getValue();
    System.out.println("IP: " + port);
    System.out.println("OS: " + os + "\n");
    }
    } public Hashtable getPort_OS() throws IOException {
    File file = new File(fileName);
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);
    String portSign = "Interesting ports on";
    String osSign1 = "OS details:";
    String osSign2 = "No OS matches for host";
    Hashtable port_OS = new Hashtable();
    String temp = br.readLine();
    String port = "";
    String os = "";
    while (temp != null) {
    temp = temp.trim();
    if (temp.indexOf(portSign) != -1) {
    port = temp.replaceAll(portSign, "").trim();
    } else if ((temp.indexOf(osSign1) != -1)
    || (temp.indexOf(osSign2) != -1)) {
    os = temp;
    port_OS.put(port, os);
    }
    temp = br.readLine();
    }
    return port_OS;
    }}
    分很多,受之有愧
      

  6.   

    谢谢各位高手
    我会给分的,这个帖子4楼40分,5楼30分,6楼30分,4楼首先发出来的嘛
    另外我开个帖子,你们去回复一下吧,我再给分帖子地址:http://topic.csdn.net/u/20081106/11/6b518ae9-7573-419b-847f-4034891c45fa.html