关于使用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 ...
随机推荐
- 【ZJOI2017 Round1练习&BZOJ4767】D1T3 两双手(排列组合,DP)
题意: 100%的数据:|Ax|,|Ay|,|Bx|,|By| <= 500, 0 <= n,Ex,Ey <= 500 思路:听说这是一道原题 只能往右或者下走一步且有禁止点的简化版 ...
- BZOJ3126: [Usaco2013 Open]Photo
n<=200000个点,m<=100000个区间,每个区间有且仅有一个点,求最多几个点,无解-1. http://www.cnblogs.com/Chorolop/p/7570191.ht ...
- UVA12345 (带修改的莫队)
UVA12345 Dynamic len Problem : 给一个序列,每次询问一个区间里面的数字种类数量,或者修改某一个位置的值. Solution : 第一关键字分块排序左端点,第二关键字分块排 ...
- csu 1600: Twenty-four point
传送门 1600: Twenty-four point Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 490 Solved: 78[Submit][S ...
- SQL SERVER 2012 第五章 创建和修改数据表 の CREATE语句
CREATE <object type> <object name> CREATE DATABASE <database name> 比较完整的语法列表: 日志文件 ...
- poj——2771 Guardian of Decency
poj——2771 Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5916 ...
- hdu 3943
数位dp #include <cstdio> #include <cstdlib> #include <cmath> #include <stack> ...
- FIREDAC驱动ORACLE的配置
1)部署中间件所在的机器必须安装OCI 2)verdorlib,指定OCI所在路径
- GO语言 --socket.io
socket.io是对websocket的封装以及扩展, 可以跨平台使用, 具体可看官网.. GO语言实现: package main import ( "github.com/googol ...
- 配置activeMQ
一.加入以下的库 并配置好路径 ws2_32.lib;Mswsock.lib;cppunit.lib;libapr-1.lib;libapriconv-1.lib;libaprutil-1.lib;l ...