centos查询 硬盘序列号查询_关于使用java执行shell脚本获取centos的硬盘序列号和mac地址...
1.獲取硬盤序列號(hào):
新建shell腳本文件:?identifier.sh, 內(nèi)容為:
1 diskdata=`fdisk -l`2 diskleft=${diskdata#*"identifier:"}3 identifier=${diskleft%%"Device Boot"*}4 echo ${identifier}
調(diào)整identifier.sh的權(quán)限:
1 chmod +x identifier.sh
使用Java代碼去調(diào)用該shell腳本獲取結(jié)果
1 private staticString 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(newInputStreamReader(process.getInputStream()));7 StringBuffer sb = newStringBuffer();8 String line;9 while ((line = br.readLine()) != null){10 sb.append(line);11 }12 String str =sb.toString();13 returnstr;14 }
2. 獲取MAC地址:
新建shell腳本文件:?macAddress.sh, 內(nèi)容為:
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}'
調(diào)整macAddress.sh的權(quán)限:
1 chmod +x macAddress.sh
使用Java代碼去調(diào)用該shell腳本獲取結(jié)果
1 private staticString 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(newInputStreamReader(process.getInputStream()));7 StringBuffer sb = newStringBuffer();8 String line;9 while ((line = br.readLine()) != null){10 sb.append(line);11 }12 String str =sb.toString();13 returnstr;14 }
===============================================
測試:
1 public static voidmain(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代碼執(zhí)行Linux命令:
1. 執(zhí)行 "ifconfig" 命令
1 private staticString getMacAddress() throws Exception {2 String[] cmd = {"ifconfig"};3
4 Process process =Runtime.getRuntime().exec(cmd);5 process.waitFor();6
7 BufferedReader br = new BufferedReader(newInputStreamReader(process.getInputStream()));8 StringBuffer sb = newStringBuffer();9 String line;10 while ((line = br.readLine()) != null) {11 sb.append(line);12 }13
14 returnstr1;15 }
2. 執(zhí)行 "fdisk -l" 命令
1 private staticString 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(newInputStreamReader(process.getInputStream()));8 StringBuffer sb = newStringBuffer();9 String line;10 while ((line = br.readLine()) != null) {11 sb.append(line);12 }13
14 String str1 =sb.toString();15
16 returnstr1;17 }
總結(jié)
以上是生活随笔為你收集整理的centos查询 硬盘序列号查询_关于使用java执行shell脚本获取centos的硬盘序列号和mac地址...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LaserJet 5000 及 5100
- 下一篇: 医学图像数据集汇总