扫描整个网段的多线程程序

  描500个IP10秒钟左右, 一个IP等待0.3秒
说来惭愧, 我觉得是用java写的过程编程, 少贴一点仅供参考:

  1. package steeven;
  2. import java.sql.*;
  3. import java.io.*;
  4. import java.util.*;
  5. import java.text.*;
  6. import javax.servlet.http.*;
  7. public class Ip extends Common implements Runnable{
  8. public String ip; // IP, 用户名, 主机名
  9. ResultSet list; // 分页显示的记录集
  10. public Ip cur; // 分页显示的当前记录
  11. static public Hashtable ping = new Hashtable(); //ping 后的结果集
  12. static int threadCount = 0//当前线程的数量, 防止过多线程摧毁电脑
  13. public Ip() {}
  14. public Ip(String ip){
  15. this.ip=ip;
  16. Thread r = new Thread(this);
  17. r.start();
  18. }
  19. public static void Ping(String ip) throws Exception{
  20. //最多30个线程
  21. while(threadCount>30)
  22. Thread.sleep(50);
  23. threadCount +=1;
  24. Ip p = new Ip(ip);
  25. }
  26. public void PingAll() throws Exception{
  27. threadCount =0;
  28. ping = new Hashtable();
  29. while(next()) //next()对所有局域网Ip放到cur
  30. Ping(cur.ip);
  31. //等着所有Ping结束
  32. while(threadCount>0)
  33. Thread.sleep(50);
  34. }
  35. public void run(){
  36. try{
  37. Process p= Runtime.getRuntime().exec ("ping "+ip+ " -w 300 -n 1");
  38. InputStreamReader ir = new InputStreamReader(p.getInputStream());
  39. LineNumberReader input = new LineNumberReader (ir);
  40. //读取结果行
  41. for (int i=1 ; i<7; i++)
  42. input.readLine();
  43. String line= input.readLine();
  44. if (line.length()<17 || line.substring(8,17).equals("timed out"))
  45. ping.put(ip,new Boolean(false));
  46. else
  47. ping.put(ip,new Boolean(true));
  48. //线程结束
  49. threadCount -= 1;
  50. }catch (IOException e){}
  51. }
  52. public static void main(String[] args) throws Exception{
  53. Ip ip= new Ip();
  54. ip.PingAll();
  55. java.util.Enumeration key = ping.keys();
  56. String k;
  57. while((k = (String)key.nextElement()) != null)
  58. System.out.println(k+": "+ping.get(k));
  59. }
  60. }
1. 利用Ping比较愚蠢, 但是相对简单些
2. 如果Ping 成功后, 用nbtstat还可以得到主机名, 当前用户名, MAC地址...一切尽在掌握中 :) 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值