关于使用java执行shell脚本获取centos的硬盘序列号和mac地址
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地址的更多相关文章
- Java执行Shell脚本
Linux 系统下采用 Java 执行 Shell 脚本,直接上代码: package com.smbea.demo; import java.io.BufferedReader; import ja ...
- Java执行shell脚本并返回结果两种方法的完整代码
Java执行shell脚本并返回结果两种方法的完整代码 简单的是直接传入String字符串,这种不能执行echo 或者需要调用其他进程的命令(比如调用postfix发送邮件命令就不起作用) 执行复杂的 ...
- 利用java执行shell脚本
BPMN中存在由系统执行的脚本任务,shell脚本任务也是该系统任务脚本中的一种,利用的也是由java执行shell脚本. 代码中的ProcessBuilder类,为java.lang.Process ...
- Java 执行Shell脚本指令
一.介绍 有时候我们在Linux中运行Java程序时,需要调用一些Shell命令和脚本.而Runtime.getRuntime().exec()方法给我们提供了这个功能,而且Runtime.getRu ...
- Java执行Shell脚本“No such file or directory” (win->Linux)异常的可能原因
转自:http://blog.csdn.net/zlpdaisy/article/details/6134314 用Runtime.getRuntime().exec()方法执行Linux的一个She ...
- java执行shell脚本并输出执行情况
1.脚本test.sh,置于/Users/hdwang目录下 #!/bin/sh cd /Users/hdwang echo ls:`ls` ;i<=;i++)); do + ); sleep ...
- 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 ...
- C# 获取本机CPU序列号,MAC地址,硬盘ID,本机IP地址,计算机名,物理内存,PC类型
首先引入服务 然后 调用 本文转载自http://blog.sina.com.cn/s/blog_7eeb43210101hf7f.html public class Computer { publi ...
- Java执行Dos-Shell脚本
Java执行Dos-Shell脚本 1.介绍 2.调用shell脚本 2.1 获取键盘输入 2.2 构建指令 2.3 Java代码 3.Java调用Shell并传入参数 4.Java调用远程的Shel ...
随机推荐
- SkyWalking 分布式追踪系统
随着微服务架构的流行,一些微服务架构下的问题也会越来越突出,比如一个请求会涉及多个服务,而服务本身可能也会依赖其他服务,整个请求路径就构成了一个网状的调用链,而在整个调用链中一旦某个节点发生异常,整个 ...
- 牛客网暑期ACM多校训练营(第六场)G
https://www.nowcoder.com/acm/contest/144/G 链接:https://www.nowcoder.com/acm/contest/144/G来源:牛客网 In Vi ...
- Sudoku---hdu2676(数独DFS)
http://poj.org/problem?id=2676 递归深搜 #include<stdio.h> #include<string.h> #include<alg ...
- Mac BOOK PRO U盘安装windows7、8及8.1
http://v.youku.com/v_show/id_XMTI1NjgzMzU0NA==.html http://jingyan.baidu.com/article/1709ad80b3d2f44 ...
- java大文件的分割和合并
原文:http://www.open-open.com/code/view/1441679313430 import java.io.File; import java.io.FileInputStr ...
- 外排序 & 败者树 & 多路归并-学习
来来来,根据这篇文章,学一下败者树吧: http://blog.csdn.net/whz_zb/article/details/7425152 一.胜者树 胜者树的一个优点是,如果一个选手的值改变了, ...
- curl 发送post请求
curl 发送post请求 curl -X POST "http://localhost:8080/usr3?id=1&name=3&departmentId=2" ...
- Java静态分派与动态分派(二)
方法调用并不等于方法执行,方法调用阶段唯一的任务就是确定被调用方法的版本(即调用哪一个方法),暂时还不涉及方法内部的具体运行过程. 在程序运行时,进行方法调用是最普遍.最频繁的操作,但是Class文件 ...
- LuaInterface简单介绍
LuaInterface简单介绍 Lua是一种非常好的扩展性语言.Lua解释器被设计成一个非常easy嵌入到宿主程序的库.LuaInterface则用于实现Lua和CLR的混合编程. (一)Lua f ...
- FancyCoverFlow
https://github.com/davidschreiber/FancyCoverFlow