nmap使用

1、查看指定服务器指定端口上的所有服务

nmap -sV 10.1.1.1-p 2000-20000

 2、使用java调用nmap

/***
 * 多线程执行 含含检测ip是否可以访问 含限制每秒的发包次数
 * D:\\nmap-7.91\\nmap.exe -sV 10.28.151.93 -p 8000-8080 --max-rate 100
 */

import java.io.*;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main22 {
    /**
     * 方法 1:使用 FileWriter 写文件
     *
     * @param file   文件目录
     * @param conent 待写入内容
     * @throws IOException
     */

    public static void fileWriterMethod(String file, String conent) {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file, true)));
            out.write(conent + "\r\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /***
     * 调用namp
     * @param ip
     * @param port
     */
    public static void
    coreExe(String ip, String port) {
        try {
            Runtime rt = Runtime.getRuntime();
            //Process pr = rt.exec("cmd /c dir"); // cmd /c calc
            // Process pr = rt.exec("D:\\xunlei\\project.aspx");
            //Process pr = rt.exec("D:\\nmap-7.91\\nmap.exe -sV " + ip + " -p " + port);
            Process pr = rt.exec("C:\\Program Files (x86)\\Nmap\\nmap.exe -sV " + ip + " -p " + port+" --max-rate 100");
            BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream(), "GBK"));
            String line = null;

            synchronized (Main22.class) {
                String log_info = "*********************************************" + ip + "*************************************************";
                fileWriterMethod("nmap_output_alive_8088.txt", log_info);
                while ((line = input.readLine()) != null) {
                    System.out.println(line);
                    fileWriterMethod("nmap_output_alive_8088.txt", line);
                }
                int exitVal = pr.waitFor();
                System.out.println("Exited with error code " + exitVal);
            }
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }
    }

    /**
     * 检测ip是否可以ping
     *
     * @param ipAddress
     * @return
     * @throws Exception
     */
    public static boolean ping(String ipAddress) throws Exception {
        int timeOut = 1000;  //超时应该在3钞以上
        boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut);
        System.out.println("ip :" + ipAddress + " ping: " + status);
        // 当返回值是true时,说明host是可用的,false则不可。
        return status;
    }

    /**
     * 1.创建固定大小的线程池。每次提交一个任务就创建一个线程,直到线程达到线程池的最大大小<br>
     * 2.线程池的大小一旦达到最大值就会保持不变,如果某个线程因为执行异常而结束,那么线程池会补充一个新线程<br>
     * 3.因为线程池大小为3,每个任务输出index后sleep 2秒,所以每两秒打印3个数字,和线程名称<br>
     * //"线程名称:" + Thread.currentThread().getName()
     *
     * @param ipLists
     */
    public static void fixTheadPoolTest(List ipLists) throws Exception {
        Main22 main = new Main22();
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
        int ipLists_len = ipLists.size();
        for (int i = 0; i < ipLists_len; i++) {
            final int ii = i;
            final String ip = (String) ipLists.get(0);
            ipLists.remove(0);
            //System.out.println(ipLists);
            fixedThreadPool.execute(() -> {
                try {
//                    if (Main22.ping(ip)) {//检测ip是否可以访问
//                        fileWriterMethod("nmap_output_8080_alive_6.txt", ip);
//                        System.out.println("线程名称:" + Thread.currentThread().getName() + ",执行" + ii + ",nmap 地址:" + ip);
//                        //main.coreExe(ip, "1-21000");
//                        main.coreExe(ip, "8080");
//                    }
                    System.out.println("线程名称:" + Thread.currentThread().getName() + ",执行" + ii + ",nmap 地址:" + ip);
                    main.coreExe(ip, "8088");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
        fixedThreadPool.shutdown();
    }

    public static void main(String[] args) throws Exception {

        //BufferedReader是可以按行读取文件
        FileInputStream inputStream = new FileInputStream("D:\\intellijProjects\\untitled\\src\\main\\java\\alive0.txt");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String str = null;
        List ipLists = new ArrayList();
        while ((str = bufferedReader.readLine()) != null) {
            ipLists.add(str);
        }


        //close
        inputStream.close();
        bufferedReader.close();

        //main.coreExe("10.28.151.93", "8000-8080");
        System.out.println("*****begin*****");
        Main22.fixTheadPoolTest(ipLists);
        System.out.println("*****end*****");
    }
}

 

posted @ 2021-10-13 08:51  权杖攻城狮  阅读(119)  评论(0编辑  收藏  举报