android - Socket Connection always returns true even if Port is not open (Port Scan) -
question:
i have for statement, calls method different ip addresses (ranging 10.0.0.1 10.0.0.255 because don't have ip scanner). method future callable , returns boolean true or false. function of method make socket, connect socket given ip & port + timeout, , return true or false based on if given ip address, has port open or not.
my plan: i'm trying check if device connected same wifi network has open port (ssh port) can ssh device without knowing it's ip-address. idea private ip-address of android device, check if equal common private ip-address (for example 10.0.0.*) , scan it's ip-range (from 10.0.0.1-255) open ssh port (22).
todo:
- get ip android device (checked)
- filter it, if private ip starting "10." && "10.0.0." or "192." && "192.168." , on (checked)
- check if port 22 open or not on range "10.0.0.1-255" or "192.168.1.1-255". (current)
i used code here port scanner: socket port scanner in question future method returned true or false if port on fixed ip-address open or not. result of method added future boolean list array. queried, user see how many ports open on fixed ip-address.
i modified method scanning number of ip-addresses fixed port, , adding ip-addresses, ssh port open, future boolean list array. works except socket.connect(new inetsocketaddress(ip, port), timeout); returns true. tried looking @ code why that, found no answers. if have idea how have socket return false, when given port on ip address not open, please let me know. in advance.
code:
here call socket method (portisopen) , if returns true, append ip adress sent method stringbuilder possibleipaddresses. way can list ip-addresses have port open , later connect 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 querying ip adress here. //checking ip-addresses 10.0.0.1-10.0.0.255 open ssh port string ip = ""; (int x=1;x<=10;x++) { ip = "10.0.0."+x; //should return true if port on ip-address open, returns true everytime..help? if (futures.add(portisopen(es, ip, 22, timeout))) { possibleipaddress.append(ip).append("\n"); } es.shutdown(); } and here code port scanner (it same in 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; } } }); }
your portisopen() method returns future, result never getting, method never returns @ all, because never called. need add list of possible ip addresses if , if result of future true.
nb you're leaking sockets in case there exception. use try-with-resources syntax fix that.
Comments
Post a Comment