posts - 35,  comments - 6,  trackbacks - 0
          //Java獲得CPU序列號(hào)和網(wǎng)卡Mac地址
          /*
          利用Runtime call操作系統(tǒng)的命令,具體的命令取決于不同的操作系統(tǒng),注意不要調(diào)用Runtime.getRuntime().exec(String)接口,要用Runtime.getRuntime().exec(String[])這個(gè)接口,不然復(fù)雜命令的執(zhí)行會(huì)有問題。例子如下(拿cpu個(gè)數(shù),其他類似):
          定義命令:
          WindowsCmd ="cmd.exe /c echo %NUMBER_OF_PROCESSORS%";//windows的特殊
          SolarisCmd = {"/bin/sh", "-c", "/usr/sbin/psrinfo | wc -l"};
          AIXCmd = {"/bin/sh", "-c", "/usr/sbin/lsdev -Cc processor | wc -l"};
          HPUXCmd = {"/bin/sh", "-c", "echo \"map\" | /usr/sbin/cstm | grep CPU | wc -l "};
          LinuxCmd = {"/bin/sh", "-c", "cat /proc/cpuinfo | grep ^process | wc -l"};

          然后判斷系統(tǒng):
          os = System.getProperty("os.name").toLowerCase();

          根據(jù)不同的操作系統(tǒng)call不同的命令。
          */
          import java.io.IOException;
          import java.io.InputStream;
          import java.io.InputStreamReader;
          import java.io.LineNumberReader;

          public class GetMACAddress
          {
          public String getMACAddress(String ipAddress)
          {
          String str = "",strMAC = "",macAddress = "";
          try
          {
          Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);
          InputStreamReader ir = new InputStreamReader(pp.getInputStream());
          LineNumberReader input = new LineNumberReader(ir);
          for(int i = 1;i < 100;i++)
          {
          str = input.readLine();
          if(str != null)
          {
          if(str.indexOf("MAC Address") > 1)
          {
          strMAC = str.substring(str.indexOf("MAC Address") + 14,str.length());
          break;
          }
          }
          }
          }
          catch(IOException ex)
          {
          return "Can't Get MAC Address!";
          }
          //
          if(strMAC.length() < 17)
          {
          return "Error!";
          }
          macAddress = strMAC.substring(0,2) + ":"
          + strMAC.substring(3,5) + ":"
          + strMAC.substring(6,8) + ":"
          + strMAC.substring(9,11) + ":"
          + strMAC.substring(12,14) + ":"
          + strMAC.substring(15,17);
          //
          return macAddress;
          }

          public static void main(String[] args)
          {
          GetMACAddress getMACAddress = new GetMACAddress();
          System.out.println(getMACAddress.getMACAddress("172.18.8.225"));

          try
          {
          java.lang.Process proc = Runtime.getRuntime().exec("ipconfig /all");
          InputStream istr = proc.getInputStream();
          byte[] data = new byte[1024];
          istr.read(data);
          String netdata = new String(data);
          System.out.println("Your Mac Address=" + procAll(netdata));
          }
          catch(IOException e)
          {
          System.out.println("error=" + e);
          }
          }

          public static String procAll(String str)
          {
          return procStringEnd(procFirstMac(procAddress(str)));
          }

          public static String procAddress(String str)
          {
          int indexof = str.indexOf("Physical Address");
          if(indexof > 0)
          {
          return str.substring(indexof,str.length());
          }
          return str;
          }

          public static String procFirstMac(String str)
          {
          int indexof = str.indexOf(":");
          if(indexof > 0)
          {
          return str.substring(indexof + 1,str.length()).trim();
          }
          return str;
          }

          public static String procStringEnd(String str)
          {
          int indexof = str.indexOf("\r");
          if(indexof > 0)
          {
          return str.substring(0,indexof).trim();
          }
          return str;
          }
          }



          import java.util.Vector;

          class GetNetMAC
          {
          //網(wǎng)卡物理地址長度
          static private final int _physicalLength = 16;

          public static void main(String[] args)
          {
          //output you computer phycail ip address
          System.out.println("The MAC Addressis:\t" + getPhysicalAddress());
          }

          static public String getPhysicalAddress()
          {
          GetNetMACShell shell = new GetNetMACShell();
          String cmd = "cmd.exe /c ipconfig/all";
          Vector result;
          result = shell.execute(cmd);
          return parseCmd(result.toString());
          }

          //從字符串中解析出所需要獲得的字符串
          static private String parseCmd(String s)
          {
          String find = "Physical Address. . . . . . . . . :";
          int findIndex = s.indexOf(find);
          if(findIndex == -1)
          {
          return "not find";
          }
          else
          {
          return s.substring(findIndex + find.length() + 1,findIndex + find.length() + 1 + _physicalLength);
          }
          }
          }



          posted on 2009-09-14 09:51 NG 閱讀(830) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。

          網(wǎng)站導(dǎo)航:
           
          <2009年9月>
          303112345
          6789101112
          13141516171819
          20212223242526
          27282930123
          45678910

          常用鏈接

          留言簿(1)

          隨筆檔案(35)

          文章分類(5)

          文章檔案(2)

          新聞檔案(5)

          java link

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 新竹市| 新沂市| 建湖县| 南康市| 呈贡县| 始兴县| 巴青县| 三江| 民权县| 阆中市| 江北区| 吴忠市| 密云县| 南涧| 游戏| 太白县| 华坪县| 富顺县| 定南县| 定襄县| 托克逊县| 日喀则市| 晋宁县| 当阳市| 迭部县| 建阳市| 东城区| 虞城县| 莎车县| 进贤县| 雅安市| 海城市| 钦州市| 满洲里市| 通城县| 潮州市| 九寨沟县| 内乡县| 安丘市| 醴陵市| 辉县市|