nmap使用
1、查看指定服务器指定端口上的所有服务
1 | nmap -sV 10.1.1.1-p 2000-20000 |
2、使用java调用nmap
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | /*** * 多线程执行 含含检测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*****" ); } } |
如果你真心觉得文章写得不错,而且对你有所帮助,那就不妨小小打赏一下吧,如果囊中羞涩,不妨帮忙“推荐"一下,您的“推荐”和”打赏“将是我最大的写作动力!
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
2015-10-13 hibernate入门