| 16 | * Given a single IP or IP range, check without hosts are alive (ping sweep). |
| 17 | */ |
| 18 | public class pingSweep extends Applet { |
| 19 | |
| 20 | public static String ipRange = ""; |
| 21 | public static int timeout = 0; |
| 22 | public static List<InetAddress> hostList; |
| 23 | |
| 24 | public pingSweep() { |
| 25 | super(); |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | public void init(){ |
| 30 | ipRange = getParameter("ipRange"); |
| 31 | timeout = Integer.parseInt(getParameter("timeout")); |
| 32 | } |
| 33 | |
| 34 | //called from JS |
| 35 | public static int getHostsNumber(){ |
| 36 | try{ |
| 37 | hostList = parseIpRange(ipRange); |
| 38 | }catch(UnknownHostException e){ //do something |
| 39 | |
| 40 | } |
| 41 | return hostList.size(); |
| 42 | } |
| 43 | |
| 44 | //called from JS |
| 45 | public static String getAliveHosts(){ |
| 46 | String result = ""; |
| 47 | try{ |
| 48 | result = checkHosts(hostList); |
| 49 | }catch(IOException io){ |
| 50 | //do something |
| 51 | } |
| 52 | return result; |
| 53 | } |
| 54 | |
| 55 | private static List<InetAddress> parseIpRange(String ipRange) throws UnknownHostException { |
| 56 | |
| 57 | List<InetAddress> addresses = new ArrayList<InetAddress>(); |
| 58 | if (ipRange.indexOf("-") != -1) { //multiple IPs: ipRange = 172.31.229.240-172.31.229.250 |
| 59 | String[] ips = ipRange.split("-"); |
| 60 | String[] octets = ips[0].split("\\."); |
| 61 | int lowerBound = Integer.parseInt(octets[3]); |
| 62 | int upperBound = Integer.parseInt(ips[1].split("\\.")[3]); |
| 63 | |
| 64 | for (int i = lowerBound; i <= upperBound; i++) { |
| 65 | String ip = octets[0] + "." + octets[1] + "." + octets[2] + "." + i; |
| 66 | addresses.add(InetAddress.getByName(ip)); |
| 67 | } |
| 68 | } else { //single ip: ipRange = 172.31.229.240 |
| 69 | addresses.add(InetAddress.getByName(ipRange)); |
| 70 | } |
| 71 | return addresses; |
| 72 | } |
| 73 | |
| 74 | private static String checkHosts(List<InetAddress> inetAddresses) throws IOException { |
| 75 | String alive = ""; |
nothing calls this directly
no outgoing calls
no test coverage detected