java获取固定IP发来所有的数据包,需要实现网络嗅探的部分功能:
代码如下;
/JpcapTipjava
/
package m;
import jpcapPacketReceiver;
import jpcapJpcapCaptor;
import jpcappacket;
import jpcapNetworkInterface;
import jpcapNetworkInterfaceAddress;
//import javanetInetAddress;
//import javanetUnknownHostException;
public class JpcapTip implements PacketReceiver {
public void receivePacket(Packet packet) {
Systemoutprintln("");
/IP数据报报文头/
byte[] l=packetheader;
/
for (int t=0;t<21;t++){
Systemoutprint(l[t]+" ");
}
/
String str="";
Systemoutprint("报文头 : ");
for (int i=0;i<llength;i++) {
//str=str+l;
int m=0;
m=l[i];
m=m<<24;
m=m>>>24;
str=str+IntegertoHexString(m);
//Systemoutprint(" "+l[i]);
}
Systemoutprintln(str);
int d=llength;
Systemoutprintln("首部长度 :"+(d8)+"bit");
/分析源IP地址和目的IP地址/
/分析协议类型/
/
if(packetgetClass()equals(IPPacketclass)) {
IPPacket ipPacket=(IPPacket)packet;
byte[] iph=ipPacketoption;
String iphstr=new String(iph);
Systemoutprintln(iphstr);
}
/
if(packetgetClass()equals(ARPPacketclass))
{
Systemoutprintln("协议类型 :ARP协议");
try {
ARPPacket arpPacket = (ARPPacket)packet;
Systemoutprintln("源网卡MAC地址为 :"+arpPacketgetSenderHardwareAddress());
Systemoutprintln("源IP地址为 :"+arpPacketgetSenderProtocolAddress());
Systemoutprintln("目的网卡MAC地址为 :"+arpPacketgetTargetHardwareAddress());
Systemoutprintln("目的IP地址为 :"+arpPacketgetTargetProtocolAddress());
} catch( Exception e ) {
eprintStackTrace();
}
}
else
if(packetgetClass()equals(UDPPacketclass))
{
Systemoutprintln("协议类型 :UDP协议");
try {
UDPPacket udpPacket = (UDPPacket)packet;
Systemoutprintln("源IP地址为 :"+udpPacketsrc_ip);
int tport = udpPacketsrc_port;
Systemoutprintln("源端口为:"+tport);
Systemoutprintln("目的IP地址为 :"+udpPacketdst_ip);
int lport = udpPacketdst_port;
Systemoutprintln("目的端口为:"+lport);
} catch( Exception e ) {
eprintStackTrace();
}
}
else
if(packetgetClass()equals(TCPPacketclass)) {
Systemoutprintln("协议类型 :TCP协议");
try {
TCPPacket tcpPacket = (TCPPacket)packet;
int tport = tcpPacketsrc_port;
Systemoutprintln("源IP地址为 :"+tcpPacketsrc_ip);
Systemoutprintln("源端口为:"+tport);
Systemoutprintln("目的IP地址为 :"+tcpPacketdst_ip);
int lport = tcpPacketdst_port;
Systemoutprintln("目的端口为:"+lport);
} catch( Exception e ) {
eprintStackTrace();
}
}
else
if(packetgetClass()equals(ICMPPacketclass))
Systemoutprintln("协议类型 :ICMP协议");
else
Systemoutprintln("协议类型 :GGP、EGP、JGP协议或OSPF协议或ISO的第4类运输协议TP4");
/IP数据报文数据/
byte[] k=packetdata;
String str1="";
Systemoutprint("数据 : ");
for(int i=0;i<klength;i++) {
//int m=0;
//m=k[i];
//m=m<<24;
//m=m>>>24;
//str1=str+IntegertoHexString(m);
str1 = new String(k);
//str1=str1+k[i];
//Systemoutprint(" "+k[i]);
}
Systemoutprintln(str1);
Systemoutprintln("数据报类型 : "+packetgetClass());
Systemoutprintln("");
}
public static void main(String[] args) throws Exception{
// TODO 自动生成方法存根
NetworkInterface[] devices = JpcapCaptorgetDeviceList(); //getDeviceList();
//for (int i =0; i<deviceslength;i++) {
int a=0;
//try {
/本地网络信息/
byte[] b=devices[1]mac_address; //网卡物理地址
//}
//catch() {}
Systemoutprint("网卡MAC : 00");
for (int j=0;j<blength;j++){
//a=a<<8;
a=b[j];
a=a<<24;
a=a>>>24;
Systemoutprint(IntegertoHexString(a));
}
Systemoutprintln();
NetworkInterfaceAddress[] k=devices[1]addresses;
//Systemoutprintln("网卡MAC : "+IntegertoHexString(a));
for(int n=0;n<klength;n++) {
Systemoutprintln("本机IP地址 : "+k[n]address); //本机IP地址
Systemoutprintln("子网掩码 : "+k[n]subnet); //子网掩码
}
Systemoutprintln("网络连接类型 : "+devices[1]datalink_description);
//}
NetworkInterface deviceName = devices[1];
/将网卡设为混杂模式下用网络设备deviceName/
JpcapCaptor jpcap =JpcapCaptoropenDevice(deviceName, 2000, false, 1); //openDevice(deviceName,1028,false,1);
jpcaploopPacket(-1,new JpcapTip());
}
}
jpcap你要自己下好相应的包和配置,不知道的就在网上搜吧··
import javanetInetAddress;
import javautilArrayList;
import javautilGregorianCalendar;
import javautilList;
import jpcapJpcapCaptor;
import jpcapJpcapSender;
import jpcapNetworkInterface;
import jpcappacketEthernetPacket;
import jpcappacketICMPPacket;
import jpcappacketIPPacket;
public class JPing {
private NetworkInterface[] devices = JpcapCaptorgetDeviceList();
private JpcapSender sender;
private JpcapCaptor jpcap;
private ICMPPacket icmpPacket;
private List<String> listResult = new ArrayList<String>();
/
组织ICMP报文发送,并开启线程接收报文
@param ip
/
public void ping(String ip) {
try {
jpcap = JpcapCaptoropenDevice(devices[0], 200, false, 20);
sender = jpcapgetJpcapSenderInstance();
jpcapsetFilter("icmp", true);// 过滤器,只接受ICMP报文
icmpPacket = new ICMPPacket();
icmpPackettype = ICMPPacketICMP_ECHO; // 发送回显请求报文
icmpPacketsetIPv4Parameter(0, false, false, false, 0, false,
false, false, 0, 1010101, 100, IPPacketIPPROTO_ICMP,
devices[0]addresses[1]address, InetAddressgetByName(ip));
// 随意的32bytes数据
icmpPacketdata = "abcdefghijklmnopqrstuvwxyzabcdef"getBytes();
EthernetPacket ethernetPacket = new EthernetPacket();
ethernetPacketframetype = EthernetPacketETHERTYPE_IP;
ethernetPacketsrc_mac = devices[0]mac_address;
// 广播地址
ethernetPacketdst_mac = new byte[] { (byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
icmpPacketdatalink = ethernetPacket;
listResultadd("Pinging " + icmpPacketdst_ip + " with "
+ icmpPacketdatalength + " bytes of data: ");
startCapThread(jpcap);
for (int i = 0; i < 5; i++) {
icmpPacketsec = 0;
//icmpPacketusec = SystemcurrentTimeMillis();
icmpPacketusec = new GregorianCalendar()getTimeInMillis();// 记录发送时间
icmpPacketseq = (short) (1000 + i);
icmpPacketid = (short) (999 + i);
sendersendPacket(icmpPacket);
try {
Threadsleep(1000);
} catch (Exception e) {
eprintStackTrace();
}
}
} catch (Exception e) {
eprintStackTrace();
}
}
/
接收ICMP报文
@param jpcap
/
public void getIcmpPacket(JpcapCaptor jpcapCaptor) {
try {
while (true) {
long tmp = 0;
String tmpStr = null;
ICMPPacket rp;
rp = (ICMPPacket) jpcapCaptorgetPacket();
if ((rp != null) && (rpseq - rpid == 1)
&& (rptype == ICMPPacketICMP_ECHOREPLY)) {// 若是ICMP回应报文,则列出。。。
tmp = (rpsec 1000 + rpusec / 1000 - icmpPacketsec
1000 - icmpPacketusec); // 计算发送与接受的时间差
if (tmp <= 0)
tmpStr = " < 1 ms ";
else
tmpStr = "= " + tmp + " ms ";
Systemoutprintln("Reply from "
+ rpsrc_ipgetHostAddress() + ": bytes = "
+ rpdatalength + " time " + tmpStr + "TTL = "
+ rphop_limit);
listResultadd("Reply from " + rpsrc_ipgetHostAddress()
+ ": bytes = " + rpdatalength + " time " + tmpStr
+ "TTL = " + rphop_limit);
}
}
} catch (Exception e) {
eprintStackTrace();
}
}
/
接收ICMP报文
@param jpcap
/
public void startCapThread(final JpcapCaptor jpcap) {
Runnable runner = new Runnable() {
public void run() {
getIcmpPacket(jpcap);
}
};
new Thread(runner)start();
}
public static void main(String[] args) {
new JPing()ping(">
import javanetInet4Address;
import javanetInetAddress;
import javanetURL;
import javautilArrays;
import jpcapJpcapCaptor;
import jpcapJpcapSender;
import jpcapNetworkInterface;
import jpcapNetworkInterfaceAddress;
import jpcappacketEthernetPacket;
import jpcappacketICMPPacket;
import jpcappacketIPPacket;
import jpcappacketPacket;
public class JpcapTip {
public static void main(String[] args) throws Exception {
args = new String[2];
args[0]="1";
args[1]="22018112443";
if (argslength < 2) {
Systemoutprintln("Usage: java Traceroute <device index (eg, 0, 1)> <target host address>");
Systemexit(0);
} // initialize Jpcap
NetworkInterface device = JpcapCaptorgetDeviceList()[IntegerparseInt(args[0])];
JpcapCaptor captor = JpcapCaptoropenDevice(device, 2000, false, 5000);
InetAddress thisIP = null;
for (NetworkInterfaceAddress addr : deviceaddresses)
if (addraddress instanceof Inet4Address) {
thisIP = addraddress;
break;
}
// obtain MAC address of the default gateway
InetAddress pingAddr = InetAddressgetByName("(">
以上就是关于java如何编程实现,获取固定IP发来所有的数据包 全部的内容,包括:java如何编程实现,获取固定IP发来所有的数据包 、有谁知道怎样使用java编写ping程序、java 模拟traceroute程序 能得到跳数 时间 经过路由ip 求高手指点 。。。 。。。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)