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. springboot注释详解

    1.属性注入 @ConfigurationProperties(prefix="...") spring会从classpath下的/config目录或者classpath的根目录查 ...

  2. hdu 4819 Mosaic

    无论是线段树还是树状数组维护最大值最小值的时候一定要注意,如果有修改操作的话,这个最小值和最大值的更新一定不能由原来的和修改的值得到,一定要重新查询一次,否则可能出现当前最小值是原来的未修改值,但事实 ...

  3. easyUI排序问题

    使用easyUI时,需要在点击页面的某一列进行desc或asc排序,那么在jsp中可以把该列js的sortable 设置true. 加在某字段上时,该字段点击时页面会出现一小三角图案 ,此时easyU ...

  4. arcgis安装路径的获得

    //Get the ArcGIS install location string sInstall = ESRI.ArcGIS.RuntimeManager.ActiveRuntime.Path; / ...

  5. c++之函数对象、bind函数

    函数对象实质上是一个实现了operator()--括号操作符--的类. class Add { public: int operator()(int a, int b) { return a + b; ...

  6. 【APUE】wait与waitpid函数

    当一个进程终止时,内核就向其父进程发送SIGCHLD信号.因为子进程终止是个异步事件,所以这种信号也是内核向父进程发的异步通知.父进程可以选择忽略该信号,或者提供一个该信号发生时即被调用执行的函数.对 ...

  7. There is no PasswordEncoder mapped for the id "null"

    There is no PasswordEncoder mapped for the id "null" 学习了:https://blog.csdn.net/dream_an/ar ...

  8. java实现从报文中获取投保单号

    java实现从报文中获取投保单号 投保单号正则表达式: String regex = "<proposalNo>([0-9]+)</proposalNo>[\\s\\ ...

  9. boost的内存管理

    smart_ptr raii ( Resource Acquisition Is Initialization ) 智能指针系列的都统称为smart_ptr.包含c++98标准的auto_ptr 智能 ...

  10. VUEX action解除页面耦合

    最近项目中需要用到vue+vuex来实现登出跳转功能,老大指派任务要用action解除页面耦合,刚从vue深渊晕晕乎乎爬出来的我是一脸懵逼啊...啥是解除耦合...网上vuex的资料太少了,vuex手 ...