Java 局域网IP地址扫描

Java 同时被 3 个专栏收录
97 篇文章 0 订阅
1 篇文章 0 订阅
1 篇文章 0 订阅

Java扫描局域网地址主要通过CMD命令,主要通过RuntimeProcess类,由于同一局域网下的IP地址比较多需要通过Java的多线程来扫描端口。

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class PingTask{
	
	private String   address;
	
	public PingTask(String address){
		this.address=address;
	}
	
	public PingResult run() {
		Runtime runtime;
		Process process;
		try {
			runtime=Runtime.getRuntime();
			process=runtime.exec("ping "+address);
			BufferedInputStream inputStream=(BufferedInputStream) process.getInputStream();
			byte [] bt =new byte[1024];
			StringBuffer buffer=new StringBuffer();
			int len=0;
			while((len=inputStream.read(bt, 0,bt.length))!=-1){
				buffer.append(new String(bt, 0, len, "GBK"));
			}
			String regex="(\\d*)?";
			String result="";
			Pattern pattern=Pattern.compile(regex);
			Matcher matcher=pattern.matcher(buffer.toString());
			while(matcher.find()){
				if(!matcher.group().equals("")){
					result=matcher.group();
				}
			}
			
			InetAddress inetAddress=InetAddress.getByName(address);
			String hostname="";
			if(!inetAddress.getHostName().equals(address)){
				hostname=inetAddress.getHostName();
			}
			String mac="";
			process=runtime.exec("arp -a "+address);
			
			BufferedInputStream macinputStream=(BufferedInputStream) process.getInputStream();
			byte [] macbt =new byte[1024];
			StringBuffer macbuffer=new StringBuffer();
			while((len=macinputStream.read(macbt, 0,macbt.length))!=-1){
				macbuffer.append(new String(macbt, 0, len, "GBK"));
			}
			String[] macresult=macbuffer.toString().trim().split("\r\n");
			
			if(!macbuffer.toString().contains("未找到 ARP")){
				mac=macresult[2].substring(20, 40).trim();
			}
			
			PingResult pingResult=new PingResult(address,(100-Integer.parseInt(result))+"%",mac,hostname);
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return pingResult;
	}
	
}

调用方法:

	public static void main(String[] args) {
		PingResult result=new PingTask("123.123.123.123");
		System.out.println(result.toString());
	}

PingResult 类

package com.xu.ip;

public class PingResult {
	
	private static String address;//IP地址
	private static String result;//是否可以连接
	private static String physicialaddress;//物理地址
	private static String hostname;//主机名
	
	public String getPhysicialaddress() {
		return physicialaddress;
	}

	public void setPhysicialaddress(String physicialaddress) {
		PingResult.physicialaddress = physicialaddress;
	}

	public String getHostname() {
		return hostname;
	}

	public void setHostname(String hostname) {
		PingResult.hostname = hostname;
	}

	public PingResult(String address, String result) {
		PingResult.address = address;
		PingResult.result = result;
	}

	public PingResult() {
		
	}
	
	public PingResult(String address, String result, String physicialaddress, String hostname) {
		PingResult.address = address;
		PingResult.result = result;
		PingResult.physicialaddress = physicialaddress;
		PingResult.hostname = hostname;
	}

	public String getAddress() {
		return address;
	}
	
	public void setAddress(String address) {
		PingResult.address = address;
	}
	
	public String getResult() {
		return result;
	}
	
	public void setResult(String result) {
		PingResult.result = result;
	}

	@Override
	public String toString() {
		return "PingResult [getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()="
				+ super.toString() + "]";
	}
	
}

  • 0
    点赞
  • 0
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值