日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

SSH客户端开发开源组件Ganymed SSH-2 for Java初体验

發布時間:2025/4/16 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SSH客户端开发开源组件Ganymed SSH-2 for Java初体验 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、Ganymed SSH-2 for Java

Ganymed SSH-2 for Java是一個純Java實現的SHH2庫。

看官方介紹:

Ganymed SSH-2 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 within Java 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.


2、開源組件下載地址:http://www.ganymed.ethz.ch/ssh2/

? ? ? 解壓后如下:

? ? ?

ganymed-ssh2-build210.jar引入工程包,javadoc是學習文檔,examples是開發案例。


3、開發案例Basic代碼參考:

package com.ssh;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 Basic {public static void main(String[] args){String hostname = "172.0.0.1";String username = "hdfs";String password = "hdfs";try{/* Create a connection instance */Connection conn = new Connection(hostname);/* Now connect */conn.connect();/* Authenticate.* If you get an IOException saying something like* "Authentication method password not supported by the server at this stage."* then please check the FAQ.*/boolean isAuthenticated = conn.authenticateWithPassword(username, password);if (isAuthenticated == false)throw new IOException("Authentication failed.");/* Create a session */Session sess = conn.openSession();sess.execCommand("uname -a && date && uptime && who");System.out.println("Here is some information about the remote host:");/* * This basic example does not handle stderr, which is sometimes dangerous* (please read the FAQ).*/InputStream stdout = new StreamGobbler(sess.getStdout());BufferedReader br = new BufferedReader(new InputStreamReader(stdout));while (true){String line = br.readLine();if (line == null)break;System.out.println(line);}/* Show exit status, if available (otherwise "null") */System.out.println("ExitCode: " + sess.getExitStatus());/* Close this session */sess.close();/* Close the connection */conn.close();}catch (IOException e){e.printStackTrace(System.err);System.exit(2);}} }

總結

以上是生活随笔為你收集整理的SSH客户端开发开源组件Ganymed SSH-2 for Java初体验的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。