檢視區域網線上IP的java原始碼
阿新 • • 發佈:2018-12-24
統計區域網內線上機子的數量,並將這些線上機子的IP輸入到資料庫!之後檢測這些線上IP是否提供web服務或ftp服務。
解決思路:用 ping 協議窮舉,然後socket探測其 80 和 25 埠是否開放。
public class Test { public static void main(String[] args) { int ips[] = new int[] { 192, 168, 43, 0 }; for(int i = 1; i < 255; i++) { String ip = ips[0] + "." + ips[1] + "." + ips[2] + "." + i; //System.out.println("start:" + ip); while(!runPT(ip)) { try { Thread.sleep(10); } catch (InterruptedException e) { } } } } public static class PingThread extends Thread { private String ip; private boolean running; public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public boolean isRunning() { return running; } public void setRunning(boolean running) { this.running = running; } @Override public void run() { while (true) { synchronized (this) { // running = true; try { if(ip != null) { //linux //String cmd = "ping -c 4 " + ip; //windows String cmd = "ping -n 4 " + ip; String key = "TTL"; //System.out.println(cmd); boolean pingok = false; java.lang.Process p = java.lang.Runtime.getRuntime().exec(cmd); java.io.InputStream is = p.getInputStream(); java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(is)); String line; int linenum=0; while(null != (line = br.readLine())) { linenum++; if(line.contains(key)) { pingok = true; //System.out.println(linenum+" : "+line); break; } if(linenum>2){ break; } } p.destroy(); if(pingok) { System.out.println("ping " + ip + " is ok..."); for(int i = 1; i < 255; i++) { while(!runCT(ip, i)) { try{ Thread.sleep(10); } catch(Exception e) { } } } }else{ //System.out.println("Unknow host "+ip); } } } catch(Exception e) { } finally { this.ip = null; running = false; try{ this.wait(); } catch(Exception e) { } } } } } } public static class ConnectThread extends Thread { private String host; private int port; private boolean running; public boolean isRunning() { return running; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public void setRunning(boolean running) { this.running = running; } @Override public void run() { while(true){ //System.out.println("is opened"); synchronized(this) { running = true; try { if(host != null) { java.net.Socket s = new java.net.Socket(this.host, this.port); s.close(); System.out.println("Host " + this.host + " port " + this.port + " is opened"); } } catch(Exception e) { } finally { running = false; this.host = null; try { this.wait(); } catch(Exception e) { } } } } } } private static final Object ct_locker = new Object(); private static java.util.List<ConnectThread> ct_pool = new java.util.ArrayList<ConnectThread>(); private static int ct_pool_limit = 500; public static boolean runCT(String host, int port) { synchronized(ct_locker){ ConnectThread ct = null; for(int i = 0; i < ct_pool.size(); i++) { if(!ct_pool.get(i).isRunning()){ ct = ct_pool.get(i); ct.setHost(host); ct.setPort(port); synchronized(ct) { ct.notify(); } break; } } if(ct == null && ct_pool.size() < ct_pool_limit) { ct = new ConnectThread(); ct.setHost(host); ct.setPort(port); ct.start(); ct_pool.add(ct); } if(ct != null) { return true; } else { return false; } } } private static final Object pt_locker = new Object(); private static java.util.List<PingThread> pt_pool = new java.util.ArrayList<PingThread>(); private static int pt_pool_limit = 500; public static boolean runPT(String ip) { synchronized(pt_locker) { boolean ret = false; PingThread pt = null; for(int i = 0; i < pt_pool.size(); i++) { if(!pt_pool.get(i).isRunning()) { pt = pt_pool.get(i); pt.setIp(ip); synchronized(pt) { pt.setRunning(true); pt.notify(); } break; } } if(pt == null && pt_pool.size() < pt_pool_limit) { pt = new PingThread(); pt.setIp(ip); pt.start(); pt.setRunning(true); pt_pool.add(pt); } if(pt != null) { ret = true; } return ret; } } }