在Linux操作系统上执行java代码,执行交互式命令

在Linux操作系统上执行java代码,执行交互式命令(真的是痛苦了好久。。。)

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.Match;
import expect4j.matches.RegExpMatch;

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

	private static Expect4j expect = null;
	
	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 cmdLine2="hprest";
		String cmdArgs2="list xxxxxxxxxxxxxxxxxx\n";
		TestLocals(cmdLine2, cmdArgs2);
	}
	public static void TestLocals(String cmdLine1,String cmdArgs1) throws Exception {
		expect = spawn(cmdLine1,cmdArgs1);
		executeCommands();
		System.out.println(getResponse());
	}
	
	protected static Expect4j spawn(String cmdArgs,String cmdArgs2) throws Exception {
		ProcessBuilder pd;
        Process process = Runtime.getRuntime().exec(cmdArgs);
        pd=new ProcessBuilder(cmdArgs);
        pd.redirectErrorStream(true);
		process = pd.start();
        InputStream is = process.getInputStream();
        OutputStream os = process.getOutputStream();
        Expect4j expect = new Expect4j(is,os);
        System.out.println("aaaaaaaaaaaaaaaaaa");
        expect.expect("$");
        expect.send(cmdArgs2);
        System.out.println("bbbbbbbbbbbbbbbbbbb");
        expect.expect("$");
        expect.send("loginout\n");
        expect.close();
        
        System.out.println("cccccccccccccccccccc");
        
        return expect;
    }
	
	
	
	protected static boolean executeCommands() {
		Closure closure = new Closure() {
			public void run(ExpectState expectState) throws Exception {
				buffer.append(expectState.getBuffer());// buffer is string buffer for appending output of executed command
				expectState.exp_continue();
			}
		};
		log.debug(buffer.toString());
		List<Match> lstPattern = new ArrayList<Match>();
		String[] regEx = linuxPromptRegEx;
		for (String regexElement : regEx) {
		RegExpMatch mat;
		try {
			mat = new RegExpMatch(regexElement, closure);
			lstPattern.add(mat);
			log.debug(lstPattern.toString());
		} catch (MalformedPatternException e) {
			e.printStackTrace();
		}
		}
		try {
		boolean isSuccess = true;
		isSuccess = !checkResult(expect.expect(lstPattern));
			return isSuccess;
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
	}

	// 检查执行返回的状态
	protected static boolean checkResult(int intRetVal) {
		if (intRetVal == -2) {
			return true;
		}
		return false;
	}

	/**
	 * 获取服务器返回的信息
	 * 
	 * @return 服务端的执行结果
	 */
	protected static String getResponse() {
		return buffer.toString();
	}
	
	
	
	
	
}
工程中,这部分代码运行非常缓慢,原因还暂时不得而知。。。

        expect.expect("$");
        expect.send(cmdArgs2);
        System.out.println("bbbbbbbbbbbbbbbbbbb");
        expect.expect("$");
        expect.send("loginout\n");
        expect.close();

给大家参考的同时,也希望给予意见。

posted @ 2022-01-27 18:37  zhangdaopin  阅读(411)  评论(0编辑  收藏  举报