关于使用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     }

 

转载于:https://www.cnblogs.com/yanwu0527/p/8984201.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值