关于使用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 ...
随机推荐
- k/3cloud表格控件块粘贴代码逻辑
大家可以在表单插件EntityBlockPasting事件中自己处理,然后将cancel设置为true.以下代码可以参考一下,插件代码中需要将其中一些属性或方法修改,例如this.BusinessIn ...
- 深入理解计算机操作系统——第10章:UNIX IO,打开,关闭,读写文件
系统级IO:输入输出是主存与外部设备(磁盘,终端,网络)之间拷贝数据的过程 输入:从IO设备拷贝数据到主存中 输出:从主存中拷贝数据到IO设备中 10.1 unix IO 所有的IO设备都被模型化为文 ...
- React学习及实例开发(三)——用react-router跳转页面
本文基于React v16.4.1 初学react,有理解不对的地方,欢迎批评指正^_^ 一.定义路由 1.安装react-router npm install react-router@ --sav ...
- HDU——1054 Strategic Game
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- DTrace C++ Mysteries Solved 转
I’ve been using DTrace on Leopard in my recent work, and while it’s a great tool, the C++ support ...
- how to read openstack code: action extension
之前我们看过了core plugin, service plugin 还有resource extension. resource extension的作用是定义新的资源.而我们说过还有两种exten ...
- 如何在Win7 x64上的配置32位的PostgreSQL ODBC数据源
在Win7 x64下安装最新版的PostgreSQL 9.x 后,从其官网下载最新的 ODBC驱动,分为普通的32位和64位版本,正常安装后,从已安装软件列表里可以看到两个版本的驱动都已经正确显示出来 ...
- Office文档如何转换 PDF 转 DOC XLS
1 使用Adobe Acrobat Pro,打开任意PDF都可以转换为XLSX格式(似乎没找到XLS) 2 如果你转换之后的东西无法打开,则先转换成DOC,然后再把DOC全选复制粘贴到XLS即可 ...
- Exception from container-launch: org.apache.hadoop.util.Shell$ExitCodeException
使用MapReduce编写的中文分词程序出现了 Exception from container-launch: org.apache.hadoop.util.Shell$ExitCodeExcept ...
- js 变量、函数提升
js 变量.函数提升 先简单理解下作用域的概念,方便对变量与函数提升的概念的理解 function foo() { var x = 1; if (x) { var x = 2; } console.l ...