Java获取CPU序列号和网卡Mac地址_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3055 | 回复: 0   主题: Java获取CPU序列号和网卡Mac地址        下一篇 
    本主题由 Administrator 于 2014-9-6 8:19:58 移动
white
注册用户
等级:少校
经验:1327
发帖:305
精华:0
注册:2011-7-21
状态:离线
发送短消息息给white 加好友    发送短消息息给white 发消息
发表于: IP:您无权察看 2014-9-5 14:49:42 | [全部帖] [楼主帖] 楼主

Java获得CPU序列号和网卡Mac地址/*利用Runtime call操作系统的命令,具体的命令取决于不同的操作系统,注意不要调用Runtime.getRuntime().exec(String)接口,要用Runtime.getRuntime().exec(String[])这个接口,不然复杂命令的执行会有问题。例子如下(拿cpu个数,其他类似):定义命令: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"};然后判断系统:os = System.getProperty("os.name").toLowerCase();根据不同的操作系统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{//网卡物理地址长度static private final int _physicalLength = 16;public static void main(String[] args){//output you computer phycail ip addressSystem.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);}}}--------------------------------------------------------------------------------Read a bit and take it out , then come back read some more .http://202.101.111.1/123/http://iAMin.BlogDriver.com YuLimin [版主] 发帖数: 1,577 活跃积分: 1,591 技术积分: 196 可用币值: 486 注册时间: 2002-9-4 用户状态:正常 Re: Java获得CPU序列号和网卡Mac地址 提交时间: Nov 2, 2005 11:51:28 AM 引用 回复 发消息 import java.io.BufferedReader;import java.io.DataInputStream;import java.io.InputStreamReader;import java.util.Vector;public class GetNetMACShell{private Process process = null;public Vector execute(String shellCommand){try{Start(shellCommand);Vector vResult = new Vector();DataInputStream in = new DataInputStream(process.getInputStream());BufferedReader reader = new BufferedReader(new InputStreamReader(in));String line;do{line = reader.readLine();if(line == null){break;}else{vResult.addElement(line);}}while(true);reader.close();return vResult;}catch(Exception e){//errorreturn null;}}public void Start(String shellCommand){try{if(process != null){kill();}Runtime sys = Runtime.getRuntime();process = sys.exec(shellCommand);}catch(Exception e){System.out.println(e.toString());}}public void kill(){if(process != null){process.destroy();process = null;}}}

该贴由system转至本版2014-9-6 8:19:58




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论