日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

centos查询 硬盘序列号查询_关于使用java执行shell脚本获取centos的硬盘序列号和mac地址...

發(fā)布時(shí)間:2024/8/1 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 centos查询 硬盘序列号查询_关于使用java执行shell脚本获取centos的硬盘序列号和mac地址... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。