栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java多线程扫描局域网中的IP

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java多线程扫描局域网中的IP

package cn.ghostcloud.clusterservice.utils;

import cn.ghostcloud.clusterservice.common.PageListUtils;
import cn.ghostcloud.clusterservice.common.PageResult;
import cn.ghostcloud.clusterservice.vo.IpScanningVo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.*;


@Slf4j
@Component
public class IpScanning {

    private List ipScanningVos = new ArrayList<>();

    

    int corePoolSize = 50;

    
    int maximumPoolSize = 60;

    private ThreadPoolExecutor threadPool =
            new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 3,
            TimeUnit.NANOSECONDS, new LinkedBlockingQueue<>(10),
            new ThreadPoolExecutor.CallerRunsPolicy());

    
    public List search(String ips) {
        //清空缓存
        ipScanningVos.clear();
        int divisionIp = ips.lastIndexOf(".");
        String substring = ips.substring(0, divisionIp + 1);
        //扫描对应网段中的所有Ip
        BlockingQueue queue = new ArrayBlockingQueue<>(255);
        for (int i = 1; i < 255; i++) {
            String iip = substring + i;
            threadPool.submit(new PingIp(iip, queue));
        }
        threadPool.shutdown();
        //判断当前线程是否全部执行完成,防止没有执行完返回结果
        while (!threadPool.isTerminated()){}
        ipScanningVos = new ArrayList<>(queue);
        return ipScanningVos;
    }

    
    public PageListUtils searchByPages(SSHUser sshUser) {
        if (ipScanningVos.isEmpty()) {
            search(sshUser.getIp());
        }
        return new PageListUtils<>(ipScanningVos, sshUser.getCurrent(), sshUser.getPageSize());
    }

    private static class PingIp implements Runnable {
        private String ip;
        private Queue array;

        public PingIp(String ip, Queue array) {
            this.array = array;
            this.ip = ip;
        }

        @Override
        public void run() {
            //遍历IP地址
            InetAddress addip = null;
            try {
                addip = InetAddress.getByName(ip);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            //检查设备是否在线,其中1000ms指定的是超时时间
            // 当返回值是true时,说明host是可用的,false则不可。
            boolean status = false;
            try {
                if (addip != null) {
                    status = addip.isReachable(1000);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (status) {
                IpScanningVo ipScanning = new IpScanningVo(addip.getHostName(), ip);
                log.info("IP地址为:" + ip + "tt设备名称为: " + addip.getHostName() + "tt是否可用: " + (status ? "可用" : "不可用"));
                array.add(ipScanning);
            }
        }
    }

    public static void main(String[] args) {
        List search = new IpScanning().search("192.168.0.6");
        for (IpScanningVo ipScanningVo : search) {
            System.out.println(ipScanningVo.getIp() + ":::" + ipScanningVo.getName());
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/854536.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号