获取服务器MAC地址以及相关网络信息

import java.io.*;
import java.net.*;
import java.util.*;

public class getMac
{
    public static void main(String[] args)
    {
        //Get IP of local host
        String IP = "";
        IP = getIP();
        System.out.println(IP);
        //Get OS type
        String OSName = getOSName();
        System.out.println("os.name:"+OSName);
        //Get IPconfig command
        String IPconfig = getIPconfig(OSName);
        System.out.println("IPconfig:"+IPconfig);
        //Get the configuration of your system
        String sysconfig = getSysConfig(IPconfig);
        System.out.println("sysconfig:"+sysconfig);
        //Get MAC address of eth0
        String MAC = getMAC(sysconfig,OSName);
        System.out.println("MAC : "+MAC);
        String DNS = getDNS(sysconfig,OSName);
        System.out.println("DNS : "+DNS);
    }
   
   
    //Get OS type
    public static String getOSName(){
        Properties properties = null ;
        properties = System.getProperties();
        String OSName = properties.getProperty("os.name");
        return OSName;
    }
   
   
    //Get IPconfig command
    public static String getIPconfig(String OSName){
        String IPconfig = "";
        //RedHat Linux 9.0
        if (OSName.compareToIgnoreCase("Linux")==0){
        IPconfig="ifconfig";
        //WINDOWS XP
        } else if ( OSName.compareToIgnoreCase("Windows XP")==0 ){
        IPconfig="ipconfig /all";
        //other OS that I don't know
        } else {
        System.out.println("I don't know your os type !");
        System.exit(1);
        }
        return IPconfig;
    }
   
   
    //Get the configuration of your system
    public static String getSysConfig(String ipconfig){
        String sysconfig = "";
        try {
        Process p = Runtime.getRuntime().exec(ipconfig);
        InputStreamReader reader = new InputStreamReader(p.getInputStream());
       
        char[] localConfig=new char[2000];
        reader.read(localConfig);
         for (int i=0;i<2000 ;i++ ){
        sysconfig+=localConfig[i];
        }
        }
        catch (java.io.IOException ex) {
        System.err.println("Problems invoking "+ipconfig);
        System.exit(1);
        }
        return sysconfig;
    }
    //Get MAC address of eth0
    //This function can only be used by getMAC
    private static String getmac(String sysconfig,String macstr){
        String mac = "";
        int index=sysconfig.indexOf(macstr);
        index+=macstr.length();
        for (; index<=sysconfig.length() ;index++ ){
        if (sysconfig.charAt(index)>='0')
            if (sysconfig.charAt(index)<='9')
        break;
        if (sysconfig.charAt(index)>='a')
        if (sysconfig.charAt(index)<='f')
        break;
        if (sysconfig.charAt(index)>='A')
        if (sysconfig.charAt(index)<='F')
        break;
        }
        for (int i=0;i<17 ;i++ )
        {
        mac+=sysconfig.charAt(index);
        index++;
        }
        return mac;
    }
   
    //Get MAC address of eth0
    //This function can only be used by getMAC
    private static String getdns(String sysconfig,String macstr){
        String dns = "";
        int index=sysconfig.indexOf(macstr);
        index+=macstr.length();
        for (; index<=sysconfig.length() ;index++ ){
        if (sysconfig.charAt(index)>='0')
            if (sysconfig.charAt(index)<='9')
        break;
        if (sysconfig.charAt(index)>='a')
        if (sysconfig.charAt(index)<='f')
        break;
        if (sysconfig.charAt(index)>='A')
        if (sysconfig.charAt(index)<='F')
        break;
        }
        for (int i=0;i<15;i++ )
        {
            dns+=sysconfig.charAt(index);
            index++;
        }
        for (int i=0;i<44;i++ )
        {
            index++;
        }
        for (int i=0;i<15;i++ )
        {
            dns+=sysconfig.charAt(index);
            index++;
        }
        return dns;
    }
   
    //Get MAC address of eth0
    public static String getMAC(String sysconfig,String OSName){
        String MAC = "";
        //RedHAT Linux 9.0
        if (OSName.compareToIgnoreCase("Linux")==0){
        MAC=getmac(sysconfig,"Ethernet HWaddr");
        }
         //WINDOWS XP
        else if ( OSName.compareToIgnoreCase("Windows XP")==0 ){
         MAC = getmac(sysconfig,"Physical Address");
        }
        else {
        System.out.println("I don't know your os type !");
        System.exit(1);
        }
         return MAC;
    }
   
    //Get MAC address of eth0
    public static String getDNS(String sysconfig,String OSName){
        String DNS = "";
        //RedHAT Linux 9.0
        if (OSName.compareToIgnoreCase("Linux")==0){
            DNS=getmac(sysconfig,"Ethernet HWaddr");
        }
         //WINDOWS XP
        else if ( OSName.compareToIgnoreCase("Windows XP")==0 ){
            DNS = getdns(sysconfig,"DNS Servers");
        }
        else {
        System.out.println("I don't know your os type !");
        System.exit(1);
        }
         return DNS;
    }
   
   
   
    //Get IP of eth0
    public static String getIP(){
            String IP = "";
            try {
             Enumeration nifs= NetworkInterface.getNetworkInterfaces();
                 while (nifs.hasMoreElements()){
                    NetworkInterface netIf= (NetworkInterface)nifs.nextElement();
                    String nifName= netIf.getName();
                    if (nifName.compareToIgnoreCase("lo")==0){
                    continue ;
                    }
                    System.out.print(nifName + " : ");
                   
                    for (Enumeration nifIp= netIf.getInetAddresses(); nifIp.hasMoreElements();) {
                        InetAddress address= (InetAddress)nifIp.nextElement();
                        IP= address.getHostAddress();
                        System.out.println(IP);
                    }
                 }
            } catch (Exception e) {
                 e.printStackTrace();
            }
        return IP;
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值