Java Tools class, get the MAC address, local IP, computer name of the computer in the Windows,linux system

Source: Internet
Author: User
Tags unique id

 PackageCom.cloudssaas.util;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;Importjava.net.InetAddress;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;/********************************************************************************* *//* Copyright (C) 2014xxxxxxx xxx. All rights Reserved. *//* Filename: * Computerinfo.java//* revision:1.0//* Author: <yao xiucai>//* Created on: * May 21, 2014//* Mo Dified by://* Modified on://*//* Description: * * < Take network card Physical Address--* 1. Available under the Windows,linux system; * 2. Obtaining a computer through Ipconifg,ifconfig Information; * 3. Find MAC address in pattern matching mode, independent of OS language > * *//* Description: < take computer name--Take > * FROM environment variable * Abstract limit inheritance/create instance*//********************************************************************************/ Public Abstract classComputerInfo {Private StaticString Macaddressstr =NULL; Private StaticString computerName = system.getenv (). Get ("ComputerName"); Private Static FinalString[] Windowscommand = {"ipconfig", "/all" }; Private Static FinalString[] Linuxcommand = {"/sbin/ifconfig", "-a" }; Private Static FinalPattern Macpattern = Pattern.compile (". *" (:? 0-9a-f]{2}[-:]) {5}[0-9a-f]{2}). * ", pattern.case_insensitive); /*** Get multiple network card addresses * *@return     * @throwsIOException*/    Private Final StaticList<string> getmacaddresslist ()throwsIOException {FinalArraylist<string> macaddresslist =NewArraylist<string>(); FinalString OS = System.getproperty ("Os.name"); FinalString command[]; if(Os.startswith ("Windows") ) {command=Windowscommand; } Else if(Os.startswith ("Linux") ) {command=Linuxcommand; } Else {            Throw NewIOException ("Unknow operating system:" +OS); }        //Execute Command        FinalProcess Process =runtime.getruntime (). exec (command); BufferedReader Bufreader=NewBufferedReader (NewInputStreamReader (Process.getinputstream ()));  for(String line =NULL; (line = Bufreader.readline ())! =NULL;) {Matcher Matcher=Macpattern.matcher (line); if(Matcher.matches ()) {Macaddresslist.add (Matcher.group (1)); //Macaddresslist.add (Matcher.group (1). ReplaceAll ("[-:]",// ""));//Remove the "-" from your Mac}} Process.destroy ();        Bufreader.close (); returnmacaddresslist; }    /*** Get a network card address (from which multiple NICs get one) * *@return     */     Public StaticString getmacaddress () {if(Macaddressstr = =NULL|| Macaddressstr.equals ("") ) {StringBuffer sb=NewStringBuffer ();//store multiple network card addresses, currently only a value of not 0000000000E0 tunnels            Try{List<String> maclist =getmacaddresslist ();  for(Iterator<string> iter =maclist.iterator (); Iter.hasnext ();) {String Amac=Iter.next (); if(!amac.equals ("0000000000E0") {sb.append (AMAC);  Break; }                }            } Catch(IOException e) {e.printstacktrace (); } macaddressstr=sb.tostring (); }        returnMacaddressstr; }    /*** Get Computer name * *@return     */     Public StaticString GetComputerName () {if(ComputerName = =NULL|| Computername.equals ("") ) {ComputerName= System.getenv (). Get ("COMPUTERNAME"); }        returnComputerName; }    /*** Get Client IP address * *@return     */     Public StaticString Getipaddrandname ()throwsIOException {returninetaddress.getlocalhost (). toString (); }    /*** Get Client IP address * *@return     */     Public StaticString getipaddr ()throwsIOException {returninetaddress.getlocalhost (). Gethostaddress (). toString (); }    /*** Get PC Unique ID * *@return     */     Public Staticstring Getcomputerid () {string id=getmacaddress (); if(id = =NULL|| Id.equals ("")) {            Try{ID=Getipaddrandname (); } Catch(IOException e) {e.printstacktrace (); }        }        returnComputerName; }    /*** Restrict creation of instances*/    PrivateComputerInfo () {} Public Static voidMain (string[] args)throwsIOException {System.out.println (computerinfo.getmacaddress ());        System.out.println (Computerinfo.getcomputername ());        System.out.println (Computerinfo.getipaddr ());    System.out.println (Computerinfo.getipaddrandname ()); }}

Related:

Computer MAC network card identification, IP, computer name and other unique identification problems in Java

Java Tools class, get the MAC address, local IP, computer name of the computer in the Windows,linux system

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.