1.获取硬盘序列号:

新建shell脚本文件: identifier.sh, 内容为:

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

调整identifier.sh的权限:

 chmod +x identifier.sh

使用Java代码去调用该shell脚本获取结果

 private static String getIdentifier() throws Exception {
String path = "/usr/local/webapp/identifier.sh";
Process process = Runtime.getRuntime().exec(path);
process.waitFor(); BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null){
sb.append(line);
}
String str = sb.toString();
return str;
}

2. 获取MAC地址:

新建shell脚本文件: macAddress.sh, 内容为:

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

调整macAddress.sh的权限:

 chmod +x macAddress.sh

使用Java代码去调用该shell脚本获取结果

 private static String getMACAddress() throws Exception {
String path = "/usr/local/webapp/macAddress.sh";
Process process = Runtime.getRuntime().exec(path);
process.waitFor(); BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null){
sb.append(line);
}
String str = sb.toString();
return str;
}

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

测试:

 public static void main(String[] args) throws Exception {

     System.out.println("==========kaishi==========");
String macAddress = getMACAddress();
System.out.println("macAddress is: " + macAddress); String identifier = getIdentifier();
System.out.println("identifier is: " + identifier); String uniquelyID = macAddress + "_" + identifier;
System.out.println("uniquelyID is: " + uniquelyID);
System.out.println("==========jieshu=========="); }

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

输出:

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

使用java代码执行Linux命令:

1. 执行 "ifconfig" 命令

     private static String getMacAddress() throws Exception {
String[] cmd = {"ifconfig"}; Process process = Runtime.getRuntime().exec(cmd);
process.waitFor(); BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
} return str1;
}

2. 执行 "fdisk -l" 命令

     private static String getIdentifier() throws Exception {
String[] cmd = {"fdisk", "-l"}; Process process = Runtime.getRuntime().exec(cmd);
process.waitFor(); BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
} String str1 = sb.toString(); return str1;
}

关于使用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. vscode 打开新文件覆盖窗口,始终显示一个窗口

    一直在使用vscode 编辑器,里面的扩展用的比较舒服,但是最近遇到一个小问题,一直也没有找好的解决办法,今天无意中把问题给解决了.具体如下 之前使用编辑器,可以同时打开多个文件,而且是多窗口展示的, ...

  2. 假面舞会(codevs 1800)

    题目描述 Description 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会. 今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选择 一个自己喜欢的面具.每个面具都有一 ...

  3. POJ 3615 Cow Hurdles

    http://poj.org/problem?id=3615 floyd 最短路径的变形 dist[i][j]变化为 : i j之间的最大边 那么输入的时候可以直接把dist[i][j] 当作i j ...

  4. C#路径,文件,目录,I/O常见操作汇总

    原文发布时间为:2008-10-25 -- 来源于本人的百度文章 [由搬家工具导入] 路径,文件,目录,I/O常见操作汇总 摘要:     文件操作是程序中非常基础和重要的内容,而路径、文件、目录以及 ...

  5. 【zTree】zTree根据后台数据生成树并动态设置前面的节点复选框的选中状态

    0.页面中准备树的ul <ul id="treeDemo10" class="ztree" style="display: none;" ...

  6. Speculative store buffer

    A speculative store buffer is speculatively updated in response to speculative store memory operatio ...

  7. React学习及实例开发(一)——开始

    本文基于React v16.4.1 初学react,有理解不对的地方,欢迎批评指正^_^ 一.构建一个新项目 1.命令行运行如下命令,构建一个新的react项目 npm install -g crea ...

  8. 最少拦截系统-----hdu1257(dp+最长上升子序列)

    Problem Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高 ...

  9. Linux下异常信号

    我们介绍一些标准信号的名称以及它们代表的事件.每一个信号名称是一个代表正整数的宏,但是你不要试图去推测宏代表的具体数值,而是直接使用名称.这是因为这个数值会随不同的系统或同样系统的不同版本而不同,但是 ...

  10. freeswitch对媒体的处理的三种方式

    一.默认方式:媒体通过freeswitch, RTP被freeswtich转发, freeswitch控制编码的协商并在协商不一致时提供语音编码转换能力, 支持录音,二次拨号等.   二.代理模式: ...