標籤: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服務狀態:
[[email protected] ~]$ service sshd statusRedirecting 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 -DWarning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.
[[email protected] ~]$ ssh localhost[email protected]'s password: Last failed login: Wed Aug 5 20:02:46 CST 2015 from 127.0.0.1 on ssh:nottyThere was 1 failed login attempt since the last successful login.Last login: Sun Jun 7 21:43:45 2015[[email protected] ~]$
著作權聲明:本文為博主原創文章,未經博主允許不得轉載[http://blog.csdn.net/doctor_who2004]。
Ganymed SSH-2 java執行遠程linux機器命令工具