Created
May 20, 2019 02:02
-
-
Save David256/b17bb1082e9d63d685c344f289645472 to your computer and use it in GitHub Desktop.
Clase Android Java para escanear un rango de ip. Pensada para otra cosa, pero...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ext.domain.subdomain.appname; | |
import java.net.Socket; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import android.util.Log; | |
/** | |
* Esta clase define un objeto rebuscador de direcciones IP que | |
* respondan a un determinado puerto, en plan nmap. | |
*/ | |
public class IoTExcavator { | |
private String ip; // como 192.168.0.0 | |
private String mask; // como 255.255.255.0 | |
private int port; // como 5000 | |
/** | |
* Constructor de la clase. | |
* @param ip ID de la red. | |
* @param mask Máscara de red. | |
* @param port Puerto. | |
*/ | |
public IoTExcavator(String ip, String mask, int port) { | |
this.ip = ip; | |
this.mask = mask; | |
this.port = port; | |
} | |
/** | |
* Busca las IP que permitan una conexión al puerto establecido. | |
* @param full Indica si se quiere buscar todas las IP, si es `true`, de lo contrario, se retorna la primera aparición. | |
* @return Lista de IP. | |
*/ | |
public ArrayList<String> findIP(boolean full) { | |
// buscamos mediante for la ip | |
// contamos la cantidad de puntos en la ip | |
Log.d("IoTExcavator", "Comenzando la búsqueda..."); | |
int countIpParts = 0; | |
for (int i = 0; i < this.ip.length(); i++) | |
{ | |
if (this.ip.charAt(i) == '.') | |
countIpParts++; | |
} | |
if (countIpParts != 3) | |
return null; | |
Log.d("IoTExcavator", "Partes de la IP: " + Integer.toString(countIpParts)); | |
// contamos la cantidad de puntos en la máscara | |
int countMaskParts = 0; | |
for (int i = 0; i < this.mask.length(); i++) | |
{ | |
if (this.mask.charAt(i) == '.') | |
countMaskParts++; | |
} | |
if (countMaskParts != 3) | |
return null; | |
Log.d("IoTExcavator", "Partes de la máscara: " + Integer.toString(countMaskParts)); | |
String theIP; | |
// separamos la ip dada | |
ArrayList<String> listIPs = new ArrayList<String>(); | |
String [] markParts = this.mask.split("\\."); | |
String [] ipParts = this.ip.split("\\."); | |
ArrayList<Integer> listMarkParts = new ArrayList<Integer>(); | |
ArrayList<Integer> listIpParts = new ArrayList<Integer>(); | |
for (String part : markParts) { | |
Log.d("IoTExcavator", "máscara << " + part); | |
listMarkParts.add(Integer.parseInt(part)); | |
} | |
for (String part : ipParts) { | |
Log.d("IoTExcavator", "ip << " + part); | |
listIpParts.add(Integer.parseInt(part)); | |
} | |
boolean ok = false; | |
// crearemos la nueva ip | |
for (int i = listIpParts.get(0); i < listIpParts.get(0) + (256 - listMarkParts.get(0)); i++) { | |
for (int j = listIpParts.get(1); j < listIpParts.get(1) + (256 - listMarkParts.get(1)); j++) { | |
for (int k = listIpParts.get(2); k < listIpParts.get(2) + (256 - listMarkParts.get(2)); k++) { | |
for (int l = listIpParts.get(3); l < listIpParts.get(3) + (256 - listMarkParts.get(3)); l++) { | |
theIP = Integer.toString(i) + "." + Integer.toString(j) + "." + Integer.toString(k) + "." + Integer.toString(l); | |
// comprobamos que se puede conectar a dicha ip:port | |
Log.d("IoTExcavator", "Preguntando por " + theIP + ":" + Integer.toString(this.port)); | |
if (this.checkIpAndPort(theIP, this.port)) { | |
Log.d("IoTExcavator", theIP + " tiene IoT"); | |
listIPs.add(theIP); | |
if (!full) { | |
ok = true; | |
break; | |
} | |
} | |
} | |
if (ok) break; | |
} | |
if (ok) break; | |
} | |
if (ok) break; | |
} | |
return listIPs; | |
} | |
/** | |
* Revisa si se puede conectar a una dirección IP a un puerto. | |
* @param ip Dirección IP. | |
* @param port Puerto. | |
* @return `true` si la conexión es exitosa. | |
*/ | |
private boolean checkIpAndPort(String ip, int port) { | |
try { | |
Socket visa = new Socket(ip, port); | |
visa.close(); | |
return true; | |
} catch (IOException e) { | |
//e.printStackTrace(); | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment