iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java实现获取内网的所有IP地址
  • 935
分享到

Java实现获取内网的所有IP地址

2024-04-02 19:04:59 935人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录题目描述代码详解效果展示题目描述 在进行网络编程时,有时需要对局域网的所有主机进行遍历,为此需要获得内网的所以IP地址 题目实现:获得内网的所有IP地址的小应用。 解题思路 创建

题目描述

在进行网络编程时,有时需要对局域网的所有主机进行遍历,为此需要获得内网的所以IP地址

题目实现:获得内网的所有IP地址的小应用。

解题思路

创建一个类:GainAlllpFrame,继承JFrame窗体类

定义一个gainAlllp()方法:用于获得所有IP,并显示在文本域中的方法

定义一个内部类Pinglp Thread,且是线程类。用于判断给定IP是否为内网IP的线程对象

线程类的执行逻辑是对指定的IP进行ping 访问

获得本机的IP地址和网段

 InetAddress host = InetAddress.getLocalHost();// 获得本机的InetAddress对象
        String hostAddress = host.getHostAddress();// 获得本机的IP地址
        int pos = hostAddress.lastIndexOf(".");// 获得IP地址中最后一个点的位置
        String wd = hostAddress.substring(0, pos + 1);// 对本机的IP进行截取,获得网段

ping***指定的IP地址,获取ping**结果

 // 获得所ping的IP进程,-w 280是等待每次回复的超时时间,-n 1是要发送的回显请求数
                Process process = Runtime.getRuntime().exec(
                        "ping " + ip + " -w 280 -n 1");
                InputStream is = process.getInputStream();// 获得进程的输入流对象
                InputStreamReader isr = new InputStreamReader(is);// 创建InputStreamReader对象
                BufferedReader in = new BufferedReader(isr);// 创建缓冲字符流对象
                String line = in.readLine();// 读取信息
                while (line != null) {
                    if (line != null && !line.equals("")) {
                        if (line.substring(0, 2).equals("来自")
                                || (line.length() > 10 && line.substring(0, 10)
                                .equals("Reply from"))) {// 判断是ping通过的IP地址
                            pingMap.put(ip, "true");// 向集合中添加IP
                        }
                    }
                    line = in.readLine();// 再读取信息
                }

注意:本题只适合在window运行

代码详解

引入hutool,pom.xml增加

<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.6.5</version>
</dependency>

GainAllpFrame

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
 
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.jscrollPane;
import javax.swing.JTextArea;

class GainAllIpFrame extends JFrame {
    private JTextArea ta_allIp;
    static public Hashtable<String, String> pingMap; // 用于存储所ping的IP是否为内网IP的集合
 
 
    public static void main(String args[]) {
        GainAllIpFrame frame = new GainAllIpFrame();
        frame.setVisible(true);
    }
 
    public void gainAllIp() throws Exception {// 获得所有IP,并显示在文本域中的方法
        InetAddress host = InetAddress.getLocalHost();// 获得本机的InetAddress对象
        String hostAddress = host.getHostAddress();// 获得本机的IP地址
        int pos = hostAddress.lastIndexOf(".");// 获得IP地址中最后一个点的位置
        String wd = hostAddress.substring(0, pos + 1);// 对本机的IP进行截取,获得网段
        for (int i = 1; i <= 255; i++) { // 对局域网的IP地址进行遍历
            String ip = wd + i;// 生成IP地址
            PingIpThread thread = new PingIpThread(ip);// 创建线程对象
            thread.start();// 启动线程对象
        }
        Set<String> set = pingMap.keySet();// 获得集合中键的Set视图
        Iterator<String> it = set.iterator();// 获得迭代器对象
        while (it.hasNext()) { // 迭代器中有元素,则执行循环体
            String key = it.next(); // 获得下一个键的名称
            String value = pingMap.get(key);// 获得指定键的值
            if (value.equals("true")) {
                ta_allIp.append(key + "\n");// 追加显示IP地址
            }
        }
    }
 
    
    public GainAllIpFrame() {
        super();
        addWindowListener(new WindowAdapter() {
            public void windowOpened(final WindowEvent e) {
                try {
                    gainAllIp();
                    ta_allIp.setText(null);
                } catch (Exception e1) {
                    e1.printStackTrace();
                    ta_allIp.setText(null);
                }
            }
        });
        setTitle("获得内网的所有IP地址");
        setBounds(400, 200, 270, 375);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        final JScrollPane scrollPane = new JScrollPane();
        getContentPane().add(scrollPane, BorderLayout.CENTER);
 
        ta_allIp = new JTextArea();
        scrollPane.setViewportView(ta_allIp);
 
        final JPanel panel = new JPanel();
        getContentPane().add(panel, BorderLayout.NORTH);
 
        final JButton button_2 = new JButton();
        button_2.addActionListener(new ActionListener() {
            public void actionPerfORMed(final ActionEvent e) {
                try {
                    ta_allIp.setText(null);
                    gainAllIp();
                } catch (Exception e1) {
                    e1.printStackTrace();
                    ta_allIp.setText(null);
                    JOptionPane.showMessageDialog(null, "应用程序异常,请再试一次。");
                }
            }
        });
        button_2.setText("显示所有IP");
        panel.add(button_2);
 
        final JButton button = new JButton();
        button.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                System.exit(0);
            }
        });
        button.setText("退    出");
        panel.add(button);
 
        pingMap = new Hashtable<String, String>();
    }
 
    class PingIpThread extends Thread {// 判断给定IP是否为内网IP的线程对象
        public String ip; // 表示IP地址的成员变量
        public PingIpThread(String ip) {// 参数为需要判断的IP地址
            this.ip = ip;
        }
        public void run() {
            try {
                // 获得所ping的IP进程,-w 280是等待每次回复的超时时间,-n 1是要发送的回显请求数
                System.out.println("尝试ping IP:"+ip);
                Process process = Runtime.getRuntime().exec(
                        "ping " + ip + " -w 280 -n 1");
                InputStream is = process.getInputStream();// 获得进程的输入流对象
                InputStreamReader isr = new InputStreamReader(is);// 创建InputStreamReader对象
                BufferedReader in = new BufferedReader(isr);// 创建缓冲字符流对象
                String line =  IoUtil.read(is,"GBK");//CMD获取的值是GBK格式的
                //String line = in.readLine();// 读取信息
                if (line != null && !line.equals("")) {
                    if (line.indexOf("来自") >0 || line.indexOf("Reply from")>0) {// 判断是ping通过的IP地址
                        pingMap.put(ip, "true");// 向集合中添加IP
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

效果展示

以上就是Java实现获取内网的所有IP地址的详细内容,更多关于Java获取内网IP地址的资料请关注编程网其它相关文章!

--结束END--

本文标题: Java实现获取内网的所有IP地址

本文链接: https://www.lsjlt.com/news/151320.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作