Ganymed SSH-2 java执行远程linux机器命令工具

Ganymed SSH2 for Java is a library which implements the SSH-2 protocol in pure Java(tested on J2SE 1.4.2 and 5.0). It allows one to connect to SSH servers from withinJava programs. It supports SSH sessions (remote command execution and shell access),local and remote port forwarding, local stream forwarding, X11 forwarding, SCP and SFTP.There are no dependencies on any JCE provider, as all crypto functionality is included.

Ganymed SSH2 for Java was first developed for the Ganymed replication project and acouple of other projects at the IKS group at ETH Zurich.

Website: http://www.ganymed.ethz.ch/ssh2.

Please read the included LICENCE.txt, latest changes can be found in HISTORY.txt.


在linux开发环境下,我们一般都是用一些终端 工具连接远程linux服务器,执行bash脚本或系统命令,其实这些工具的核心是实现SSH2协议,通过SSH 会话,我们传递命令,显示命令输出结果。

在java环境下,授权比较宽松的是Ganymed SSH-2.


在本地机器模拟一下远程登陆,执行系统命令:


package com.doctor.ganymed_ssh2;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;

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

/**
 * 
 * @author doctor
 *
 * @time 2015年8月5日  
 *
 * @see https://github.com/northern-bites/ganymed-ssh2/blob/master/examples/Basic.java
 *
 */
public class Basic {

	/**
	 * 如果出现:Password authentication fails, I get "Authentication method password
	 * not supported by the server at this stage
	 * 
	 * 请查看:ssh配置文件: /ect/ssh/sshd_config , 找到配置: "PasswordAuthentication" 被设为 "no" .
	 * changed it to "PasswordAuthentication yes"
	 * 
	 * 
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		String hostname = "127.0.0.1";
		String username = "doctor";
		String passwd = "****";
		Connection connection = new Connection(hostname);
		connection.connect();

		boolean authenticateWithPassword = connection.authenticateWithPassword(username, passwd);
		if (!authenticateWithPassword) {
			throw new RuntimeException("authenticateWithPassword failed");
		}

		Session session = connection.openSession();
		session.execCommand("pwd  ; date ;echo hello doctor");
		InputStream streamGobbler = new StreamGobbler(session.getStdout());

		String result = IOUtils.toString(streamGobbler, StandardCharsets.UTF_8);
		System.out.println("result:" + result);
		System.out.println("ExitStatus:" + session.getExitStatus());

		session.close();
		connection.close();

	}

}

在执行上面的程序时候,你还得确认下当前系统的ssh服务可用否。

可用ssh 127.0.0.1 命令确认。

上面程序执行结果:


result:/home/doctor
2015年 08月 06日 星期四 20:12:47 CST
hello doctor

ExitStatus:0




现在一些工具也都集成这些服务,比较知名的jenkins插件ansible就实现了ssh2协议。

附:

检查ssh服务状态:

[doctor@localhost ~]$ service sshd status
Redirecting to /bin/systemctl status  sshd.service
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: <em><strong><span style="color:#FF6666;">active (running)</span> </strong></em>since 四 2015-08-06 18:58:15 CST; 1h 19min ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 975 (sshd)
   CGroup: /system.slice/sshd.service
           └─975 /usr/sbin/sshd -D

Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.


[doctor@localhost ~]$ ssh localhost
doctor@localhost's password: 
Last failed login: Wed Aug  5 20:02:46 CST 2015 from 127.0.0.1 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Sun Jun  7 21:43:45 2015
[doctor@localhost ~]$ 


你可能感兴趣的:(Ganymed,Ganymed,SSH-2,SSH-2,java执行)