zoukankan      html  css  js  c++  java
  • 获取本地ip,dns 以及 mac等ipconfig/all信息代码


    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; 
        }
     
    }
     


  • 相关阅读:
    微服务架构综述
    何为正确
    如何在github中写出自己的readme文件
    redhat中如何在一块网卡上创建多个虚拟IP
    Vim 编辑器与shell命令脚本
    管道符、重定向与环境变量
    linux常用命令
    Android性能优化总结
    HTTP 协议漫谈
    Zxing QRCode
  • 原文地址:https://www.cnblogs.com/bluespot/p/1242326.html
Copyright © 2011-2022 走看看