查看局域网在线IP的java源代码

       统计局域网内在线机子的数量,并将这些在线机子的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;
        }
    }
}

 

 

 

 

猜你喜欢

转载自blog.csdn.net/m0_37732829/article/details/85017485