跪求JAVA中如何获取CPU的序列号和硬盘的序列号?
定义命令:
windows cmd = " 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处理器| WC-l " };
HPUXCmd = {"/bin/sh ","-c "," echo \ " map \ " |/usr/sbin/cstm | grep CPU | WC-l " };
LinuxCmd = {"/bin/sh ","-c "," cat/proc/CPU info | grep ^process | WC-l " };
然后判断系统:
os = System.getProperty("os.name ")。toLowerCase();
根据不同的操作系统调用不同的命令。
*/
导入Java . io . io exception;
导入Java . io . inputstream;
导入Java . io . inputstreamreader;
导入Java . io . line number reader;
公共类GetMACAddress
{
公共字符串getMACAddress(字符串ipAddress)
{
String str = " ",strMAC = " ",macAddress =
尝试
{
Process pp = Runtime.getRuntime()。exec(" nbtstat-a "+IP address);
InputStreamReader IR = new InputStreamReader(PP . getinputstream());
line number reader input = new line number reader(IR);
for(int I = 1;我& lt100;i++)
{
str = input . readline();
if(str!=空)
{
if(str . index of(" MAC Address ")& gt;1)
{
strMAC = str . substring(str . index of(" MAC Address ")+14,str . length());
打破;
}
}
}
}
捕捉(IOException ex)
{
返回“无法获取MAC地址!”;
}
//
if(strmac . length()& lt;17)
{
返回“错误!”;
}
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);
//
返回macAddress
}
公共静态void main(String[] args)
{
getmac address getmac address = new getmac address();
system . out . println(getmac address . getmac address(" 172.18 . 8 . 225 "));
尝试
{
Java . lang . process proc = runtime . get runtime()。exec(" ipconfig/all ");
InputStream istr = proc . getinputstream();
byte[] data =新字节[1024];
istr.read(数据);
String netdata =新字符串(数据);
System.out.println("你的Mac地址= "+procAll(net data));
}
捕捉(IOException e)
{
system . out . println(" error = "+e);
}
}
公共静态字符串procAll(字符串str)
{
返回proc stringend(procFirstMac(procAddress(str));
}
公共静态字符串procAddress(字符串str)
{
int indexof = str.indexOf("物理地址");
if(index of & gt;0)
{
返回str.substring(indexof,str . length());
}
返回字符串;
}
公共静态字符串procFirstMac(String str)
{
int index of = str . index of(":");
if(index of & gt;0)
{
返回str.substring(indexof + 1,str.length())。trim();
}
返回字符串;
}
公共静态字符串procStringEnd(String str)
{
int index of = str . index of(" \ r ");
if(index of & gt;0)
{
返回str.substring(0,indexof)。trim();
}
返回字符串;
}
}
导入Java . util . vector;
GetNetMAC类
{
//网卡的物理地址长度
静态私有final int _ physical length = 16;
公共静态void main(String[] args)
{
//输出你电脑的ip地址
system . out . println(" MAC address is:\ t "+getPhysicalAddress());
}
静态公共字符串getPhysicalAddress()
{
GetNetMACShell shell = new GetNetMACShell();
string cmd = " cmd . exe/c ipconfig/all ";
向量结果;
result = shell . execute(cmd);
返回parse cmd(result . tostring());
}
//从字符串中解析所需的字符串。
静态私有字符串parseCmd(字符串s)
{
String find = "物理地址。。。。。。。。。:";
int find index = s . index of(find);
if(findIndex == -1)
{
返回“找不到”;
}
其他
{
返回s . substring(find index+find . length()+1,find index+find . length()+1+_ physical length);
}
}
}
导入Java . io . buffered reader;
导入Java . io . data inputstream;
导入Java . io . inputstreamreader;
导入Java . util . vector;
公共类GetNetMACShell
{
私有进程process = null
公共向量执行(字符串shell命令)
{
尝试
{
start(shell command);
Vector vResult = new Vector();
data inputstream in = new data inputstream(process . getinputstream());
buffered reader reader = new buffered reader(new InputStreamReader(in));
弦线;
做
{
line = reader . readline();
if(line == null)
{
打破;
}
其他
{
vresult . add element(line);
}
}
while(真);
reader . close();
返回vResult
}
捕捉(例外e)
{
//错误
返回null
}
}
公共void Start(字符串shellCommand)
{
尝试
{
如果(过程!=空)
{
kill();
}
runtime sys = runtime . get runtime();
process = sys . exec(shell command);
}
捕捉(例外e)
{
system . out . println(e . tostring());
}
}
公共void kill()
{
如果(过程!=空)
{
process . destroy();
process = null
}
}
}
试试吧:)