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[]} | 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. }

关于使用java执行shell脚本获取centos的硬盘序列号和mac地址的更多相关文章

  1. Java执行Shell脚本

    Linux 系统下采用 Java 执行 Shell 脚本,直接上代码: package com.smbea.demo; import java.io.BufferedReader; import ja ...

  2. Java执行shell脚本并返回结果两种方法的完整代码

    Java执行shell脚本并返回结果两种方法的完整代码 简单的是直接传入String字符串,这种不能执行echo 或者需要调用其他进程的命令(比如调用postfix发送邮件命令就不起作用) 执行复杂的 ...

  3. 利用java执行shell脚本

    BPMN中存在由系统执行的脚本任务,shell脚本任务也是该系统任务脚本中的一种,利用的也是由java执行shell脚本. 代码中的ProcessBuilder类,为java.lang.Process ...

  4. Java 执行Shell脚本指令

    一.介绍 有时候我们在Linux中运行Java程序时,需要调用一些Shell命令和脚本.而Runtime.getRuntime().exec()方法给我们提供了这个功能,而且Runtime.getRu ...

  5. Java执行Shell脚本“No such file or directory” (win->Linux)异常的可能原因

    转自:http://blog.csdn.net/zlpdaisy/article/details/6134314 用Runtime.getRuntime().exec()方法执行Linux的一个She ...

  6. java执行shell脚本并输出执行情况

    1.脚本test.sh,置于/Users/hdwang目录下 #!/bin/sh cd /Users/hdwang echo ls:`ls` ;i<=;i++)); do + ); sleep ...

  7. java 执行 shell脚本通过mysql load data导入数据

    1:load_data_test.sh #!/bin/sh load_data_log=/mnt/load_data_test/load.log load_sql="LOAD DATA LO ...

  8. C# 获取本机CPU序列号,MAC地址,硬盘ID,本机IP地址,计算机名,物理内存,PC类型

    首先引入服务 然后 调用 本文转载自http://blog.sina.com.cn/s/blog_7eeb43210101hf7f.html public class Computer { publi ...

  9. Java执行Dos-Shell脚本

    Java执行Dos-Shell脚本 1.介绍 2.调用shell脚本 2.1 获取键盘输入 2.2 构建指令 2.3 Java代码 3.Java调用Shell并传入参数 4.Java调用远程的Shel ...

随机推荐

  1. hdu4115:Eliminate the Conflict

    n<=10000局剪刀石头布,对面第i局出Ai,m<=10000种对你出什么提出的要求:Xi Yi Wi 表示第Xi局和第Yi局,Wi=1:必须不同:Wi=0:必须相同,问是否存在你一局都 ...

  2. Mysql相关工具

    •Mysql相关工具 –Mysqlslap  压力测试工具 –Mysqlsla  日志分析工具 –Mysqlreport   效能监控工具 –Mysqlproxy  快速实现读写分离以及负载均衡 –p ...

  3. 2015轻院校赛 B 迷宫 (bfs)

    http://acm.zznu.edu.cn/problem.php?id=1967 这套题的有毒   我交了好多遍才对 坑:机关要按照顺序走 并且在走这个机关之前不能走这个机关  但是能穿过这个机关 ...

  4. Edit Distance(动态规划,难)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  5. codechef Taxi Driver

    题意: 给N个点求任意两个点的“距离”总和: A,B的“距离”定义为:min(|ax-bx|,|ay-by|) (n<200000) 好题! 解析: 看着没思路 先是公式化简:让 ax=sx+s ...

  6. spring mvc技术

                                                     spring mvc之访问路径        1. @RequestMapping这个注解  在实际项 ...

  7. xml解析工具mashaller javaee自带解析类

    1.怎样去掉Marshaller的格式化? : JAXBContext context = JAXBContext.newInstance(Entity.class); Marshaller mars ...

  8. MySQL入门笔记 - 视图

    参考书籍<MySQL入门很简单> 1.视图定义 视图是从一个或者多个表中导出来的虚拟的表,透过这个窗口可以看到系统专门提供的数据,使用户可以只关心对自己有用的数据,方便用户对数据操作,同时 ...

  9. Google C++ style guide——C++类

    1.构造函数的职责 构造函数中仅仅进行那些没有实际意义的初始化.由于成员变量的"有意义"的值大多不在构造函数中确定. 能够的话,使用Init()方法集中初始化为有意义的数据. 长处 ...

  10. C++开发人脸性别识别教程(8)——搭建MFC框架之读取目录信息

    在上一篇博客中我们已经绘制了MFC界面,在这篇博客中我们将加入响应代码,为MFC框架加入一个最主要的功能:打开一个目录. 一.加入相关头文件 这里头文件主要包括三类:opencv头文件.批量读取文件相 ...