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() + "]";
?? ?}
?? ?
}以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java開發(fā)分布式服務(wù)框架Dubbo原理機(jī)制詳解
這篇文章主要為大家介紹了java開發(fā)分布式服務(wù)框架Dubbo的原理機(jī)制詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
SpringAop中AspectJ框架的切入點(diǎn)表達(dá)式
這篇文章主要介紹了SpringAop中AspectJ框架的切入點(diǎn)表達(dá)式,AspectJ是一個(gè)基于Java語言的AOP框架,Spring2.0以后新增了對(duì)AspectJ切點(diǎn)表達(dá)式支持,@AspectJ 是AspectJ1.5新增功能,通過JDK5注解技術(shù),允許直接在Bean類中定義切面,需要的朋友可以參考下2023-08-08
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(26)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07
Spring Cloud詳解實(shí)現(xiàn)聲明式微服務(wù)調(diào)用OpenFeign方法
這篇文章主要介紹了Spring Cloud實(shí)現(xiàn)聲明式微服務(wù)調(diào)用OpenFeign方法,OpenFeign 是 Spring Cloud 家族的一個(gè)成員, 它最核心的作用是為 HTTP 形式的 Rest API 提供了非常簡潔高效的 RPC 調(diào)用方式,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2022-07-07
解決mapper.xml中resultType映射類型的問題
這篇文章主要介紹了解決mapper.xml中resultType映射類型的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Java使用flyway實(shí)現(xiàn)腳本自動(dòng)化的方法詳解
Flyway是一個(gè)開源的數(shù)據(jù)庫版本控制工具,主要用于管理數(shù)據(jù)庫的版本和變更,它可以自動(dòng)化地將數(shù)據(jù)庫遷移到不同的版本,同時(shí)支持多種數(shù)據(jù)庫類型,本文給大家介紹了如何使用flyway實(shí)現(xiàn)腳本自動(dòng)化,需要的朋友可以參考下2023-10-10

