使用dos命令获取MAC地址

     获取mac地址的方法比较多,在这里介绍两种常用的方法,在Win9x 可用:WinIPcfg获得,在2000、XP可用IPconfig -all获得。如果你处于内部局域网内,可以用 nbtstat -A IP来获取指定ip地址计算机的mac地址,后者只能在2000/XP下使用。 

代码如下:

 

package  test.io;

import  java.io. * ;

/**
 * 获取本地和局域网中其他计算机MAC地址
 * 
 * 
@author 左杰
 * 
 
*/

public   class  MacTest  {
    
/**
     * 获取本地mac地址,同多执行dos命令ipconfig -all
     * 
     * 
@return
     
*/

    
public static String getLocalMac() {
        String MACAddr 
= "";
        
try {
            Process process 
= Runtime.getRuntime().exec("ipconfig -all");
            InputStreamReader ir 
= new InputStreamReader(process
                    .getInputStream());
            LineNumberReader input 
= new LineNumberReader(ir);
            String line;
            
while ((line = input.readLine()) != null{
                
if (line.indexOf("Physical Address"> 0{
                    MACAddr 
= line.substring(line.indexOf("-"- 2);

                }

            }

        }
 catch (java.io.IOException e) {
            System.err.println(
"IOException " + e.getMessage());
        }

        
return MACAddr;
    }


    
/**
     * 获取本地mac地址,同多执行dos命令nbtstat -A ip
     * 
     * 
@param ip        指定ip地址
     * 
@return
     
*/

    
public static String getMakeIpMac(String ip) {
        String MACAddr 
= "";
        
try {
            
if (ip.equals("")) {
                MACAddr 
= "ip为空!";
            }
 else {
                Process process 
= Runtime.getRuntime().exec("nbtstat -A " + ip);
                InputStreamReader ir 
= new InputStreamReader(process
                        .getInputStream());
                LineNumberReader input 
= new LineNumberReader(ir);
                String line;
                
while ((line = input.readLine()) != null{
                    
if (line.indexOf("MAC Address"> 0{
                        MACAddr 
= line.substring(line.indexOf("-"- 2);
                    }

                }

            }

        }
 catch (java.io.IOException e) {
            System.err.println(
"IOException " + e.getMessage());
        }

        
return MACAddr;
    }


    
/**
     * 测试获取地址
     * 
     * 
@param args
     
*/

    
public static void main(String[] args) {
        System.out.println(
"本地MAC地址为:" + getLocalMac());
        String ip 
= "192.168.0.21";
        System.out.println(
"ip地址为" + ip +"计算机的MAC地址为:" + getMakeIpMac(ip));
    }

}

简单的应用实例,但是我还不知道能不能批量将局域网的所有计算机的ip,mac地址都获取出来
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值