ping批量端口

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Description TestRemotePort
 * Date 2021/11/20
 * Created by www.ij34.com
 */
public class TestRemotePort {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        //ping的端口
        for (int i = 1; i <= 1000; i++) {
            list.add(i);
        }
        int threads = 50;//线程数
        int start = 0;
        int size = list.size() / threads;
        for (int i = 1; i <= threads; i++) {
            new Thread(new ThreadTask(list.subList(start, (start + size) > list.size() ? list.size() : start + size))).start();
            start += size;
        }


    }


    private static class ThreadTask implements Runnable {
        List list;

        public ThreadTask(List<Integer> list2) {
            list = list2;
        }

        @Override
        public void run() {
            list.forEach(c -> {
                boolean b = reachableIP("f2od82dan8xypungixks.okefafs.com",(Integer) c);
                if (b) {
                    System.out.println(Thread.currentThread().getId() + "线程成功端口---------------" + c);
                } else {
//                    System.out.println(Thread.currentThread().getId()  + "|" + c);
                }
            });
        }

        boolean reachableIP(String url,int port) {
            String cmd = "tcping -n 1 "+url+" " + port;
            try {
                Process p = Runtime.getRuntime().exec(cmd);
                InputStream is = p.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                String line;
                reader.readLine();
                String str = reader.readLine();
                try {
                    p.waitFor();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                is.close();
                reader.close();
                p.destroy();
                return str.contains("Port is open") ? true : false;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
    }
}

 

posted on 2021-11-21 13:32  Honey_Badger  阅读(172)  评论(0编辑  收藏  举报

导航

github