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.


Comments

  1. Thanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
    mobile application development training online
    mobile app development course
    mobile application development course
    learn mobile application development
    mobile app development training
    app development training
    mobile application development training
    mobile app development course online
    online mobile application development

    ReplyDelete
  2. thanks for your information really good and very nice web design company in velachery

    ReplyDelete
  3. You are providing a post is very useful for develop my knowledge and I learn more info from your blog.
    Digital Marketing Course In Kolkata
    Web Design Course In Kolkata
    SEO Course In Kolkata

    ReplyDelete
  4. Way cool! lap Some very valid points! I appreciate you penning this write-up plus the rest of the website is really good.

    ReplyDelete
  5. Hello! I just now would like to supply a massive thumbs up for any wonderful information you could have here within this post. We are coming back to your blog post for further soon. good luck
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete

  6. Thanks of sharing this post…Python is the fastest growing language that helps to get your dream job in a developing area. It says every fundamental in a programming, so if you want to become an expertise in python get some training





    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery





    ReplyDelete
  7. Salesforce CRM software will help companies have a brief overview about how their competitors in the similar niche are performing and thereby helping them in developing a solution that would help in efficient management. Salesforce interview questions and answers

    ReplyDelete
  8. https://designingcourses.in/
    Very Informative and useful... Keep it up the great work. I really appreciate your post.

    graphic designing courses in Bangalore
    web designing course in Bangalore
    UX Design course in Bangalore

    ReplyDelete
  9. Thank you for sharing your article. Great efforts put it to find the list of articles which is very useful to know, Definitely will share the same to other forums. Amazon Web Services Training in Chennai

    ReplyDelete
  10. MAJOR168 is open for football betting today. There are many big camps together BTi SBOBET IBCBET CMD365 if you are looking for a football betting website. Do not miss this site, there is football, there are all sports in the world. คาสิโนออนไลน์. Betting is available 24 hours a day with the best odds per pair in Thailand. Guaranteed automatic deposit and withdrawal system 10 seconds.

    Live sports betting Online football betting Good price with every football match open for today online football betting SAGAME88 There are many big camps together, SBOBET IBCBET BTi CMD365, the only website complete in online football betting คาสิโนออนไลน์. There is every sport on the planet in here. With the automatic deposit and withdrawal system for 10 seconds, we have a live football system to watch every night.


    We offer a wide variety of services. Called him the only player to finish with everything else does not have to go to the web preview ufabet as online. Online casinos Baccarat online Online betting games, Slot online, and with new technology, you can play ufabet via mobile phone today. Mobile Baccarat, play online via the website


    Ufabet1688 of us again the way we are websites directly, not through a General Services, where customers will know it absolutely was extremely really no cheating possible on-site gambling online, it is ufabet1688 of us will hit prices.

    ReplyDelete
  11. Choose to bet on football with us There are more than 100 different sports to choose from, playing live every match for you to have fun 24 hours a day. There is an online casino, Baccarat, สมัคร ufa, Dragon Tiger, Sa Casino Sexy Bacarat, live broadcast directly to your hand 24 hours a day.

    ReplyDelete
  12. https://salsabeelahmedandco.com/
    Very Informative and useful... Keep it up the great work. I really appreciate your post.

    https://salsabeelahmedandco.com/company-registration-in-bangalore/
    https://salsabeelahmedandco.com/ca-firms-in-bangalore/
    https://salsabeelahmedandco.com/accounting-services-in-bangalore/
    https://salsabeelahmedandco.com/gst-consultants-in-bangalore/
    https://salsabeelahmedandco.com/public-limited-company/
    https://salsabeelahmedandco.com/private-limited-company-in-india/
    https://salsabeelahmedandco.com/one-person-company-in-india/
    https://salsabeelahmedandco.com/partnership-firm-in-bangalore/
    https://salsabeelahmedandco.com/limited-liability-partnership-llp/

    ReplyDelete
  13. Ucuz, kaliteli ve organik sosyal medya hizmetleri satın almak için Ravje Medyayı tercih edebilir ve sosyal medya hesaplarını hızla büyütebilirsin. Ravje Medya ile sosyal medya hesaplarını organik ve gerçek kişiler ile geliştirebilir, kişisel ya da ticari hesapların için Ravje Medyayı tercih edebilirsin. Ravje Medya internet sitesine giriş yapmak için hemen tıkla: ravje.com

    İnstagram takipçi satın almak için Ravje Medya hizmetlerini tercih edebilir, güvenilir ve gerçek takipçilere Ravje Medya ile ulaşabilirsin. İnstagram takipçi satın almak artık Ravje Medya ile oldukça güvenilir. Hemen instagram takipçi satın almak için Ravje Medyanın ilgili sayfasını ziyaret et: instagram takipçi satın al

    Tiktok takipçi satın al istiyorsan tercihini Ravje Medya yap! Ravje Medya uzman kadrosu ve profesyonel ekibi ile sizlere Tiktok takipçi satın alma hizmetide sunmaktadır. Tiktok takipçi satın almak için hemen tıkla: tiktok takipçi satın al

    İnstagram beğeni satın almak için Ravje medya instagram beğeni satın al sayfasına giriş yap, hızlı ve kaliteli instagram beğeni satın al: instagram beğeni satın al

    Youtube izlenme satın al sayfası ile hemen youtube izlenme satın al! Ravje medya kalitesi ile hemen youtube izlenme satın almak için tıklayın: youtube izlenme satın al

    Twitter takipçi satın almak istiyorsan Ravje medya twitter takipçi satın al sayfasına tıkla, Ravje medya güvencesi ile organik twitter takipçi satın al: twitter takipçi satın al

    ReplyDelete

Post a Comment