Linux上使用java代码实现Local Interactive Commands(such as:hprest;hpssacli)

本文介绍了一种在Linux系统中实现本地交互式命令的方法,通过Java代码调用本地命令并处理输出,适用于需要自动化执行shell命令的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现Local Interactive Commands(本地的交互式命令)可以使用脚本方式:

也可以使用java代码在linux上实现:(此方法要在linux系统上运行,windows系统是不可以的)

http://blog.youkuaiyun.com/itzhangdaopin/article/details/53905385 有java打jar包在Linux上运行的例子,可供参考。

package com;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.oro.text.regex.MalformedPatternException;

import expect4j.Closure;
import expect4j.Expect4j;
import expect4j.ExpectState;
import expect4j.matches.EofMatch;
import expect4j.matches.Match;
import expect4j.matches.RegExpMatch;
import expect4j.matches.TimeoutMatch;

public class TestLocal {
	
	private static Logger log = Logger.getLogger(TestLocal.class);

	private static Expect4j expect = null;
	private static final long defaultTimeOut = 2000;
	private static StringBuffer buffer = new StringBuffer();
	
	public static String[] linuxPromptRegEx = new String[] { "~]#", "~#", "#", ":~#", "/$", ">" };
	public static String[] errorMsg = new String[] { "could not acquire the config lock " };
	
	public static void main(String[] args) throws Exception {
		String[] cmdArgs1={"hprest","help","logout"};
		expect = spawn(cmdArgs1);
		executeCommands(cmdArgs1);
		System.out.println(getResponse());
		disconnect();
	}
	
	protected static Expect4j spawn(String[] cmdArgs) throws Exception {
		ProcessBuilder pd;
        Process process = Runtime.getRuntime().exec(cmdArgs[0]);//取出第一个元素来进行运行(hprest;hpssacli)
        pd=new ProcessBuilder(cmdArgs[0]);
        pd.redirectErrorStream(true);
        process = pd.start();
        InputStream is = process.getInputStream();
        OutputStream os = process.getOutputStream();
        Expect4j expect = new Expect4j(is,os);
        
        return expect;
    }
	protected static void disconnect() {
	    	if (expect != null) {
	    		expect.close();
	    	}
		}
	
	
	public static boolean executeCommands(String[] commands) {
		
		if (expect == null) {
			return false;
		}

		log.debug("----------Running commands are listed as follows:----------");
		for (String command : commands) {
			log.debug(command);
			System.out.println(command);
		}
		log.debug("----------End----------");

		Closure closure = new Closure() {
			public void run(ExpectState expectState) throws Exception {
				buffer.append(expectState.getBuffer());
				expectState.exp_continue();

			}
		};
		List<Match> lstPattern = new ArrayList<Match>();
		String[] regEx = linuxPromptRegEx;
		if (regEx != null && regEx.length > 0) {
			synchronized (regEx) {
				// list of regx like, :>, /> etc. it is possible command prompts of your remote machine
				for (String regexElement : regEx) {
					try {
						RegExpMatch mat = new RegExpMatch(regexElement, closure);
						lstPattern.add(mat);
					} catch (MalformedPatternException e) {
						return false;
					} catch (Exception e) {
						return false;
					}
				}
				lstPattern.add(new EofMatch(new Closure() {
					public void run(ExpectState state) {
					}
				}));
				lstPattern.add(new TimeoutMatch(defaultTimeOut, new Closure() {
					public void run(ExpectState state) {
					}
				}));
			}
		}
		for (String regexElement : regEx) {
		RegExpMatch mat;
		try {
			mat = new RegExpMatch(regexElement, closure);
			lstPattern.add(mat);
		} catch (MalformedPatternException e) {
			e.printStackTrace();
		}
		}
		try {
			boolean isSuccess = true;
			//for (String strCmd : commands) {
			for (int i = 1; i < commands.length; i++) {//从第二个元素开始遍历,在第一个元素的条件下(如:hprest> xxx)运行命令
				String strCmd=commands[i];
				isSuccess = isSuccess(lstPattern, strCmd);
			}
			
			isSuccess = !checkResult(expect.expect(lstPattern));
			
			String response = buffer.toString().toLowerCase();
			for (String msg : errorMsg) {
				if (response.indexOf(msg) > -1) {
					return false;
				}
			}

			return isSuccess;
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
	}
	private static boolean isSuccess(List<Match> objPattern, String strCommandPattern) {
		try {
			boolean isFailed = checkResult(expect.expect(objPattern));
			if (!isFailed) {
				expect.send(strCommandPattern);
				expect.send("\n");
				expect.send("\r");
				return true;
			}
			return false;
		} catch (MalformedPatternException ex) {
			return false;
		} catch (Exception ex) {
			return false;
		}
	}

	private static boolean checkResult(int intRetVal) {
		if (intRetVal == -2) {
			return true;
		}
		return false;
	}

	public static String getResponse() {
		return buffer.toString();
	}
	
	
	
	
}


HP官网下载的磁盘阵列操作工具,直接下载,在Linux上安装使用 hpacucli、hpssacli 是HP服务器特有的阵列卡管理工具,官方提供的rpm包直接安装即可使用。hpacucli工具支持的阵列卡型号如下: Smart Array products: Smart Array 5312 Controller Smart Array 5302 Controller Smart Array 5304 Controller Smart Array 532 Controller Smart Array 5i Controller Smart Array 641 Controller Smart Array 642 Controller Smart Array 6400 Controller Smart Array 6400 EM Controller Smart Array 6i Controller Smart Array P600 Controller Smart Array P400 Controller Smart Array P400i Controller Smart Array E200 Controller Smart Array E200i Controller Smart Array P800 Controller Smart Array E500 Controller Smart Array P700m Controller Smart Array P410i Controller Smart Array P411 Controller Smart Array P212 Controller Smart Array P712m Controller Smart Array B110i SATA RAID Smart Array P812 Controller Smart Array P220i Controller Smart Array P222 Controller Smart Array P420 Controller Smart Array P420i Controller Smart Array P421 Controller Smart Array P822 Controller Smart Array P721m Controller Dynamic Smart Array B320i RAID Dynamic Smart Array B120i RAID MSA products: MSA500 Controller MSA500 G2 Controller MSA1000 Controller MSA1500 CS Controller MSA20 Controller 命令用法如下: hpacucli ctrl all show config detail 查看阵列详细信息、配置 hpacucli ctrl all show 查看所有控制器状态 hpacucli ctrl slot=0 show config detail 查看slot 0阵列信息详细状态 hpacucli ctrl slot=0 logicaldrive 2 show 逻辑磁盘信息 hpacucli ctrl slot=0 physicaldrive 1I:2:1 show(物理磁盘信息) hpacucli ctrl slot=0 create type=ld drives=1I:1:3,1I:1:4,2I:1:5,2I:1:6 raid=1+0 用3,4,5,6号盘创建一个raid1+0阵列 hpacucli ctrl slot=0 create type=ld drives=1I:1:3,1I:1:4,2I:1:5 raid=5 用3,4,5号盘创建一个raid5阵列 hpacucli ctrl slot=0 create type=ld drives=1I:1:3 raid=0 用3号盘创建一个raid0阵列 hpacucli ctrl slot=0 array B delet
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值