Java实现获取内网的所有IP地址
作者:学好c语言的小王同学 发布时间:2023-01-01 07:48:56
标签:Java,内网,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;
/**
* Description:
*
* @author 小王同学
* @version 1.0
*/
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地址
}
}
}
/**
* Create the frame
*/
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();
}
}
}
}
效果展示
来源:https://blog.csdn.net/weixin_59796310/article/details/125249469


猜你喜欢
- JDK JRE JVMJDK:Java标准开发包,它提供了编译、运行Java程序所需的各种工具和资源,包括Java编译器、Java运行时环境
- 定义建造者模式(Builder Pattern),又叫生成器模式,是一种对象构建模式 它可以将复杂对象的建造过程抽象出来,使这个抽象过程的不
- 概览Android 平台包含蓝牙网络堆栈支持,此支持能让设备以无线方式与其他蓝牙设备交换数据。应用框架提供通过 Android Blueto
- 1、数组数组其实也是一种数据格式,不过是一种复合类型,它可以存储多个同类型的值。使用数组可以将同类型的变量整合起来管理,比如说我们现在要记录
- 一、Code First 代码优先DbContext可以用于数据库优先,代码优先和模型优先的开发。DbContext主要包含一组非常易于使用
- 1.鼠标右击我的电脑–》属性–》高级系统设置2.把下面的变量名称和电脑文件的本地路径填进去即可(注意:变量值后面后面不要带分号)jdk环境变
- 需要的Maven<!--redis--> <dependency&g
- 这里我们通过Apache Commons CLI来完成目标功能,废话不多说直接上代码所需的maven依赖<dependency>
- 1. 定义在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。2. 使用的
- 本文实例讲述了Android实现调用震动的方法。分享给大家供大家参考,具体如下:调用Android系统的震动,只需要一个类 那就是Vibra
- MyBatis是支持定制化SQL、存储过程以及高级映射的优秀的持久层框架,避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。spri
- 前言最近在工作中遇到了这么一个需求:如何实现 Android 应用前后台切换的监听?下面来一起看看详细的介绍:iOS 内边是可以实现的,Ap
- 关于滑动冲突在Android开发中,如果是一些简单的布局,都很容易搞定,但是一旦涉及到复杂的页面,特别是为了兼容小屏手机而使用了Scroll
- 前言最近有个网友问了我一个问题:系统中大事务问题要如何处理?正好前段时间我在公司处理过这个问题,我们当时由于项目初期时间比较紧张,为了快速完
- 一、ArrayList类概述什么是集合:提供一种存储空间可变的存储模型,存储的数据容量可以发生改变ArrayList集合的特点:底层是数组实
- 本文实例讲述了Android编程实现分页加载ListView功能。分享给大家供大家参考,具体如下:package eoe.listview;
- 本文实例讲述了C#中DataSet转化为实体集合类的方法,分享给大家供大家参考。具体实现方法如下:/// <summary>//
- 1.汉诺塔介绍汉诺塔规则1.有三根杆子A,B,C。A杆上有若干碟子2.每次移动一块碟子,小的只能叠在大的上面3.把所有碟子从A杆全部移到C杆
- IDEA 在接入外接屏且扩展的情况下,如果突然拔掉外接屏,就可能会产生IDEA 整个窗口只在屏幕的右侧显示一点点边框且无法拖拽到当前屏幕的情
- 一. 思路今天接到个小任务,让把json文件转换成excel文件,按照列展开.思路:既然json已经都已经是现成的,那直接将jso