How to get the IP address of a system using Android phone? - java

How to get the IP address of the PC using Android phone? (In particular how to fetch the IP address of a system with a specific MAC address, connected on the same network as the Android phone)?
wired wired
modem--------router---------PC(mac:EE:00:B3:2F:56:12)
|
|
|
wireless
|
|
|
-------------android phone(A)

private String getIP(String mac) {
String ip = null;
try {
BufferedReader br = new BufferedReader(new FileReader("/proc/net/arp"));
String line = "";
while((line = br.readLine()) != null) {
String[] tokens = line.split("\\s+");
// The ARP table has the form:
// IP address HW type Flags HW address Mask Device
// 192.168.178.21 0x1 0x2 00:1a:2b:3c:4d:5e * tiwlan0
if(tokens.length >= 4 && tokens[3].equalsIgnoreCase(mac)) {
ip = tokens[0];
break;
}
}
br.close();
}
catch(Exception e) { Log.e(TAG, e.toString()); }
return ip;
}
But beware: Unless you have already established contact with your PC (and you'll need either its IP address or its name), the ARP table will be empty.
I suppose you'd like to do it the other way round, establish the connection with the PC knowing only its MAC address. Then it's not that simple. You might try to ping everyone on the local network (Runtime.getRuntime().exec("ping -b 192.168.178.255");) just to fill the ARP table.
Or, maybe, you can get the list of all clients with their IP addresses from your router?

Related

Is there is anyway to get host Mac and ip address inside docker container using java progams

I have tried below program on System it returns the host IP address but when running as docker container its give container IP
public static Map<String, String> getMAC(){
Map<String, String> dataMap = new HashMap<>();
try{
InetAddress inetaddress=InetAddress.getLocalHost(); //Get LocalHost refrence
String ip = inetaddress.getHostAddress(); // Get Host IP Address
dataMap.put("ip", ip);
//get Network interface Refrence by InetAddress Refrence
NetworkInterface network = NetworkInterface.getByInetAddress(inetaddress);
byte[] macArray = network.getHardwareAddress(); //get Harware address Array
StringBuilder str = new StringBuilder();
// Convert Array to String
for (int i = 0; i < macArray.length; i++) {
str.append(String.format("%02X%s", macArray[i], (i < macArray.length - 1) ? "-" : ""));
}
String macAddress=str.toString();
dataMap.put("mac", macAddress);
//return dataMap; //return MAc Address
}
catch(Exception E){
E.printStackTrace(); //print Exception StackTrace
//return null;
}
return dataMap;
}
According to Docker docs, you can get the host's IP address by resolving host.docker.internal. So try changing the first line in the try block to:
InetAddress inetaddress=InetAddress.getByName("host.docker.internal");
This is for development purpose and will not work in a production
environment outside of Docker Desktop for Mac.

How can I get MAC Address without using the IP in Java?

How can I found the MAC address of a network card without knowing and using the IP address in Java?
you can use NetworkInterface.getHardwareAddress. You can get the list of all network cards with NetworkInterface.getNetworkInterfaces().
you can use this code for knowing MAC Address.
InetAddress address = InetAddress.getLocalHost();
NetworkInterface nwi = NetworkInterface.getByInetAddress(address);
byte mac[] = nwi.getHardwareAddress();
System.out.println(mac);
You can use system command like below(if you on nix systems, if your os is windows use ipconfig in a similar way):
Runtime r = Runtime.getRuntime();
Process p = r.exec("ifconfig -u |grep ether");
p.waitFor();
BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = b.readLine()) != null) {
System.out.println(line);
}
b.close();
On my system it gives:
ether 28:37:37:15:3b:31
ether b2:00:10:65:56:41
...
Notice that there are maybe many MAC addresses, the Bluetooth, the Ethernet, the WIFI and so on.

Find the MAC address of all devices connected to the same Wifi network

I'm trying to build a software that checks what devices are connected to my home network and return a list of those device's MAC address every 10 minutes or so.
My approach was to ping all of the possible IP addresses on the network and call "arp -a" afterwards.
The following code works to find if a device is registered on an IP address, but I don't know how to get the MAC address from this.
try {
String currentIP = InetAddress.getLocalHost().toString();
String subnet = getSubnet(currentIP);
System.out.println("subnet: " + subnet);
for (int i=1;i<254;i++){
String host = subnet + i;
System.out.println("Checking :" + host);
if (InetAddress.getByName(host).isReachable(timeout)){
System.out.println(host + " is reachable");
try {
Socket connected = new Socket(subnet, port);
}
catch (Exception s) {
System.out.println(s);
}
}
}
}
catch(Exception e){
System.out.println(e);
}
Any suggestions?
You're blindly assuming IPV4, which is not so reasonable anymore these days.
And you're trying to dig out information that routers and access points have no good reason for disclosing in the first place (at least not to robots who won't authenticate themselves as an admin with access rights to the router's or access point's admin page).
Try this one.
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
public class App{
public static void main(String[] args){
InetAddress ip;
try {
ip = InetAddress.getLocalHost();
System.out.println("Current IP address : " + ip.getHostAddress());
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
System.out.print("Current MAC address : ");
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();
}
}
}
This NetworkInterfaceNetworkInterface.getHardwareAddress() method is only allowed to access localhost MAC address, not remote host MAC address.
Note : You can't do this in remotely for PC or Device in java.But you can do it by using C# and WMI - WMI with netowrk in .NET language

Getting the Host name of a Local IP address (java)

I use this method in a loop to get the host name of 4 terminal in my local network identified by the ip address terminal[i].getIp() .
try {
// Get hostname by textual representation of IP address
InetAddress addr = InetAddress.getByName(terminal[i].getIp());
// Get the host name
String hostname = addr.getHostName();
} catch (UnknownHostException e) {
}
the problem here its that code take so long to return the result (up to 5 seconds)
I wonder if there is another more optimized method.
Try the library from Google Guava, I think there are more optimized
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/net/InetAddresses.html

Finding mac Address using IP address in java

public static void main(String[] args) {
try {
InetAddress address = InetAddress.getLocalHost();
// InetAddress address = InetAddress.getByName("192.168.46.53");
/*
* Get NetworkInterface for the current host and then read the
* hardware address.
*/
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
if (ni != null) {
byte[] mac = ni.getHardwareAddress();
if (mac != null) {
/*
* Extract each array of mac address and convert it to hexa with the
* following format 08-00-27-DC-4A-9E.
*/
for (int i = 0; i < mac.length; i++) {
System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
}
} else {
System.out.println("Address doesn't exist or is not accessible.");
}
} else {
System.out.println("Network Interface for the specified address is not found.");
}
I'm having a problem finding the MAC address of a remote host, but I am able to find the MAC address of my local host. If I have the IP address of the other system can I retrieve the MAC address of that system?
InetAddress address = InetAddress.getByName("192.168.46.53");
if i specify the ip address of a system in my workgroup... ni values gets null.... and not able to fetch it.... but if give my ip address of my system...it fetches???
Thanks,
Sunny
You will only be able to fetch the MAC address of remote hosts on your local LAN, that is, hosts that are in the same subnet as your computer. MAC addresses of hosts more than one hop away (IP hop, not Ethernet hop) cannot be determined.
And note that fetching the corresponding MAC address for hosts on your local LAN requires the permissions necessary to fetch either the ARP table, or those necessary to send and receive raw packets. Most OSes allow reading of the ARP table without special permissions, but the mechanism you use to do so will change depending on the OS. If you need a technique for a particular OS, you will have to update your question to include that info.

Categories