關於使用java執行shell腳本獲取centos的硬盤序列號和mac地址


1.獲取硬盤序列號:

新建shell腳本文件: identifier.sh, 內容為:

1 diskdata=`fdisk -l`
2 diskleft=${diskdata#*"identifier: "}
3 identifier=${diskleft%%" Device Boot"*}
4 echo ${identifier}

調整identifier.sh的權限:

1 chmod +x identifier.sh

使用Java代碼去調用該shell腳本獲取結果

 1 private static String getIdentifier() throws Exception {
 2     String path = "/usr/local/webapp/identifier.sh";
 3     Process process = Runtime.getRuntime().exec(path);
 4     process.waitFor();
 5 
 6     BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
 7     StringBuffer sb = new StringBuffer();
 8     String line;
 9     while ((line = br.readLine()) != null){
10         sb.append(line);
11     }
12     String str = sb.toString();
13     return str;
14 }

 

2. 獲取MAC地址:

新建shell腳本文件: macAddress.sh, 內容為:

1 macAddress=`ifconfig | awk -F'[ :]+' '!NF{if(eth!=""&&ip=="")print eth;eth=ip4=""}/^[^ ]/{eth=$1}/inet addr:/{ip=$4}'`
2 ifconfig ${macAddress[1]} | grep "ether" | awk '{print $2}'

調整macAddress.sh的權限:

1 chmod +x macAddress.sh

使用Java代碼去調用該shell腳本獲取結果

 1 private static String getMACAddress() throws Exception {
 2     String path = "/usr/local/webapp/macAddress.sh";
 3     Process process = Runtime.getRuntime().exec(path);
 4     process.waitFor();
 5 
 6     BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
 7     StringBuffer sb = new StringBuffer();
 8     String line;
 9     while ((line = br.readLine()) != null){
10         sb.append(line);
11     }
12     String str = sb.toString();
13     return str;
14 }

===============================================

測試:

 1 public static void main(String[] args) throws Exception {
 2 
 3     System.out.println("==========kaishi==========");
 4     String macAddress = getMACAddress();
 5     System.out.println("macAddress is: " + macAddress);
 6     
 7     String identifier = getIdentifier();
 8     System.out.println("identifier is: " + identifier);
 9     
10     String uniquelyID = macAddress + "_" + identifier;
11     System.out.println("uniquelyID is: " + uniquelyID);
12     System.out.println("==========jieshu==========");
13 
14 }

===============================================

輸出:

==========kaishi==========
macAddress is: **:**:**:**:**:**
identifier is: *x********
uniquelyID is: **:**:**:**:**:**_*x********
==========jieshu==========

 

使用java代碼執行Linux命令:

1. 執行 "ifconfig" 命令

 1     private static String getMacAddress() throws Exception {
 2         String[] cmd = {"ifconfig"};
 3 
 4         Process process = Runtime.getRuntime().exec(cmd);
 5         process.waitFor();
 6 
 7         BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
 8         StringBuffer sb = new StringBuffer();
 9         String line;
10         while ((line = br.readLine()) != null) {
11             sb.append(line);
12         }
13 
14         return str1;
15     }

 2. 執行 "fdisk -l" 命令

 1     private static String getIdentifier() throws Exception {
 2         String[] cmd = {"fdisk", "-l"};
 3 
 4         Process process = Runtime.getRuntime().exec(cmd);
 5         process.waitFor();
 6 
 7         BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
 8         StringBuffer sb = new StringBuffer();
 9         String line;
10         while ((line = br.readLine()) != null) {
11             sb.append(line);
12         }
13 
14         String str1 = sb.toString();
15 
16         return str1;
17     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM