Is there a way to find :
Of a give IP or MAC address in java. I've been give a task to get that data. I'd really appreciate a code sample or if you know of any libraries that can be used. eg :
If I were to use an IP address of a computer on the same network. I would like to know the name of the computer and which OS they are using and other hardware metadata if that's possible.
import java.util.Map;
public class EnvMap {
public static void main (String[] args) {
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
System.out.format("%s=%s%n",
envName,
env.get(envName));
}
}
}
I guess you need the following properties:
COMPUTERNAME
OS
regarding MAC Addrss and IP Address you have 2 options:
use
InetAddress ip;
try {
ip = InetAddress.getLocalHost();
System.out.println("Current IP address : " + ip.getHostAddress());
} catch (UnknownHostException e) {
e.e.printStackTrace();
}
or use ipconfig/ifconfig output but I'll leave the choice it to you
Getting the mac
The proper way to do this is to use the ARP protocol, but that resides at the Data Link Layer to which Java provides no direct API access. So you'd have to go native for a good solution. Use jNetPcap to gain access to this.
Getting computer name and OS
This would require you to tap into some protocol that provides that data. NBT provides this kind of data.
If you're trying to find out the hostname associated with an IP address, use InetAddress#getHostName() , though that will only work if the local network has hosts registered for reverse DNS. Finding out another host's OS requires either a service you run that tells you or inference through fingerprinting , which isn't available from Java and isn't entirely reliable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.