java链接linux 执行命令

/**

  • @author cosmo07

*/
package com.cosmosource.core.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import ch.ethz.ssh2.StreamGobbler;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class ExeLinuxCmdUtil {

private String userName;
private String password;
private String host;
static StringBuffer sb = new StringBuffer();
public ExeLinuxCmdUtil(String host, String userName, String password) {
	this.host = host;
	this.userName = userName;
	this.password = password;
}

private Session getSession() {
	JSch jsch = new JSch();
	Session session = null;
	try {
		session = jsch.getSession(userName, host);
		session.setPassword(password);

		Properties props = new Properties();
		props.put("StrictHostKeyChecking", "no");
		session.setConfig(props);
		session.connect();
	} catch (JSchException e) {
		e.printStackTrace();
	}
	return session;
}

public boolean exec(String[] cmds) {
	StringBuffer cmdBuffer = new StringBuffer();
	for (int i = 0; i < cmds.length; i++) {
		if (i != 3) {
			cmdBuffer.append(" ");
		}
		cmdBuffer.append(cmds[i]);
	}
	return exec(cmdBuffer.toString());
}

public boolean exec(String cmd) {
	Session session = getSession();
	Channel channel = null;
	InputStream in = null;
	try {
		channel = session.openChannel("exec");
		((ChannelExec) channel).setCommand(cmd);
		((ChannelExec) channel).setInputStream(null);
		((ChannelExec) channel).setErrStream(System.err); // 获取执行错误的信息
		in = channel.getInputStream();
		channel.connect();
		byte[] b = new byte[1024];
		int size = -1;
		while ((size = in.read()) > -1) {
			System.out.print(new String(b, 0, size)); // 打印执行命令的所返回的信息
			sb.append(new String(b, 0, size));

		}
		return true;
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		// 关闭流
		if (in != null) {
			try {
				in.close();
			} catch (IOException e) {
			}
		}
		// 关闭连接
		channel.disconnect();
		session.disconnect();
	}
	return false;
}


public static String exeLinuxCmd(String cmd,String ip,String uid,String pwd) {
	StringBuffer cmdBuffer = new StringBuffer();
	cmdBuffer.append(cmd);
	ExeLinuxCmdUtil ssh = new ExeLinuxCmdUtil(ip, uid, pwd);
	boolean flag=ssh.exec(cmdBuffer.toString());
	//System.exit(flag ? 0 : 1);
	return sb.toString();
}
public static void main(String[] args) {
	//exeLinuxCmd("mkdir /root/cdcd/iii.txt","192.168.0.157","root","123456");
	String ip="192.168.1.157";
	String uid="wxt";
	String pwd="123456";
	String cmd="gdm a.py > /home/wxt/a.txt";
	String cmd1="cat a.txt";
	//String cmd2="/usr/local/python3/bin/tensorboard --logdir=/root/modelshow/";
	//exeLinuxCmd(cmd, ip, uid, pwd);
	exeLinuxCmd(cmd1, ip, uid, pwd);
	// 用以保存命令(可能是一串很长的命令)
	/*StringBuffer cmdBuffer = new StringBuffer();
	//cmdBuffer.append("/usr/local/python3/bin/tensorboard --logdir=/root/modelshow/");
	cmdBuffer.append("mkdir /root/cdcd/ii.txt");
	ExeLinuxCmdUtil ssh = new ExeLinuxCmdUtil("192.168.0.157", "root", "123456");
	System.exit(ssh.exec(cmdBuffer.toString()) ? 0 : 1);*/
}

}

猜你喜欢

转载自blog.csdn.net/qq_43034494/article/details/90200256