使用jFreeChart实现web实时设备状态的图形化监控之三:使用jPcap抓取网络数据包小试

抓包前,首先要编写实现了PacketReceiver接口的类,即数据包处理器,由与抓包时,对应某个网络接口的JpcapCaptor对象会阻塞,所以我们将每个网卡上得到的对应的JpcapCaptor对象放到一个独立线程中运行;TestPacketReceiver是个数据包解析器,本例中我们只是简单打印出收到的数据包类型及关键参数,阅读如下代码建议参照jPcap的在线文档(http://netresearch.ics.uci.edu/kfujii/jpcap/doc/javadoc/index.html )

TestPacketReceiver.java代码如下:

1. import
2. import
3. import
4. import
5. /**
6.  * 使用jpcap显示网络上的各种数据包
7.  * @author www.NetJava.cn 
8.  */
9. public class
10.       
11. //程序启动主方法
12. public static void
13. try{  
14. //获取本机上的网络接口对象数组
15. final
16. for(int i=0;i<devices.length;i++){  
17.             NetworkInterface nc=devices[i];  
18. //创建某个卡口上的抓取对象,最大为2000个
19. 2000, true, 20);  
20.         startCapThread(jpcap);  
21. "开始抓取第"+i+"个卡口上的数据");  
22.         }  
23. catch(Exception ef){  
24.             ef.printStackTrace();  
25. "启动失败:  "+ef);  
26.         }  
27.   
28.    }  
29. //将每个Captor放到独立线程中运行
30. public static void startCapThread(final
31.        JpcapCaptor jp=jpcap;  
32. new
33. public void
34. //使用接包处理器循环抓包
35. 1, new
36.            }  
37.        };  
38. new Thread(rnner).start();//启动抓包线程
39.    }      
40. }  
41.   
42. /**
43.  * 抓包监听器,实现PacketReceiver中的方法:打印出数据包说明
44.  * @author www.NetJava.cn 
45.  */
46. class TestPacketReceiver  implements
47. /**
48.        * 实现的接包方法:
49.        */
50. public void
51. //Tcp包,在java Socket中只能得到负载数据
52. if(packet instanceof
53.             TCPPacket p=(TCPPacket)packet;  
54. "TCPPacket:| dst_ip "+p.dst_ip+":"+p.dst_port  
55. "|src_ip "+p.src_ip+":"+p.src_port  
56. " |len: "+p.len;  
57.         System.out.println(s);  
58.         }  
59. //UDP包,开着QQ,你就会看到:它是tcp+udp
60. else if(packet instanceof
61.             UDPPacket p=(UDPPacket)packet;  
62. "UDPPacket:| dst_ip "+p.dst_ip+":"+p.dst_port  
63. "|src_ip "+p.src_ip+":"+p.src_port  
64. " |len: "+p.len;  
65.            System.out.println(s);  
66.         }  
67. //如果你要在程序中构造一个ping报文,就要构建ICMPPacket包
68. else if(packet instanceof
69.            ICMPPacket p=(ICMPPacket)packet;  
70. //ICMP包的路由链
71. "";  
72. for(int i=0;i<p.router_ip.length;i++){  
73. " "+p.router_ip[i].getHostAddress();  
74.            }  
75. "@ @ @ ICMPPacket:| router_ip "+router_ip  
76. " |redir_ip: "+p.redir_ip  
77. " |mtu: "+p.mtu  
78. " |length: "+p.len;  
79.           System.out.println(s);  
80.         }  
81. //是否地址转换协议请求包
82. else if(packet instanceof
83.            ARPPacket p=(ARPPacket)packet;  
84. //Returns the hardware address (MAC address) of the sender
85.            Object  saa=   p.getSenderHardwareAddress();  
86.            Object  taa=p.getTargetHardwareAddress();  
87. "* * * ARPPacket:| SenderHardwareAddress "+saa  
88. "|TargetHardwareAddress "+taa  
89. " |len: "+p.len;  
90.          System.out.println(s);  
91.               
92.         }  
93. //取得链路层数据头 :如果你想局网抓包或伪造数据包,嘿嘿
94.      DatalinkPacket datalink  =packet.datalink;  
95. //如果是以太网包
96. if(datalink instanceof
97.          EthernetPacket ep=(EthernetPacket)datalink;  
98. "  datalink layer packet: "
99. "|DestinationAddress: "+ep.getDestinationAddress()  
100. "|SourceAddress: "+ep.getSourceAddress();  
101.           System.out.println(s);  
102.     }      
103.   }  
104.   
105. }

 


 运行这段程序,你机器从网络收所有收发到的数据,就都可以展现在你眼前了!,在我的机器上,摘抄一段输出如下:

datalink layer packet: |DestinationAddress: 01:00:5e:00:00:01|SourceAddress: 00:19:e0:f0:ee:55