Skip to main content

Java Code to Get MAC Address From Target IP Address

MAC Address is a Unique identification of a Device. Computer, Mobile, Tablet, PC all have a Unique MAC Address. If you want to know about MAC Address here is some, MAC on Wiki. MAC Address is generally 48 bits long eg, 00:ff:a3:f0:76:ae
Here we are going to discuss about How to get that address by using supplied IP Address over the network.
Here is the Java Code
GetMACAddress.java

import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.Arrays;

import jpcap.*;
import jpcap.packet.*;

public class GetMACAddress{
    public static byte[] arp(InetAddress ip) throws java.io.IOException{
        //find network interface
        NetworkInterface[] devices=JpcapCaptor.getDeviceList();
        NetworkInterface device=null;

loop:    for(NetworkInterface d:devices){
            for(NetworkInterfaceAddress addr:d.addresses){
                if(!(addr.address instanceof Inet4Address)) continue;
                byte[] bip=ip.getAddress();
                byte[] subnet=addr.subnet.getAddress();
                byte[] bif=addr.address.getAddress();
                for(int i=0;i<4;i++){
                    bip[i]=(byte)(bip[i]&subnet[i]);
                    bif[i]=(byte)(bif[i]&subnet[i]);
                }
                if(Arrays.equals(bip,bif)){
                    device=d;
                    break loop;
                }
            }
        }
      
        if(device==null)
            throw new IllegalArgumentException(ip+" is not a local address");
      
        //open Jpcap
        JpcapCaptor captor=JpcapCaptor.openDevice(device,2000,false,3000);
        captor.setFilter("arp",true);
        JpcapSender sender=captor.getJpcapSenderInstance();
      
        InetAddress srcip=null;
        for(NetworkInterfaceAddress addr:device.addresses)
            if(addr.address instanceof Inet4Address){
                srcip=addr.address;
                break;
            }

        byte[] broadcast=new byte[]{(byte)255,(byte)255,(byte)255,(byte)255,(byte)255,(byte)255};
        ARPPacket arp=new ARPPacket();
        arp.hardtype=ARPPacket.HARDTYPE_ETHER;
        arp.prototype=ARPPacket.PROTOTYPE_IP;
        arp.operation=ARPPacket.ARP_REQUEST;
        arp.hlen=6;
        arp.plen=4;
        arp.sender_hardaddr=device.mac_address;
        arp.sender_protoaddr=srcip.getAddress();
        arp.target_hardaddr=broadcast;
        arp.target_protoaddr=ip.getAddress();
      
        EthernetPacket ether=new EthernetPacket();
        ether.frametype=EthernetPacket.ETHERTYPE_ARP;
        ether.src_mac=device.mac_address;
        ether.dst_mac=broadcast;
        arp.datalink=ether;
      
        sender.sendPacket(arp);
      
        while(true){
            ARPPacket p=(ARPPacket)captor.getPacket();
            if(p==null){
                throw new IllegalArgumentException(ip+" is not a local address");
            }
            if(Arrays.equals(p.target_protoaddr,srcip.getAddress())){
                return p.sender_hardaddr;
            }
        }
    }
      
    public static void main(String[] args) throws Exception{
        if(args.length<1){
            System.out.println("Usage: java GetMACAddress <ip address>");
        }else{
            byte[] mac=GetMACAddress.arp(InetAddress.getByName(args[0]));
            for (byte b : mac)
                System.out.print(Integer.toHexString(b&0xff) + ":");
            System.out.println();
            System.exit(0);
        }
    }
}

To run this you need to get a library called WinPCap from winpcap.org for Windows or LibPCap for Linux. And more over you need to get a Java Library JPCap from Click Here .


If you have both these Library installed on your system then you can compile and run.
E:\>javac GetMACAddress.java


E:\>java GetMACAddress 192.168.174.94
0:21:97:8f:1b:a7:
E:\>


This is How you can get a MAC address from any machines from your Network/LAN.


Popular posts from this blog

The top 10 open source forum/bulletin/discussion software

The forum software is needed to build a discussion platform. There are many forum software which can be get freely on internet. Here I am going to list out some of 10 best forum software which you can be able to put it on your website.

1. Discourse:Discourse is an open sourceInternet forum software application founded in 2013 by Jeff Atwood, Robin Ward, and Sam Saffron. Discourse received funding from First Round Capital and Greylock.[3]
From a usability perspective, Discourse breaks with existing forum software by including features recently popularized by large social networks, such as infinite scrolling, live updates, oneboxing, expanding links, and drag and dropattachments.[4] However, the stated goals of the project are social rather than technical, to improve online discussion quality through improved forum software.
The application is written in JavaScript and Ruby on Rails,[5] and is released under the GNU General Public License version 2. PostgreSQL is the supported datab…

MySQL Database Connection at OpenShift Redhat Cloud using PDO Class

I was very unhappy when my code was correct but could not connect to my mysql database in openshift. But after trying some tricks it got to work. So I will share some tips that you can also put to your PHP MySQL application in openshift.
First thing to be noted is that when you create a PHP Application in OpenShift you will get some environmental variable. Which you can view them by using echo $ENV_NAME in the shell. Some of MYSQL DB Environmental variables in openshift clouds are follows. OPENSHIFT_MYSQL_DB_HOST
OPENSHIFT_MYSQL_DB_PORT
OPENSHIFT_MYSQL_DB_USERNAME
OPENSHIFT_MYSQL_DB_PASSWORD
OPENSHIFT_MYSQL_DB_SOCKET
OPENSHIFT_MYSQL_DB_URL You can Find more about it here OpenShift Variables
Considering we have database name 'gitspot' and have a table name having demo where there is a field 'name' and has a record 'RedHat Openshift' Now create a php file conf.php which contains all the Database Host Port etc and the way to connect using PDO Class. <?php
  …