在linux用java根据ip获得mac地址?
try {
ip = InetAddress.getLocalHost();
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
if (network != null) {
byte[] mac = network.getHardwareAddress();
if(mac != null) {
System.out.print(“MAC address : “);
StringBuilder sb = new StringBuilder();
for (int i = 0; i mac.length; i++) {
sb.append(String.format(“%02X%s”, mac[i], (i mac.length – 1) ? “-” : “”));
}
System.out.println(sb.toString());
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
}
java如何编程实现,获取固定IP发来所有的数据包 ?
java获取固定IP发来所有的数据包,需要实现网络嗅探的部分功能:
代码如下;
/*******************
* JpcapTip.java
*/
package m;
import jpcap.PacketReceiver;
import jpcap.JpcapCaptor;
import jpcap.packet.*;
import jpcap.NetworkInterface;
import jpcap.NetworkInterfaceAddress;
//import java.net.InetAddress;
//import java.net.UnknownHostException;
public class JpcapTip implements PacketReceiver {
public void receivePacket(Packet packet) {
System.out.println(“********************************************”);
/*IP数据报报文头*/
byte[] l=packet.header;
/*
for (int t=0;t21;t++){
System.out.print(l[t]+” *** “);
}
*/
String str=””;
System.out.print(“报文头 : “);
for (int i=0;il.length;i++) {
//str=str+l;
int m=0;
m=l[i];
m=m24;
m=m24;
str=str+Integer.toHexString(m);
//System.out.print(” *** “+l[i]);
}
System.out.println(str);
int d=l.length;
System.out.println(“首部长度 :”+(d*8)+”bit”);
/*分析源IP地址和目的IP地址*/
/*分析协议类型*/
/**
if(packet.getClass().equals(IPPacket.class)) {
IPPacket ipPacket=(IPPacket)packet;
byte[] iph=ipPacket.option;
String iphstr=new String(iph);
System.out.println(iphstr);
}
*/
if(packet.getClass().equals(ARPPacket.class))
{
System.out.println(“协议类型 :ARP协议”);
try {
ARPPacket arpPacket = (ARPPacket)packet;
System.out.println(“源网卡MAC地址为 :”+arpPacket.getSenderHardwareAddress());
System.out.println(“源IP地址为 :”+arpPacket.getSenderProtocolAddress());
System.out.println(“目的网卡MAC地址为 :”+arpPacket.getTargetHardwareAddress());
System.out.println(“目的IP地址为 :”+arpPacket.getTargetProtocolAddress());
} catch( Exception e ) {
e.printStackTrace();
}
}
else
if(packet.getClass().equals(UDPPacket.class))
{
System.out.println(“协议类型 :UDP协议”);
try {
UDPPacket udpPacket = (UDPPacket)packet;
System.out.println(“源IP地址为 :”+udpPacket.src_ip);
int tport = udpPacket.src_port;
System.out.println(“源端口为:”+tport);
System.out.println(“目的IP地址为 :”+udpPacket.dst_ip);
int lport = udpPacket.dst_port;
System.out.println(“目的端口为:”+lport);
} catch( Exception e ) {
e.printStackTrace();
}
}
else
if(packet.getClass().equals(TCPPacket.class)) {
System.out.println(“协议类型 :TCP协议”);
try {
TCPPacket tcpPacket = (TCPPacket)packet;
int tport = tcpPacket.src_port;
System.out.println(“源IP地址为 :”+tcpPacket.src_ip);
System.out.println(“源端口为:”+tport);
System.out.println(“目的IP地址为 :”+tcpPacket.dst_ip);
int lport = tcpPacket.dst_port;
System.out.println(“目的端口为:”+lport);
} catch( Exception e ) {
e.printStackTrace();
}
}
else
if(packet.getClass().equals(ICMPPacket.class))
System.out.println(“协议类型 :ICMP协议”);
else
System.out.println(“协议类型 :GGP、EGP、JGP协议或OSPF协议或ISO的第4类运输协议TP4”);
/*IP数据报文数据*/
byte[] k=packet.data;
String str1=””;
System.out.print(“数据 : “);
for(int i=0;ik.length;i++) {
//int m=0;
//m=k[i];
//m=m24;
//m=m24;
//str1=str+Integer.toHexString(m);
str1 = new String(k);
//str1=str1+k[i];
//System.out.print(” *** “+k[i]);
}
System.out.println(str1);
System.out.println(“数据报类型 : “+packet.getClass());
System.out.println(“********************************************”);
}
public static void main(String[] args) throws Exception{
// TODO 自动生成方法存根
NetworkInterface[] devices = JpcapCaptor.getDeviceList(); //.getDeviceList();.
//for (int i =0; idevices.length;i++) {
int a=0;
//try {
/*本地网络信息*/
byte[] b=devices[1].mac_address; //网卡物理地址
//}
//catch() {}
System.out.print(“网卡MAC : 00”);
for (int j=0;jb.length;j++){
//a=a8;
a=b[j];
a=a24;
a=a24;
System.out.print(Integer.toHexString(a));
}
System.out.println();
NetworkInterfaceAddress[] k=devices[1].addresses;
//System.out.println(“网卡MAC : “+Integer.toHexString(a));
for(int n=0;nk.length;n++) {
System.out.println(“本机IP地址 : “+k[n].address); //本机IP地址
System.out.println(“子网掩码 : “+k[n].subnet); //子网掩码
}
System.out.println(“网络连接类型 : “+devices[1].datalink_description);
//}
NetworkInterface deviceName = devices[1];
/*将网卡设为混杂模式下用网络设备deviceName*/
JpcapCaptor jpcap =JpcapCaptor.openDevice(deviceName, 2000, false, 1); //openDevice(deviceName,1028,false,1);
jpcap.loopPacket(-1,new JpcapTip());
}
}
Java web 怎么得到客户端的Mac地址
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
/*
* 物理地址是48位,别和ipv6搞错了
*/
public class LOCALMAC {
/**
* @param args
* @throws UnknownHostException
* @throws SocketException
*/
public static void main(String[] args) throws UnknownHostException, SocketException {
// TODO Auto-generated method stub
//得到IP,输出PC-201309011313/122.206.73.83
InetAddress ia = InetAddress.getLocalHost();
System.out.println(ia);
getLocalMac(ia);
}
private static void getLocalMac(InetAddress ia) throws SocketException {
// TODO Auto-generated method stub
//获取网卡,获取地址
byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
System.out.println(“mac数组长度:”+mac.length);
StringBuffer sb = new StringBuffer(“”);
for(int i=0; imac.length; i++) {
if(i!=0) {
sb.append(“-“);
}
//字节转换为整数
int temp = mac[i]0xff;
String str = Integer.toHexString(temp);
System.out.println(“每8位:”+str);
if(str.length()==1) {
sb.append(“0″+str);
}else {
sb.append(str);
}
}
System.out.println(“本机MAC地址:”+sb.toString().toUpperCase());
}
}
现在我用java写了一个arp包发送到网络中。
java的底层能力非常有限的 要使用系统层的API,即是要JNI、JNA编程
如何用java获得局域网内所有ip?最好用arp方法,ping的太慢
需要遍历局域网IP的,大体分为两步:
1.得到局域网网段,可由自己机器的IP来确定
2.根据IP类型,一次遍历局域网内IP地址 给一个JAVA类,编译之后直接运行便可以得到局域网内所有IP,具体怎样使用自己编写相应代码调用便可 代码如下:
package bean;
import java.io.*;
import java.util.*;