JAVA to obtain local, remote macAddress

Source: Internet
Author: User

Obtain local macAddress
Java code
Import java. io. InputStreamReader;
Import java. io. LineNumberReader;
 
 
Public class LocalMacAddr
{
Public String getMACAddr ()
{
String mac = "";
Try
{
Process process = runtime.getruntime(cmd.exe c ("ipconfig/all ");
InputStreamReader ir = new InputStreamReader (process. getInputStream ());
LineNumberReader input = new LineNumberReader (ir );
String line = null;
While (line = input. readLine ())! = Null)
{
If (line. indexOf ("Physical Address")> 0)
{
Mac = line. substring (line. indexOf ("-")-2 );
}
}
}
Catch (Exception e)
{
E. printStackTrace ();
}
Return mac;
}

Public static void main (String [] args)
{
LocalMacAddr ma = new LocalMacAddr ();
System. out. println (ma. getMACAddr ());
}
 
}
 
Obtain remote macAddress
Java code
Import java. io. IOException;
Import java.net. DatagramPacket;
Import java.net. DatagramSocket;
Import java.net. InetAddress;
 
Public class RemoteMacAddr
{
Private String sRemoteAddr;
Private int iRemotePort = 137;
Private byte [] buffer = new byte [1, 1024];
Private DatagramSocket ds = null;
 
Public RemoteMacAddr (String strAddr) throws Exception
{
This. sRemoteAddr = strAddr;
This. ds = new DatagramSocket ();
}
 
Private writable rampacket send (byte [] B) throws IOException
{
Required rampacket dp = new DatagramPacket (B, B. length, InetAddress. getByName (sRemoteAddr), iRemotePort );
Ds. send (dp );
Return dp;
}
 
Private writable rampacket receive () throws Exception
{
DatagramPacket dp = new DatagramPacket (buffer, buffer. length );
Ds. receive (dp );
Return dp;
}
 
// Query the package structure:
// Transaction ID two bytes (16 bits) 0x00 0x00
// Flags two bytes (16 bits) 0x00 0x10
// Questions two-byte (16-bit) 0x00 0x01
// AnswerRRs two bytes (16 bits) 0x00 0x00
// AuthorityRRs two bytes (16 bits) 0x00 0x00
// AdditionalRRs two-byte (16 bits) 0x00 0x00
// Name: array [1 .. 34] 0x20 0x43 0x4B 0x41 (30) 0x00;
// Type: NBSTAT two bytes 0x00 0x21
// Class: INET two bytes (16 bits) 0x00 0x01
Private byte [] GetQueryCmd () throws Exception
{
Byte [] t_ns = new byte [50];
T_ns [0] = 0x00;
T_ns [1] = 0x00;
T_ns [2] = 0x00;
T_ns [3] = 0x10;
T_ns [4] = 0x00;
T_ns [5] = 0x01;
T_ns [6] = 0x00;
T_ns [7] = 0x00;
T_ns [8] = 0x00;
T_ns [9] = 0x00;
T_ns [10] = 0x00;
T_ns [11] = 0x00;
T_ns [12] = 0x20;
T_ns [13] = 0x43;
T_ns [14] = 0x4B;
For (int I = 15; I <45; I ++)
{
T_ns [I] = 0x41;
}
T_ns [45] = 0x00;
T_ns [46] = 0x00;
T_ns [47] = 0x21;
T_ns [48] = 0x00;
T_ns [49] = 0x01;
Return t_ns;
}
 
// Table 1 structure and main fields of the "UDP-NetBIOS-NS" response packet
// The length of the serial number field name
// 1 Transaction ID two bytes (16 bits)
// 2 Flags two bytes (16 bits)
// 3 Questions two-byte (16 bits)
// 4 AnswerRRs two-byte (16 bits)
// 5 AuthorityRRs two-byte (16 bits)
// 6 AdditionalRRs two-byte (16 bits)
// 7 Name <Workstation/Redirector> 34 bytes (272 bits)
// 8 Type: NBSTAT two-byte (16 bits)
// 9 Class: INET two bytes (16 bits)
// 10 Time To Live four bytes (32 bits)
// 11 Length two bytes (16 bits)
// 12 Number of name one byte (8 bits)
// NetBIOS Name Info 18 × Number Of Name byte
// Unit ID 6 bytes (48 bits)
Private String GetMacAddr (byte [] B) throws Exception
{
// Obtain the computer name
// System. out. println (new String (B, 57, 18 ));
// System. out. println (new String (B, 75, 18 ));
// System. out. println (new String (B, 93, 18 ));
Int I = B [56] * 18 + 56;
String sAddr = "";
StringBuffer sb = new StringBuffer (17 );
// Read the Number Of Names (the Number Of NetBIOS Names, each Of which is NetBIOS Names
// The Info part occupies 18 bytes)
// You can then calculate the position Of the "Unit ID" field = 56 + Number
// Names × 18. The last six bytes are read from this position consecutively, that is, the MAC address of the target host.
For (int j = 1; j <7; j ++)
{
SAddr = Integer. toHexString (0xFF & B [I + j]);
If (sAddr. length () <2)
{
Sb. append (0 );
}
Sb. append (sAddr. toUpperCase ());
If (j <6)
Sb. append ('-');
}
Return sb. toString ();
}
 
Private void close ()
{
Try
{
Ds. close ();
}
Catch (Exception ex)
{
Ex. printStackTrace ();
}
}
 
Public String GetRemoteMacAddr () throws Exception
{
Byte [] bqcmd = GetQueryCmd ();
This. send (bqcmd );
DatagramPacket dp = this. receive ();
String smac = GetMacAddr (dp. getData ());
This. close ();
Return smac;
}
 
Public static void main (String [] args) throws Exception
{
RemoteMacAddr addr = new RemoteMacAddr ("192.168.1.119 ");
System. out. println (addr. GetRemoteMacAddr ());
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.