Reputation: 51
I am able to get local MAC address using the below code
package com.eiw.server;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
class FindMACAddress {
public static void main(String[] args) {
InetAddress ip;
try {
ip = InetAddress.getLocalHost();
System.out.println("The mac Address of this machine is :"
+ ip.getHostAddress());
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
System.out.print("The mac address is : ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i],
(i < mac.length - 1) ? "-" : ""));
}
System.out.println(sb.toString());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
}
}
}
but I Need to find the remote system Mac Address... Whether it is possible? I already gone through some of the posts...but not clear....
Upvotes: 3
Views: 11571
Reputation: 819
You can get Client IP address and MAC address using HttpServletRequest
Reference link : Link
public void clientIpAndMacAddress(HttpServletRequest request)
{
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
String userIpAddress = httpServletRequest.getHeader("X-Forwarded-For");
if (userIpAddress == null) {
userIpAddress = request.getRemoteAddr();
}
System.out.println("Ip address : " + userIpAddress);
String str = "";
String macAddress = "";
try {
Process p = Runtime.getRuntime()
.exec("nbtstat -A " + userIpAddress);
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (int i = 1; i < 100; i++) {
str = input.readLine();
if (str != null) {
if (str.indexOf("MAC Address") > 1) {
macAddress = str.substring(
str.indexOf("MAC Address") + 14, str.length());
break;
}
}
}
} catch (IOException e) {
e.printStackTrace(System.out);
}
System.out.println("Mac address : " + macAddress);
}
Upvotes: -1
Reputation: 21
You can get mac addr of remote host calling function getMacAddrHost("192.168.1.xx"). It's maybe not the greatest solution but it works great. Note this works only inside the LAN.
public static String getMacAddrHost(String host) throws IOException, InterruptedException {
//
boolean ok = ping3(host);
//
if (ok) {
InetAddress address = InetAddress.getByName(host);
String ip = address.getHostAddress();
return run_program_with_catching_output("arp -a " + ip);
}
//
return null;
//
}
public static boolean ping3(String host) throws IOException, InterruptedException {
boolean isWindows = System.getProperty("os.name").toLowerCase().contains("win");
ProcessBuilder processBuilder = new ProcessBuilder("ping", isWindows ? "-n" : "-c", "1", host);
Process proc = processBuilder.start();
int returnVal = proc.waitFor();
return returnVal == 0;
}
public static String run_program_with_catching_output(String param) throws IOException {
Process p = Runtime.getRuntime().exec(param);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
if (!line.trim().equals("")) {
// keep only the process name
line = line.substring(1);
String mac = extractMacAddr(line);
if (mac.isEmpty() == false) {
return mac;
}
}
}
return null;
}
public static String extractMacAddr(String str) {
String arr[] = str.split(" ");
for (String string : arr) {
if (string.trim().length() == 17) {
return string.trim().toUpperCase();
}
}
return "";
}
Upvotes: 2
Reputation: 1
private static String getMacAdressByUseArp(String ip) throws IOException {
String cmd = "arp -a " + ip;
Scanner s = new Scanner(Runtime.getRuntime().exec(cmd).getInputStream());
String str = null;
Pattern pattern = Pattern.compile("(([0-9A-Fa-f]{2}[-:]){5}[0-9A-Fa-f]{2})|(([0-9A-Fa-f]{4}\\.){2}[0-9A-Fa-f]{4})");
try {
while (s.hasNext()) {
str = s.next();
Matcher matcher = pattern.matcher(str);
if (matcher.matches()){
break;
}
else{
str = null;
}
}
}
finally {
s.close();
}
return (str != null) ? str.toUpperCase(): null;
}
Upvotes: 0
Reputation: 33
arp -a
will show you active connections. For example:
Interface: 10.0.0.9 --- 0x19
| Internet Address | Physical Address |Type || 10.0.0.1 | c4-3d-c7-68-82-87 | dynamic |
I have awk on this machine so the following will output the MAC address for me. I am also looking for a way to implement this in code (in a way that is system independent).
This might solve what you were looking for (in Java wrap it with something like Process p = Runtime.getRuntime().exec("Enter command here")
):
arp -a | awk "/10.0.0.1/"' { gsub(/-/, "", $2); print toupper($2)}
Output:
C43DC7688287
Upvotes: 0
Reputation: 1314
You can get the MAC address of the other system by standard network means when both systems are in the same network segment (same local area network, no IP-routers in between)
Query ARP cache to get MAC ID seems to answer your question
Upvotes: 0
Reputation: 949
It depends. If you can connect to the remote system, then you can execute ifconfig/ipconfig commands and from the output gauge the mac address of that machine. But if you cannot connect and execute commands on the remote machine, I do not think there's any way to know the MAC address of that machine.
Upvotes: 0