I am trying to develop an open-source network monitoring application
The application scan the network to find all connected devices. It shows all IPs in the given range but the connected devices will appear with checked box (JCheckBox). I list the connected devices in a JTable with information about each device such as name or IP address using address.getHostName() and address.getHostAddress() -Methods in the class InetAddress-
Now I am trying to get the physical address (MAC). I tried this code but the application shows the local device only; the devices (connected or disconnected) in the given IPs range did not appear.
byte[] mac = NetworkInterface.getByInetAddress(address).getHardwareAddress();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
String MAC = sb.toString();
To request MAC (Hardware) address you need to make ARP lookup.
Answer to appropriate question may help you.
There are several ways defined there:
arp -a lookup (UNIX-specific way)
SNMP request
Send your own ARP request packet
Anyway you can get MAC address that belongs only to devices within your local network.
Related
I am trying to program an android app dashboard(latest Java android 5.1 lollipop) that would be able to control my govee lamp which has a LAN control api which connects to a multicast address on my LAN(239.255.255.250) but I need a way to find the IP of the specific lamp so that I can send it commands after user interaction on my app.
Pinged the multicast IP and got no response(operation timed out). Looked around google for a way but couldn't find anything so wondering if someone here could help me out. TY for your time :)
Assuming you know the mac address you can send a reverse ARP packet (Reverse Address Resolution Protocol) to obtain the IP address. ARP itself is normally used to map an IP address to the mac address so the local router can properly forward the packet. RARP does the opposite.
Another way is to interrogate the local ARP cache from within the program on any attached device and see if that has it. So on a windows OS within the program you can initiate an arp -a in the console window to see the current arp cache. The only caveat is that the particular value may have time out and been removed.
Here is an example of the latter.
String mac = "1c-7e-81-9e-48-e8";
try {
Runtime rt = Runtime.getRuntime();
// send broadcast address
rt.exec(new String[]{
"ping","-n 1 -w 200 192.255.255.255"
});
// interrogate arp cache
Process p = rt.exec(new String[]{
"cmd.exe","/c","arp","-a"
});
BufferedReader in = new BufferedReader(
new java.io.InputStreamReader(p.getInputStream()));
String line1;
while ((line1 = in.readLine()) != null) {
if (line1.contains(mac)) {
System.out.println(line1);
}
}
} catch (Exception e) {
e.printStackTrace();
}
prints something like
192.168.1.107 1c-7e-81-9e-48-e8 dynamic
The above may need to be adjusted to fulfill your exact requirements.
Of course the easiest way might be to force the issue and assign a static address of your choosing.
I've created a Java GUI which lists all serial ports in a drop down menu from which the user selects the correct port and clicks connect. Connection to an Arduino is then established and the user is able to perform some actions. I get the available ports using Fazecast JSerialComm:
SerialPort[] ports = SerialPort.getCommPorts();
I grab the ports and put the results into the drop down. This works flawlessly BUT only when the Arduino is plugged into the Mac BEFORE launching my Java GUI. Is there a way to detect a hotplugged device in Java? I already thought of getting the com ports periodically (every second or so) but to me that does seem to be a very elegant solution.
update:
I found this answered by another user:
Serial communication manager have APIs to find serial port names like COMxx dynamically. Just connect your USB-USRT IC and SCM library will tell you the device node for it. Just google for Serial communication manager. It is hosted on GitHub.
src
You might do this by checking if the port is opened or not after trying to open it using:
SerialPort[] serialPorts = SerialPort.getCommPorts();
SerialPort liveSerialPort = null;
for (SerialPort p: serialPorts) {
p.openPort();
if (p.isOpen()) {
liveSerialPort = p;
System.out.println("HERE opened port = " + liveSerialPort.getSystemPortName());
break;
}
}
I'm trying to write a program that will show a DHCP client list in Java. I want to get the IP addresses, MAC addresses and the host names of all the devices connected to my wifi network.
I have a Belkin router. Its homepage has a 'DHCP client list' option which when clicked shows me this table :
That's exactly what I'm looking for. But I want to show all this data in the form of a list in a Java Swing program. I also want to be able to update this list by pressing a refresh button. Is there any way to achieve this?
It should look something like this :
I've written a basic java program that shows all the IP addresses that are online. Here's the code :
public static void main(String[] args) throws IOException {
InetAddress localhost = InetAddress.getLocalHost();
// this code assumes IPv4 is used
byte[] ip = localhost.getAddress();
for (int i = 1; i <= 254; i++)
{
ip[3] = (byte)i;
InetAddress address = InetAddress.getByAddress(ip);
if (address.isReachable(1000))
{
// machine is turned on and can be pinged
System.out.println(address + "is online");
}
else if (!address.getHostAddress().equals(address.getHostName()))
{
// machine is known in a DNS lookup
System.out.println(address + "is in a DNS lookup");
}
else
{
// the host address and host name are equal, meaning the host name could not be resolved
System.out.println(address + " is not online");
}
}
}
But this doesn't serve the purpose and it's really slow. I want to write a Swing program that'll show me the DHCP client list as seen in the image above.
Any help is appreciated.
I would think about three possible alternatives:
1-The one you implemented that is slow but it can work. You need to find a JAVA API to get the MAC addresses of received messages (I don't know if it exists or not). You can also send ARP messages asking "who has this IP address) and obtain the MAC address from the response. Use some Java interface for pcap library: jNetPcap vs Jpcap , http://jnetpcap.com/
2-Create an app that accesses your router web interface using HTTP and sending the appropriate messages with data as if you were using the UI. In this way you can programatically follow the steps a human would go and get the list that you browser shows, parse it and obtain the data.
3-If the router/access point provides a web API, which I doubt, you can use it.
iam just simply trying to know what are the input devices and output devices connected to the system. Do there any api to get information about input/output devices using java?
EDIT
independent of any operating system.
Please suggest.
To find what usb devices are connected to the system you can use jUSB. This article has more in-depth information on how to use the api. In particular, to find all usb devices (slightly modified from the article):
Host host = HostFactory.getHost();
// Obtain a list of the USB buses available on the Host.
Bus[] bus = host.getBusses();
// Traverse through all the USB buses.
for (int i = 0; i < bus.length; i++) {
// Access the root hub on the USB bus and obtain the number of USB ports available on the root hub.
Device root = bus[i].getRootHub();
int totalPorts = root.getNumPorts();
// Traverse through all the USB ports available on the
// root hub. It should be mentioned that the numbering
// starts from 1, not 0.
for (int j=1; j<=total_port; j++) {
// Obtain the Device connected to the port.
Device device = root.getChild(j);
if (device != null) {
// USB device available, do something here.
}
}
}
Similarly, you can use the api for MIDI systems to find what MIDI devices are connected, see the java tutorials for more information.
I think you should try commandline via java Runtime.getRuntime().exec(command);
According to this site http://michaelminn.com/linux/command_line
Following command should return this:
lspci: Lists information about devices connected to the internal PCI busses.
lspci -vv: Full dump of information.
so
String command="lspci";
And from windows you may need to download DevCon command! http://support.microsoft.com/kb/311272/EN-US
so
String command="devcon find";
Anyone could you please help me to list the name of all computers connected to the LAN with JAVA?
One way you could solve this without using outside system calls would be to try every single possible IP address.
Iterate over addresses, sending data to each one using something like this...
for (int i =0; i<100; i++) {
String ip = "192.168.1." + i
InetAddress address = InetAddress.getByAddress(ip);
}
Any responses you get would indicate an active IP. You will be a limited by your subnet in this approach, however.