How to get the list of devices connected to the network

Asked

Viewed 4,679 times

17

I need to find the devices connected to the network on which my program runs. It is a network via Wifi (I do not know if this changes the difficulty of the thing), where I know that there are at least two devices connected to the router.

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

  • 2

    Lucas, I don’t know exactly how to do (so I can’t help) because I’ve never dealt with local DNS lookup. I found a question in the EN OS that can help you start sketching the solution: http://stackoverflow.com/questions/13198669/any-way-to-discover-android-devices-on-your-network. The question is very broad. I suggest starting to implement and any doubt carry out a new question with the code you have already made.

  • 1

    This week I need to do something similar, I thank Luiz for the edition, I was almost creating a question here.

  • @re22 I found the question cool and worthy of editing. I’m glad it will possibly help more people. :)

2 answers

17


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. https://github.com/rorist/android-network-discovery

Features

  • Local network device discovery (connect/ping Discovery, dns Discovery)
  • TCP (connect() scan port scanner)
  • NIC database
  • Output to XML Sdcard
  • Access the Wifi settings
  • Adaptive scan speed (slow start, then adapts to network latency)
  • Open Source


A simple implementation alternative would also be drip (ICMP) all devices on your subnet, as in the example using the 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 in: http://www.javaprogrammingforums.com/java-networking/6739-how-get-list-all-computers-my-network-computers-connected-me.html.

Complementing my response, regarding you trying a discovery of the network devices trying to explore using the multicast address, I find a more complicated approach, it is designed for communication using datagrams, or that there is no established connection and there will be no answer (acknowledgment) of the protocol of its receipt.
You can explore Multicasting and Java Datagrams using Apis Datagramsocket, Datagrampacket and Multicastsocket.

Oracle reference documentation using datagrams: https://docs.oracle.com/javase/tutorial/networking/datagrams/broadcasting.html

UDP Portscanner: https://code.google.com/p/portscanner/source/browse/trunk/PortScanner/src/UDPScanner.java

8

I will address and describe methods I use to get information from equipment connected on wifi network, no doubt the ping is an alternative, but if the device has some firewall or anti-virus blocking packages ICMP, you will never get back, the same happens for any type of Scan that tries to scan your network, besides being slow has great chances of having some equipment browsing on your network in which the scan can not catch.

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

Here is a real example of using:

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 picked up the information in this example using a Unix client called snmpwalk, the parameters will always be versão of the SNMP to comunity (type a password for access), the address IP of your router and the OID that brings the information you need, each equipment has specific Oids and most of the time you will need to take this information in documentation provided by the manufacturer, for my Dlink here the OID that brings the information of equipment connected to it is the 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 with example is SNMP4J. Then you ask me in your example have three connected equipments, as I know the IP theirs ?

One of the ways is to send one broadcast network and catch the return of the table arp

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]

My equipment is china and has no SNMP or no SNMP OID’s that bring the information I need and now?

Enable on your router some remote access method (ssh, telnet), connect to 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 on the router and I ran the command get clientinfo, again you will need to query some doc from the manufacturer to know which command returns what you need, in java there are also telnet client libs that make this connection and iteration for sending commands, all you have to do is gather the data...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.