Java代碼如何判斷l(xiāng)inux系統(tǒng)windows系統(tǒng)
Java代碼判斷l(xiāng)inux系統(tǒng)windows系統(tǒng)
在使用硬件SDK的時候,往往有windows、linux兩套。
本地開發(fā)的時候使用windows,打包發(fā)布的時候使用linux,來回切換很麻煩。
可以使用下面的判斷來加載不同版本的SDK。
package Commom;
public class osSelect {
public static boolean isLinux() {
return System.getProperty("os.name").toLowerCase().contains("linux");
}
public static boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("windows");
}
}Java在Linux與windows系統(tǒng)下獲取主板序列號,cpu序列號以及mac地址
概述:
實現(xiàn)了獲取當前操作系統(tǒng)名稱,主板序列號,CPU序列號,mac地址的相關方法函數(shù)。
應對的場景是信創(chuàng)設備無法正常識別我們的加密狗,對于軟件license的限制,我們通過系統(tǒng)當前日期以及綁定對方設備進行限制。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DmcUtils {
/**
* 獲取當前操作系統(tǒng)名稱
*/
public static String getOSName() {
return System.getProperty("os.name").toLowerCase();
}
// 主板序列號 windows
public static String getMainBordId_windows() {
String result = "";
try {
File file = File.createTempFile("realhowto", ".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
+ "Set colItems = objWMIService.ExecQuery _ \n" + " (\"Select * from Win32_BaseBoard\") \n"
+ "For Each objItem in colItems \n" + " Wscript.Echo objItem.SerialNumber \n"
+ " exit for ' do the first cpu only! \n" + "Next \n";
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
} catch (Exception e) {
System.out.print("獲取主板信息錯誤");
}
return result.trim();
}
// 主板序列號 linux
public static String getMainBordId_linux() {
String result = "";
String maniBord_cmd = "dmidecode | grep 'Serial Number' | awk '{print $3}' | tail -1";
Process p;
try {
p = Runtime.getRuntime().exec(new String[] { "sh", "-c", maniBord_cmd });// 管道
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
result += line;
break;
}
br.close();
} catch (IOException e) {
System.out.print("獲取主板信息錯誤");
}
return result;
}
/**
* 獲取mac地址 (如果Linux下有eth0這個網(wǎng)卡)
*/
public static String getMAC_linux() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
// linux下的命令,一般取eth0作為本地主網(wǎng)卡
process = Runtime.getRuntime().exec("ifconfig eth0");
// 顯示信息中包含有mac地址信息
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
// 尋找標示字符串[hwaddr]
index = line.toLowerCase().indexOf("hwaddr");
if (index >= 0) {// 找到了
// 取出mac地址并去除2邊空格
mac = line.substring(index + "hwaddr".length() + 1).trim();
break;
}
}
} catch (IOException e) {
System.out.print("獲取mac信息錯誤");
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
System.out.print("獲取mac信息錯誤");
}
bufferedReader = null;
process = null;
}
return mac;
}
/*
* 獲取Linux的mac
*/
public static String getMAC_linuxs() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
// linux下的命令,一般取eth0作為本地主網(wǎng)卡
process = Runtime.getRuntime().exec("ifconfig");
// 顯示信息中包含有mac地址信息
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null)
{
Pattern pat = Pattern.compile("\\b\\w+:\\w+:\\w+:\\w+:\\w+:\\w+\\b");
Matcher mat= pat.matcher(line);
if(mat.find())
{
mac=mat.group(0);
}
}
} catch (IOException e) {
System.out.print("獲取mac信息錯誤");
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
System.out.print("獲取mac信息錯誤");
}
bufferedReader = null;
process = null;
}
return mac;
}
/**
* 獲取widnows網(wǎng)卡的mac地址.
*/
public static String getMAC_windows() {
InetAddress ip = null;
NetworkInterface ni = null;
List<String> macList = new ArrayList<String>();
try {
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
ni = (NetworkInterface) netInterfaces.nextElement();
// ----------特定情況,可以考慮用ni.getName判斷
// 遍歷所有ip
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ip = (InetAddress) ips.nextElement();
if (!ip.isLoopbackAddress() // 非127.0.0.1
&& ip.getHostAddress().matches("(\\d{1,3}\\.){3}\\d{1,3}")) {
macList.add(getMacFromBytes(ni.getHardwareAddress()));
}
}
}
} catch (Exception e) {
System.out.print("獲取mac信息錯誤");
}
if (macList.size() > 0) {
return macList.get(0);
} else {
return "";
}
}
private static String getMacFromBytes(byte[] bytes) {
StringBuffer mac = new StringBuffer();
byte currentByte;
boolean first = false;
for (byte b : bytes) {
if (first) {
mac.append("-");
}
currentByte = (byte) ((b & 240) >> 4);
mac.append(Integer.toHexString(currentByte));
currentByte = (byte) (b & 15);
mac.append(Integer.toHexString(currentByte));
first = true;
}
return mac.toString().toUpperCase();
}
/**
* 獲取CPU序列號 Windows
*
* @return
*/
public static String getCPUID_Windows() {
String result = "";
try {
File file = File.createTempFile("tmp", ".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
+ "Set colItems = objWMIService.ExecQuery _ \n" + " (\"Select * from Win32_Processor\") \n"
+ "For Each objItem in colItems \n" + " Wscript.Echo objItem.ProcessorId \n"
+ " exit for ' do the first cpu only! \n" + "Next \n";
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
file.delete();
} catch (Exception e) {
System.out.print("獲取mac信息錯誤");
}
return result.trim();
}
/**
* 獲取CPU序列號 linux
*
* @return
*/
public static String getCPUID_linux() throws InterruptedException {
String result = "";
String CPU_ID_CMD = "dmidecode";
BufferedReader bufferedReader = null;
Process p = null;
try {
p = Runtime.getRuntime().exec(new String[] { "sh", "-c", CPU_ID_CMD });// 管道
bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
// 尋找標示字符串[hwaddr]
index = line.toLowerCase().indexOf("uuid");
if (index >= 0) {// 找到了
// 取出mac地址并去除2邊空格
result = line.substring(index + "uuid".length() + 1).trim();
break;
}
}
} catch (IOException e) {
System.out.print("獲取mac信息錯誤");
}
return result.trim();
}
public static void main(String [] args) throws Exception {
System.out.println("開始獲?。?);
String cpuId=getCPUID_linux();
System.out.println("linux cpuId:"+cpuId);
//String cpuId= getCPUID_Windows();
//System.out.println("Windows cpuId:"+cpuId);
String bordId= getMainBordId_linux();
System.out.println("linux bordId:"+bordId);
//String bordId= getMainBordId_windows();
//System.out.println("Windows bordId:"+bordId);
System.out.println("獲取結束!");
}
}總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot全局Controller返回值格式統(tǒng)一
本文主要介紹了SpringBoot全局Controller返回值格式統(tǒng)一,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07
詳解java.lang.reflect.Modifier.isInterface()方法
這篇文章主要介紹了詳解java.lang.reflect.Modifier.isInterface()方法的相關資料,這里提供實例幫助大家理解這個方法的使用,需要的朋友可以參考下2017-09-09
Spring?Data?JPA框架的Repository自定義實現(xiàn)詳解
Spring?Data?JPA是Spring基于JPA規(guī)范的基礎上封裝的?套?JPA?應?框架,可使開發(fā)者?極簡的代碼即可實現(xiàn)對數(shù)據(jù)庫的訪問和操作,本篇我們來了解Spring?Data?JPA框架的Repository自定義實現(xiàn)2022-04-04
Mybatis遷移到Mybatis-Plus的實現(xiàn)方法
這篇文章主要介紹了Mybatis遷移到Mybatis-Plus的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08

