public class macaddress { public static void main(String[] args) { macaddress mdd = new macaddress(); String str=mdd.getMacOnWindow(); System.out.println(str); } private static String getMacOnWindow() { String s = ""; try { String s1 = "ipconfig /all"; Process process = Runtime.getRuntime().exec(s1); BufferedReader bufferedreader = new BufferedReader( new InputStreamReader(process.getInputStream())); String nextLine; for (String line = bufferedreader.readLine(); line != null; line = nextLine) { nextLine = bufferedreader.readLine(); if (line.indexOf("Physical Address") <= 0) { continue; } int i = line.indexOf("Physical Address") + 36; s = line.substring(i); break; }
String userName = map.get("USERNAME");// 获取用户名
String computerName = map.get("COMPUTERNAME");// 获取计算机名
String userDomain = map.get("USERDOMAIN");// 获取计算机域名
System.out.println(userName);
System.out.println(computerName);
System.out.println(userDomain);
import java.util.*;
public class YourJavaProperties {
public static void main(String args[]){
Properties props=System.getProperties();
System.out.println("Java的运行环境版本:"+props.getProperty("java.version"));
System.out.println("Java的运行环境供应商:"+props.getProperty("java.vendor"));
System.out.println("Java供应商的URL:"+props.getProperty("java.vendor.url"));
System.out.println("Java的安装路径:"+props.getProperty("java.home"));
System.out.println("Java的虚拟机规范版本:"+props.getProperty("java.vm.specification.version"));
System.out.println("Java的虚拟机规范供应商:"+props.getProperty("java.vm.specification.vendor"));
System.out.println("Java的虚拟机规范名称:"+props.getProperty("java.vm.specification.name"));
System.out.println("Java的虚拟机实现版本:"+props.getProperty("java.vm.version"));
System.out.println("Java的虚拟机实现供应商:"+props.getProperty("java.vm.vendor"));
System.out.println("Java的虚拟机实现名称:"+props.getProperty("java.vm.name"));
System.out.println("Java运行时环境规范版本:"+props.getProperty("java.specification.version"));
System.out.println("Java运行时环境规范供应商:"+props.getProperty("java.specification.vender"));
System.out.println("Java运行时环境规范名称:"+props.getProperty("java.specification.name"));
System.out.println("Java的类格式版本号:"+props.getProperty("java.class.version"));
System.out.println("Java的类路径:"+props.getProperty("java.class.path"));
System.out.println("加载库时搜索的路径列表:"+props.getProperty("java.library.path"));
System.out.println("默认的临时文件路径:"+props.getProperty("java.io.tmpdir"));
System.out.println("一个或多个扩展目录的路径:"+props.getProperty("java.ext.dirs"));
System.out.println("操作系统的名称:"+props.getProperty("os.name"));
System.out.println("操作系统的构架:"+props.getProperty("os.arch"));
System.out.println("操作系统的版本:"+props.getProperty("os.version"));
System.out.println("文件分隔符:"+props.getProperty("file.separator")); //在 unix 系统中是"/"
System.out.println("路径分隔符:"+props.getProperty("path.separator")); //在 unix 系统中是":"
System.out.println("行分隔符:"+props.getProperty("line.separator")); //在 unix 系统中是"/n"
System.out.println("用户的账户名称:"+props.getProperty("user.name"));
System.out.println("用户的主目录:"+props.getProperty("user.home"));
System.out.println("用户的当前工作目录:"+props.getProperty("user.dir"));
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/justinavril/archive/2009/09/06/4525550.aspx
学到了很多东西 谢谢 但是, 请问 怎么得到IP和MAC还有 IP 要包括 本地网卡和无线网卡 谢谢了
Set ipList = new HashSet();
while (en.hasMoreElements()) {
NetworkInterface intf = (NetworkInterface)en.nextElement();
Enumeration enAddr = intf.getInetAddresses();
while (enAddr.hasMoreElements()) {
InetAddress addr = (InetAddress) enAddr.nextElement();
ipList.add(addr.getHostAddress());
}
}
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class macaddress {
public static void main(String[] args) {
macaddress mdd = new macaddress();
String str=mdd.getMacOnWindow();
System.out.println(str);
}
private static String getMacOnWindow() {
String s = "";
try {
String s1 = "ipconfig /all";
Process process = Runtime.getRuntime().exec(s1);
BufferedReader bufferedreader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String nextLine;
for (String line = bufferedreader.readLine(); line != null; line = nextLine) {
nextLine = bufferedreader.readLine();
if (line.indexOf("Physical Address") <= 0) {
continue;
}
int i = line.indexOf("Physical Address") + 36;
s = line.substring(i);
break;
}
bufferedreader.close();
process.waitFor();
} catch (Exception exception) {
s = "";
}
return s.trim();
}
}
import java.util.*;
public class Add{
public static void main(String[] args) throws UnknownHostException{ InetAddress addr = InetAddress.getLocalHost();
String ip=addr.getHostAddress().toString(); //获取本机ip
String hostName=addr.getHostName().toString(); //获取本机计算机名称
System.out.println("本机IP:"+ip+"\n本机名称:"+hostName);
Properties props=System.getProperties();
System.out.println("操作系统的名称:"+props.getProperty("os.name"));
System.out.println("操作系统的版本:"+props.getProperty("os.version"));
}
}
这是在网上找到一篇文章:import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; /**
* 与系统相关的一些常用工具方法.
*
* @author lvbogun
* @version 1.0.0
*/
public class SystemTool { /**
* 获取当前操作系统名称.
* return 操作系统名称 例如:windows xp,linux 等.
*/
public static String getOSName() {
return System.getProperty("os.name").toLowerCase();
}
/**
* 获取unix网卡的mac地址.
* 非windows的系统默认调用本方法获取.如果有特殊系统请继续扩充新的取mac地址方法.
* @return mac地址
*/
public static String getUnixMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("ifconfig eth0");// linux下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息
bufferedReader = new BufferedReader(new InputStreamReader(process
.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
index = line.toLowerCase().indexOf("hwaddr");// 寻找标示字符串[hwaddr]
if (index >= 0) {// 找到了
mac = line.substring(index +"hwaddr".length()+ 1).trim();// 取出mac地址并去除2边空格
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
} return mac;
} /**
* 获取widnows网卡的mac地址.
* @return mac地址
*/
public static String getWindowsMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("ipconfig /all");// windows下的命令,显示信息中包含有mac地址信息
bufferedReader = new BufferedReader(new InputStreamReader(process
.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
index = line.toLowerCase().indexOf("physical address");// 寻找标示字符串[physical address]
if (index >= 0) {// 找到了
index = line.indexOf(":");// 寻找":"的位置
if (index>=0) {
mac = line.substring(index + 1).trim();// 取出mac地址并去除2边空格
}
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
} return mac;
} /**
* 测试用的main方法.
*
* @param argc
* 运行参数.
*/
public static void main(String[] argc) {
String os = getOSName();
System.out.println(os);
if(os.startsWith("windows")){
//本地是windows
String mac = getWindowsMACAddress();
System.out.println(mac);
}else{
//本地是非windows系统 一般就是unix
String mac = getUnixMACAddress();
System.out.println(mac);
}
}
}
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Formatter;
import java.util.Locale;public class IPMACMain {
public static void main(String[] args) throws Exception {
InetAddress address = InetAddress.getLocalHost();
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
ni.getInetAddresses().nextElement().getAddress();
byte[] mac = ni.getHardwareAddress();
String sIP = address.getHostAddress();
String sMAC = "";
Formatter formatter = new Formatter();
for (int i = 0; i < mac.length; i++) {
sMAC = formatter.format(Locale.getDefault(), "%02X%s", mac[i],
(i < mac.length - 1) ? "-" : "").toString(); }
System.out.println("IP:" + sIP);
System.out.println("MAC:" + sMAC);
}
}
这句没用去掉好了
如果允许当然是好
import com.fenghuo.jsnmp.collection.task.LinuxExec;
import java.util.Hashtable;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.OperatingSystem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;/**
*使用开源项目
* Hyperic-hq产品的基础包sigar.jar来实现服务器状态数据的获取
* @author huangxf
*/
public class UseSigar { public int cpunumber = 0;
public int cpuuser = 0;
public int cpuworkfreq = 0;
public int memtotal = 0;
public int memuser = 0;
public int disktotal = 0;
public int diskuser = 0;
public String osversionvalue = "";
public String osvendorvalue = "";
public String machine = "";
public Hashtable<String, Hashtable> nethashtable = new Hashtable(); public UseSigar(String osnamevalue) {
System.out.println("Collection信息:java.library.path:" + System.getProperty("java.library.path"));
if (osnamevalue.startsWith("Linux")) {
System.loadLibrary("sigar-x86-linux");
} else if (osnamevalue.startsWith("Win")) {
System.loadLibrary("sigar-x86-winnt");
}
} public synchronized void runCpuPerc() {
Sigar sigar = new Sigar();
CpuPerc cpuList[] = null;
try {
//cpu相关
CpuInfo infos[] = sigar.getCpuInfoList();
CpuInfo info = infos[0];
cpuworkfreq = info.getMhz();
cpuList = sigar.getCpuPercList();
double totalall = 0;
cpunumber = cpuList.length;
for (int i = 0; i < cpuList.length; i++) {
double total = cpuList[i].getCombined();
total = total * 100;
totalall = totalall + total;
}
cpuuser = ((Double) (totalall / cpunumber)).intValue();
//内存相关
Mem mem = sigar.getMem();
long memuserlog = mem.getTotal();
memuserlog = memuserlog / 1024;
memuserlog = memuserlog / 1024;
Long longobj = new Long(memuserlog);
memtotal = longobj.intValue();
memuser = ((Long) (mem.getUsed() * 100 / mem.getTotal())).intValue();
//磁盘相关
FileSystem fslist[] = sigar.getFileSystemList();
long diskusePercentall = 0;
int disktotalall = 0;
for (int i = 0; i < fslist.length; i++) {
FileSystem fs = fslist[i];
FileSystemUsage usage = null;
try {
usage = sigar.getFileSystemUsage(fs.getDirName());
} catch (SigarException e) {
if (fs.getType() == 2) {
throw e;
}
continue;
}
switch (fs.getType()) {
case 0: // TYPE_UNKNOWN :未知
break;
case 1: // TYPE_NONE
break;
case 2: // TYPE_LOCAL_DISK : 本地硬盘
// // 文件系统总大小
// System.out.println(" Total = " + usage.getTotal() + "KB");
// // 文件系统剩余大小
// System.out.println(" Free = " + usage.getFree() + "KB");
// // 文件系统可用大小
// System.out.println(" Avail = " + usage.getAvail() + "KB");
// // 文件系统已经使用量
// System.out.println(" Used = " + usage.getUsed() + "KB");
// double usePercent = usage.getUsePercent() * 100D;
// 文件系统资源的利用率
// System.out.println(" Usage = " + usePercent + "%");
disktotalall = disktotalall + ((Long) usage.getTotal()).intValue();
diskusePercentall = diskusePercentall + usage.getUsed();
// System.out.println(diskusePercentall);
// diskusePercentall = diskusePercentall + usePercent;
break;
case 3:// TYPE_NETWORK :网络
break;
case 4:// TYPE_RAM_DISK :闪存
break;
case 5:// TYPE_CDROM :光驱
break;
case 6:// TYPE_SWAP :页面交换
break;
}
}
disktotal = disktotalall / 1024;
// diskusePercentall = diskusePercentall / 1024;
long diskuser1 = diskusePercentall * 100 / disktotalall;
diskuser = ((Long) diskuser1).intValue();
//操作系统相关
OperatingSystem OS = OperatingSystem.getInstance();
osversionvalue = OS.getVersion();
osvendorvalue = OS.getVendor();
machine = OS.getMachine();
//网络相关
String ifNames[] = sigar.getNetInterfaceList();
nethashtable.clear();
for (int i = 0; i < ifNames.length; i++) {
String name = ifNames[i];
NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
Hashtable netmessage = new Hashtable();
netmessage.put("ip", ifconfig.getAddress());
netmessage.put("mac", ifconfig.getHwaddr());
this.nethashtable.put(name, netmessage);
// print("\nname = " + name);//网络设备名
// print("Address = " + ifconfig.getAddress());//IP地址
// ifconfig.getHwaddr();
// print("Netmask = " + ifconfig.getNetmask());//子网掩码
// if ((ifconfig.getFlags() & 1L) <= 0L) {
// System.out.println("!IFF_UP...skipping getNetInterfaceStat");
// continue;
// }
}
} catch (SigarException e) {
// e.printStackTrace();
return;
} finally {
sigar.close();
}
} public synchronized boolean getNetFlag(String netname) {
boolean rs = false;
Sigar sigar = new Sigar();
try {
String[] ifNames = sigar.getNetInterfaceList();
// nethashtable.clear();
for (int i = 0; i < ifNames.length; i++) {
String name = ifNames[i];
if (netname.equals(name)) {
NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
if ((ifconfig.getFlags() & 1L) <= 0L) {
rs = false;
} else {
rs = true;
}
}
}
} catch (SigarException ex) {
System.out.println(ex.getMessage());
} finally {
sigar.close();
}
return rs;
} public String getFQDN() {
try {
// return InetAddress.getLocalHost().getCanonicalHostName();
// return InetAddress.getLocalHost().getHostName();
return LinuxExec.getHostName();
} catch (Exception e) {
Sigar sigar = new Sigar();
try {
return sigar.getFQDN();
} catch (SigarException ex) {
System.out.println(ex.getMessage());
return null;
} finally {
sigar.close();
}
}
} public static void main(String[] args) {
UseSigar sigar = new UseSigar("Win");
sigar.runCpuPerc();
System.out.println(sigar.diskuser);
System.out.println(sigar.nethashtable);
System.out.println(sigar.memtotal);
}
}
这个的实现机制,想看下它的源代码,能帖出来么?应该装反编译了吧。。