Java實(shí)現(xiàn)局域網(wǎng)IP地址掃描
Java掃描局域網(wǎng)地址主要通過CMD命令,主要通過Runtime和Process類,由于同一局域網(wǎng)下的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;
?? ?}
?? ?
}調(diào)用方法:
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;//主機(jī)名
?? ?
?? ?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() + "]";
?? ?}
?? ?
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JAVA-NIO之Socket/ServerSocket Channel(詳解)
下面小編就為大家?guī)硪黄狫AVA-NIO之Socket/ServerSocket Channel(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
理解JDK動(dòng)態(tài)代理為什么必須要基于接口
這篇文章主要介紹了理解JDK動(dòng)態(tài)代理為什么必須要基于接口,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Spring 框架中注入或替換方法實(shí)現(xiàn)
這篇文章主要介紹了Spring 框架中注入或替換方法實(shí)現(xiàn),非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05
spring aop實(shí)現(xiàn)用戶權(quán)限管理的示例
本篇文章主要介紹了spring aop實(shí)現(xiàn)用戶權(quán)限管理的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
Springboot通過ObjectMapper配置json序列化詳解
SpringBoot默認(rèn)集成Jackson庫,其中ObjectMapper類是核心,用于Java對(duì)象與JSON字符串的互轉(zhuǎn),提供配置序列化特性、注冊(cè)模塊等方法,在SpringBoot中可以全局配置JSON格式,如日期格式化、將Long轉(zhuǎn)為字符串,還可以配置序列化時(shí)的各種規(guī)則,感興趣的可以了解一下2024-10-10
Spring的@CrossOrigin注解處理請(qǐng)求源碼解析
這篇文章主要介紹了Spring的@CrossOrigin注解處理請(qǐng)求源碼解析,@CrossOrigin源碼解析主要分為兩個(gè)階段@CrossOrigin注釋的方法掃描注冊(cè),請(qǐng)求匹配@CrossOrigin注釋的方法,本文從源碼角度進(jìn)行解析,需要的朋友可以參考下2023-12-12
Java?Bean轉(zhuǎn)Map的那些踩坑實(shí)戰(zhàn)
項(xiàng)目中有時(shí)會(huì)遇到Map轉(zhuǎn)Bean,Bean轉(zhuǎn)Map的情況,下面這篇文章主要給大家介紹了關(guān)于Java?Bean轉(zhuǎn)Map那些踩坑的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07

