© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
Quick Links
Easy Studies

Getting IP and MAC Address of our system and Updating the ARP Table Using Java


This program is to get the IP address and MAC address of a system and updating those details into the ARP table.The fields of an ARP table are Systemname, IPaddress, MACAddress, Time. InetAddress.getLocalHost() mthod returns the system name and ip address of that system. getHardwareAddress() method is to get the MAC address of the System. Every system in a network will have its own ARP table and it will have the IPAddress and MAC address of the Sysem.In this program, we are getting the IPAddress and MAC Address of the system and updating it in the ARP table.

Getting IP and MAC Address of our system and Updating the ARP Table Using Java:

import java.net.*; import java.util.*; import java.sql.*; public class GettingMAC{ public InetAddress ipaddr; public String ipaddress; String Macaddress,Sysname,Sysipaddr; public void getAddress(){ try{ ipaddr = InetAddress.getLocalHost(); System.out.println("IPAddress"+ipaddr); System.out.println(); ipaddress=ipaddr.toString(); //Get the MACAddress of the System NetworkInterface ni = NetworkInterface.getByInetAddress(ipaddr); byte[] mac = ni.getHardwareAddress(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < mac.length; i++) { String s = ""+mac[i]; if(i < mac.length - 1){ s = ""+mac[i]; } int j=Integer.parseInt(s); String s1=Integer.toHexString(j); if(s1.length()>2){ int l=s1.length()-2; s1 = s1.substring(l); } if(i != 0){ s1 = "-"+s1; } sb.append(s1); } Macaddress=sb.toString(); System.out.println("MACAddress"); //Displaying MAC Address at the Client Side for (int i = 0; i < mac.length; i++) { System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""); } }catch(Exception e){ System.out.println("Exception"+e); } System.out.println(); } public void updateDB(){ int x=0; StringTokenizer str=new StringTokenizer(ipaddress,"/"); Sysname=str.nextToken(); Sysipaddr=str.nextToken(); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:ARPcl"); Statement st=con.createStatement(); Statement st1=con.createStatement(); ResultSet rs,rs1; String query="select IPAddress from ARPclient where IPAddress='"+Sysipaddr+"'"; rs=st1.executeQuery(query); while(rs.next()){ x++; } if(x<=0){ java.util.Date d=new java.util.Date(); int hr=d.getHours(); int min=d.getMinutes(); int tot=(hr*60)+min; int a=st.executeUpdate("insert into ARPclient values('"+Sysname+"','"+Sysipaddr+"','"+Macaddress+"',"+tot+")"); System.out.println("Data Updated Successfully"); } st.close(); con.close(); } catch(Exception e){ System.out.println("Exception"+e); } } public static void main(String args[]){ GettingMAC gmac=new GettingMAC(); gmac.getAddress(); gmac.updateDB(); } }

Sample ScreenShot:

SLogix Student Projects

⇓Student Projects⇓
⇑Student Projects⇑