Skip to content

Instantly share code, notes, and snippets.

@scottshuffler
Created June 6, 2017 20:47
Show Gist options
  • Save scottshuffler/ff5f68950bcf84aa02bb31c1b7995ce2 to your computer and use it in GitHub Desktop.
Save scottshuffler/ff5f68950bcf84aa02bb31c1b7995ce2 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.lang.reflect.Array;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
//import org.apache.log4j.Logger;
/**
* Created by scott on 6/28/16.
*
*/
public class networkJava {
// private final static Logger logger = Logger.getLogger(networkJava.class);
private int total_ips_scanned = 0;
// String[] subnets = {"192.168.","10.0.","10.1.","10.10.","152.10.","200.200."};
// String[] subnets = {"152.10."};
String[] subnets = {"192.168."};
public static void main(String args[]) {
networkJava nj = new networkJava();
nj.searchHosts();
}
public ArrayList<String> searchHosts() {
long startTime = System.currentTimeMillis();
ArrayList<String> result = null;
for (String subnet : subnets) {
result = searchAllHosts(subnet);
result.forEach(System.out::println);
if (!result.isEmpty()) {
break;
}
}
long endTime = System.currentTimeMillis();
System.out.println("That took " + (endTime - startTime) + " milliseconds");
return result;
}
/**
* Searches for all hosts
* @return list of hosts found
*/
public ArrayList<String> searchAllHosts(String ip) {
// logger.info("Searching for host on subnet " + ip);
ArrayList<String> allHosts;
ArrayList<String> foundHosts = new ArrayList<>();
for (int i=0;i<255;i++) {
allHosts = findAllHosts(ip + i);
if(!allHosts.isEmpty()) {
// System.out.println("Network detected, stopping...");
// System.out.println("All devices found: ");
for (String allHost : allHosts) {
System.out.print("IP: " + allHost);
foundHosts.add(allHost);
InetAddress byName = null;
try {
byName = InetAddress.getByName(allHost);
} catch (UnknownHostException e) {
// logger.error("Error while scanning network");
// logger.error("Reason: " + e);
System.out.println("error");
}
assert byName != null;
String hostName = byName.getHostName();
System.out.println("Hostname: " + hostName);
}
// break;
}
}
// for(String hostsFound : foundHosts) {
// logger.info("Found host: " + hostsFound);
// }
return foundHosts;
}
/**
*
* @param subnet -
* @return -
*/
private ArrayList<String> findAllHosts(String subnet){
int timeout=10;
ArrayList<String> ips = new ArrayList<>();
for (int i=1;i<255;i++){
String host=subnet + "." + i;
// System.out.println(host);
setTotal_ips_scanned(getTotal_ips_scanned() + 1);
try {
if(isReachable(host,10)) {
// }
// if (InetAddress.getByName(host).isReachable(timeout)){
InetAddress byName = InetAddress.getByName(host);
String hostName = byName.getHostName();
// logger.info(hostName);
System.out.println(host);
if (hostName.contains("rpi")) {
// System.out.println(hostName);
ips.add(host);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
return ips;
}
/**
*
* @param ip -
* @return -
*/
public boolean ipIsReachable(String ip) {
int timeout = 1000;
boolean res = false;
try {
res = InetAddress.getByName(ip).isReachable(timeout);
} catch (IOException e) {
e.printStackTrace();
}
return res;
}
private static boolean isReachable(String addr, int timeOutMillis) {
// Any Open port on other machine
// openPort = 22 - ssh, 80 or 443 - webserver, 25 - mailserver etc.
try {
try (Socket soc = new Socket()) {
soc.connect(new InetSocketAddress(addr, 22), timeOutMillis);
}
return true;
} catch (IOException ex) {
return false;
}
}
public void setTotal_ips_scanned(int total) {
total_ips_scanned = total;
}
public int getTotal_ips_scanned() {
return total_ips_scanned;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment