java连接linux

1.在java代码中连接linux服务器

 1.1依赖的添加

		<dependency>
			<groupId>commons-net</groupId>
			<artifactId>commons-net</artifactId>
			<version>2.2</version>
		</dependency>

		<dependency>
			<groupId>com.jcraft</groupId>
			<artifactId>jsch</artifactId>
			<version>0.1.54</version>
		</dependency>
		
		<dependency>
		  <groupId>ch.ethz.ganymed</groupId>
		  <artifactId>ganymed-ssh2</artifactId>
		  <version>build210</version>
		</dependency>

1.2java代码示例

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

public class ExeShell {

	/**
	 * 执行函数
	 * @param commands 执行命令
	 * @param sshInfo 远程连接信息 包括远程地址用户名密码
	 * @return
	 */
	public static boolean exeCommand(String[] commands,String[] sshInfo){
		
		String sshPort="22";//ssh远程连接的默认端口是22
		String hostIp=sshInfo[0];
		String hostName=sshInfo[1];
		String hostPassword=sshInfo[2];
		Session sess = null;
		Connection conn = null;
		boolean result=false;
		try {
			conn = new Connection(hostIp,Integer.valueOf(sshPort));
			conn.connect();
			boolean isAuthenticated = conn.authenticateWithPassword(hostName, hostPassword);

			if (isAuthenticated == false){
				throw new IOException("Authentication failed. name= " + hostIp);
			}
			for (int i = 0; i < commands.length; i++) {
				sess = conn.openSession();
				sess.execCommand(commands[i]);
				result = getResult(sess);
				if (!result) {
					return false;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} finally {
			if (sess != null) {
				sess.close();
			}
			if (conn != null) {
				conn.close();
			}
		}
		return true;
	}

	/**
	 * 获取Linux执行以后的信息进行判断
	 * @param sess
	 * @return
	 * @throws IOException
	 */
	private static boolean getResult(Session sess) throws IOException{
		StringBuffer stringBuffer=new StringBuffer();
		BufferedReader stderrReader = null;
		BufferedReader stdoutReader = null;
		try {
			InputStream stdout = new StreamGobbler(sess.getStdout());
			InputStream stderr = new StreamGobbler(sess.getStderr());
			stderrReader = new BufferedReader(new InputStreamReader(stderr, "UTF-8"));
			stdoutReader = new BufferedReader(new InputStreamReader(stdout, "UTF-8"));
			char[] arr = new char[512];
			int read;
			StringBuilder outmsg = new StringBuilder();
			while (true) {
				read = stdoutReader.read(arr, 0, arr.length);
				if (read < 0) {
					break;
				}
				outmsg.append(new String(arr, 0, read));
			}
			System.out.println(outmsg);
			while (true) {
				read = stderrReader.read(arr, 0, arr.length);
				if (read < 0) {
					break;
				}
				stringBuffer.append(new String(arr, 0, read));
			}
			System.out.println(stringBuffer);
			if(stringBuffer.length()>0){
				return false;
			}else{
				return true;
			}
			
		} catch (IOException e) {
			throw new IOException(e);
		} finally {
			if (stderrReader != null) {
				stderrReader.close();
			}
			if (stdoutReader != null) {
				stdoutReader.close();
			}
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值