ARP - RARP Protocol

 // ARP - RARP Protocol



import java.io.*;

import java.util.*;

 

class ARP_RARP

{ 

    public static void main(String args[]) throws Exception

    {

        Scanner sc=new Scanner(System.in);

     

         // Read current ARP table from OS

        HashMap<String,String> arpTable = new HashMap<>();

                 

         arpTable.clear();

         ProcessBuilder pb = new ProcessBuilder("arp", "-a");

        Process p = pb.start();

 

        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

         String line;

         while((line=br.readLine())!=null)

        {

            line=line.trim();

 

            // Ignore headings

            if(line.startsWith("Interface") ||

                   line.startsWith("Internet") ||

                   line.length()==0)

                   continue;

 

            String parts[]=line.split("\\s+");

 

            if(parts.length>=3)

            {

                String ip=parts[0];

                String mac=parts[1];

 

                arpTable.put(ip,mac);

            }

        }

        br.close();

 

       System.out.println("ARP Table");

        for(Map.Entry<String,String> e:arpTable.entrySet())

        {

             System.out.printf("%-20s %s\n",  e.getKey(), e.getValue());

        }

                 

                 

      // ARP : IP -> MAC  

      System.out.print("Enter IP : ");

        String ip = sc.nextLine();

                 

      if(arpTable.containsKey(ip))

            System.out.println("MAC Address : "+arpTable.get(ip));

       else

            System.out.println("IP not found.");

                 

                  // RARP : MAC -> IP

                  System.out.print("Enter MAC : ");

                  String mac = sc.nextLine();

                 

                  boolean found=false;

                for(Map.Entry<String,String> e:arpTable.entrySet())

               {

                    if(e.getValue().equalsIgnoreCase(mac))

                     {

                            System.out.println("IP Address : "+e.getKey());

                            found=true;

                    }

                }

            if(!found)

                    System.out.println("MAC not found.");

         }

}



/* 

Sample output


>javac ARP_RARP.java

>java ARP_RARP

ARP Table

239.255.255.250      01-00-5e-7f-ff-fa

255.255.255.255      ff-ff-ff-ff-ff-ff

10.58.180.155        4a-e1-2f-34-53-1b

10.58.180.255        ff-ff-ff-ff-ff-ff

224.0.0.252          01-00-5e-00-00-fc

224.0.0.251          01-00-5e-00-00-fb

224.0.0.22           01-00-5e-00-00-16


Enter IP : 224.0.0.251

MAC Address : 01-00-5e-00-00-fb


Enter MAC : ff-ff-ff-ff-ff-ff

IP Address : 255.255.255.255

IP Address : 10.58.180.255


>java ARP_RARP

ARP Table

239.255.255.250      01-00-5e-7f-ff-fa

255.255.255.255      ff-ff-ff-ff-ff-ff

10.58.180.155        4a-e1-2f-34-53-1b

10.58.180.255        ff-ff-ff-ff-ff-ff

224.0.0.252          01-00-5e-00-00-fc

224.0.0.251          01-00-5e-00-00-fb

224.0.0.22           01-00-5e-00-00-16


Enter IP : 127.0.0.1

IP not found.


Enter MAC : ad-aa-ca-bb-cc-aa

MAC not found.


*/

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu