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:
- Get IP from Android device (Checked)
- Filter it, if its a private IP starting with "10." && "10.0.0." OR "192." && "192.168." and so on (Checked)
- 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;
}
}
});
}