How to get the list of devices connected to the network

17

I need to find the devices connected to the network where my program runs. It's a WiFi network (I do not know if that changes the difficulty of the thing), where I know there are at least two devices connected to the router.

Does anyone know if it's possible to do this in Java? Maybe via multicast? (multicast question because in the router there is information that there is a multicast group configured with the address 239.255.255.250 ).

    
asked by anonymous 30.08.2014 / 18:38

2 answers

16

A good starting point should be this project, written in Java:

  

Android network tool: Discover hosts and scan their ports, in your   Wifi / 3G network.    link

Features

  • Discovering devices on the local network (connect / ping discovery, dns discovery)
  • TCP port scanner (connect () scan)
  • NIC database
  • Export results to SDCard in XML
  • Accessing Wifi Settings
  • Adaptive Scan Speed (slow start, then adapts to network latency)
  • Open Source Home |

A simple implementation alternative would also dump (ICMP) all devices in your subnet, as in the example using java.net.InetAddress :

import java.io.IOException;
import java.net.InetAddress;

public class NetworkPing {
    public static void main(String[] args) throws IOException {

        InetAddress localhost = InetAddress.getLocalHost();
        // uso de IPv4 e assumido!
        byte[] ip = localhost.getAddress();

        for (int i = 1; i <= 254; i++) {
            ip[3] = (byte) i;
            InetAddress address = InetAddress.getByAddress(ip);
            if (address.isReachable(1000)) {
                System.out.println(address + " maquina esta ligada e pode ser pingada");
            } else if (!address.getHostAddress().equals(address.getHostName())) {
                System.out.println(address + " maquina reconhecida por um DNSLookup");
            } else {
                System.out.println(address + " o endereço de host e o nome do host são iguais, o host name não pode ser resolvido.");
            }
        }

    }
}


source code obtained from: link . Home Complementing my answer, in relation to you attempting a discovery of network devices trying to exploit using the multicast address , I think one more complicated approach, it is designed to communicate using datagrams , ie there is no connection and there will be no response ( acknowledgment ) of your receipt. Home You can explore Multicasting and Datagrams in java using DatagramSocket , DatagramPacket and MulticastSocket .

Oracle reference documentation using datagrams : link

UDP PortScanner: link

    
25.07.2015 / 21:06
8

I'm going to address and describe methods that I use to get information from devices connected to the wifi network, no doubt ping is an alternative, but if the device has any firewall or anti-virus that blocks ICMP you will never get a return, so does any type of scan that tries to scan your network. In addition to being slow you have a great chance of having some equipment browsing your network in which the scan can not catch.

The safest way to bring correct information is to go straight to the source (ie your router), you can enable queries via SNMP in your router / access point, I believe it is very rare that a network equipment does not have this option nowadays, this way you can consult any information distributed by the equipment, such as bandwidth consumption, CPU utilization, memory used and it is very likely that some OID (is the identifier, a number that collects information for a given need) returns a list of MAC addresses of the devices connected to it.

Here is a real example of usage:

snmpwalk -v2c -c senhaSNMPAQUI 192.168.1.214 enterprises.171.11.37.4.4.5.2.1.2.1

SNMPv2-SMI::enterprises.171.11.37.4.4.5.2.1.2.1.1 = Hex-STRING: 2X V6 98 XX XX XX
SNMPv2-SMI::enterprises.171.11.37.4.4.5.2.1.2.1.2 = Hex-STRING: A1 52 66 XX XX XX
SNMPv2-SMI::enterprises.171.11.37.4.4.5.2.1.2.1.3 = Hex-STRING: 51 SF 6D XX XX XX

Of course I've gotten the information in this example using a unix client called snmpwalk , the parameters will always be versão SNMP to comunity (type a password to access), IP ad address of your router and OID that brings the information you need, each device has specific OIDs and most of the time you will need to get this information in the documentation provided by the manufacturer, to my Dlink here the OID that brings the equipment information connected to it is enterprises.171.11.37.4.4.5.2.1.2.1 , OK you can do the same using java libs to do snmp query, I believe you will find several, one of them and example is SNMP4J . Hi, I have a problem with my computer.

One way is to send IP to the network and get the return of the broadcast table

arp -a

? (192.168.1.11) at A1 52 66 XX XX XX on eth0 expires in 931 seconds [ethernet]
? (192.168.1.52) at 2X V6 98 XX XX XX on eth0 expires in 1169 seconds [ethernet]
? (192.168.1.17) at 51 SF 6D XX XX XX on eth0 expires in 650 seconds [ethernet]

Is my machine out of China and does it have SNMP or does it have OID's SNMP to bring the information I need right now?

Enable your remote access (ssh, telnet) method on your router, plug in the device, and find some command inside it that takes the information you need

Another real example:

telnet 192.168.2.1
Trying 192.168.2.1...
Connected to 192.168.2.1.
Escape character is '^]'.
login: usuariodorouter
Password: ******
WAP-> get clientinfo
Client1--time:1424
Client1--ssid: primary SSID
Client1--mac:CW:45:67:XX:XX:XX
Client1--auth:OPEN
Client1--rssi:11
Client1--mode:11n
Client1--psmode:0
Client1--rx_bytes:234239
Client1--tx_bytes:285309

In this example a telnet connection was made to the router and I ran the command arp , again you will need to consult some doc from the manufacturer to know which command returns what you need, in java there are also telnet client libs which make this connection and iteration for sending commands, all you have to do is collect the data ...

    
29.07.2015 / 14:24