0

Question:

I have this FOR Statement, that calls a Method with different IP Addresses (ranging from 10.0.0.1 to 10.0.0.255 because I don't have an IP scanner). This Method is a Future Callable and returns a Boolean true or false. The function of the Method is to basically make a Socket, connect to this Socket with given IP & port + a timeout, and return true or false based on if given IP address, has the port open or not.

My Plan: I'm trying to Check if a device connected to the same WIFI network has an open port (SSH port) so I can SSH into this device without knowing it's IP-Address. My idea was to get the private IP-Address of the Android device, check if its equal to common private IP-Address (for example 10.0.0.*) and scan it's IP-Range (from 10.0.0.1-255) for the open SSH Port (22).


ToDo:

  1. Get IP from Android device (Checked)
  2. Filter it, if its a private IP starting with "10." && "10.0.0." OR "192." && "192.168." and so on (Checked)
  3. Check if the Port 22 is open or not on the Range "10.0.0.1-255" or "192.168.1.1-255". (Current)

I used code from here for the Port Scanner: Socket Port Scanner In this question the Future Method returned true or false if the port on a fixed IP-Address was open or not. The result of this Method then was added to a Future Boolean List Array. Then queried, the User could see how many ports were open on this fixed IP-Address.

I modified it so the Method was scanning a number of IP-Addresses for a fixed port, and then adding the IP-Addresses, were the SSH Port was open, to the Future Boolean List Array. This works except socket.connect(new InetSocketAddress(ip, port), timeout); always returns true. I tried looking at its code as to why it does that, but I found no answers. If you have any Idea how I could have the Socket return false, when the given port on a IP address is not open, then please let me know. Thanks in Advance.

Code:

Here I call the Socket Method (portIsOpen) and if it returns true, I append the IP Adress that was sent to the Method to my Stringbuilder possibleIPAddresses. This way I can list all the IP-Addresses that have the Port open and then later connect to them.

//Initialisiert Variablen für den Port Prüfer
public StringBuilder possibleIPAddress = new StringBuilder("");
final ExecutorService es = Executors.newFixedThreadPool(50);
final int timeout = 500;
final List<Future<Boolean>> futures = new ArrayList<>();

//Code for querying IP Adress here.

//Checking all IP-Addresses from 10.0.0.1-10.0.0.255 for open SSH Port
String ip = "";
for (int x=1;x<=10;x++) {
    ip = "10.0.0."+x;

    //Should return true if port on IP-Address is open, but returns true everytime..help?
    if (futures.add(portIsOpen(es, ip, 22, timeout))) {
        possibleIPAddress.append(ip).append("\n");
    }

    es.shutdown();

}

And here is the Code for the port scanner (It is the same as in the port scanner question)

public static Future<Boolean> portIsOpen(final ExecutorService es, final String ip, final int port, final int timeout) {
    return es.submit(new Callable<Boolean>() {
        @Override public Boolean call() {
            try {
                Socket socket = new Socket();
                socket.connect(new InetSocketAddress(ip, port), timeout);
                socket.close();
                return true;
            } catch (Exception ex) {
                return false;
            }
        }
    });
}
Community
  • 1
  • 1
Ederbit
  • 205
  • 1
  • 12

1 Answers1

0

Your portIsOpen() method returns a Future, whose result you are never getting, so your method never returns anything at all, because it is never called. You need to add to your list of possible IP addresses if and only if the result of the Future is true.

NB you're also leaking sockets in the case where there is an exception. Use the try-with-resources syntax to fix that.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks for your answer, but when I try this it still returns true everytime: `System.out.println(futures.add(portIsOpen(es, ip, 22, timeout)));`... That means the method is executing the socket and then returns true, so I'm getting the result, no ?. And I do specify that if and only if the result is true, because `if (futures.add(portIsOpen(es, ip, 22, timeout)))` is the same as `if (futures.add(portIsOpen(es, ip, 22, timeout)) == true)`. Could you help me? – Ederbit Jan 06 '16 at 10:43
  • And how would i inplement try-with-resources? I tried this: `try (Socket socket = new Socket(socket.connect(new InetSocketAddress(ip, port), timeout))){ }` but it tells me that the variable socket might not have been initialized. – Ederbit Jan 06 '16 at 10:57