码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-08-06 22:34:43      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:ganymed ssh-2 java执行   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 ~]$ 


版权声明:本文为博主原创文章,未经博主允许不得转载[http://blog.csdn.net/doctor_who2004]。

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

标签:ganymed ssh-2 java执行   ganymed ssh-2   java执行远程linux机器命令工具   

原文地址:http://blog.csdn.net/doctor_who2004/article/details/47322105

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!