Java 扫描局域网IP

AI摘要

Java程序通过ping扫描局域网内所有IP地址,并使用ARP命令获取并输出活跃主机的IP列表。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;

public class IP {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			//Runtime.getRuntime().exec("ping 192.168.1.1").destroy();
			//Runtime.getRuntime().exec("for /l %i in (1,1,255) do " +
			//		"(ping -w 2 -n 1 192.168.1.%i)").destroy();
			
			String localIP=InetAddress.getLocalHost().getHostAddress();
			String[] net=localIP.split(".");
			
			int j=Integer.parseInt(net[2]);
			for(int i=1;i<256;i++){
				Runtime.getRuntime().exec("ping -w 2 -n 1 192.168."+j+"."+i).destroy();
			}
			
			Process p=Runtime.getRuntime().exec("arp -a");
			BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
			
			br.readLine();
			br.readLine();
			br.readLine();
			String temp=null;
			while(((temp=br.readLine())!=null&&!temp.isEmpty())){
				System.out.println(temp.trim().split(" ")[0]);
				
			}
			
			//String localIP=InetAddress.getLocalHost().getHostAddress();
			//String[] net=localIP.split(".");
			
			//System.out.println(net[2]);
			
			//System.out.println(InetAddress.getLocalHost().getHostAddress());
			p.destroy();
			br.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}


}

Saiyintai

软件测试工程师

相关推荐

文件操作File(Java)

File类常用方法:isFile()判断是否为文件,length()获取文件字节数,exists()检查文件存在性,createNewFile()创建新文件,delete()删除文件/空目录,isDirectory()判断是否为目录。

JAVA常见的几种排序方法

Java常用排序算法包括冒泡、快速、选择、插入、基数、鸡尾酒、桶和鸽巢排序,并展示了部分算法的实现代码与运行结果。

Java调用高德Api

注册高德API获取Key,使用Java代码调用步行路径规划服务,输入起终点坐标即可返回详细路线信息。

暂无评论