茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1672329 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
PIMG_00208.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

爪哇咖啡屋 : [轉貼]Ganymed ssh 小使用

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Ganymed ssh 小使用
Ganymed ssh小使用

java實現ssh協議ganymed實現SCP和SFTP
Ganymed SSH

目錄
1 SSH簡介 1
2 JAVA實現SSH的項目 1
3 GANYMED SSH 1
3.1 依賴說明 1
3.2 實現簡單命令 1
3.2 實現SCP命令

1 SSH簡介
SSH協議的簡介可見:http://xmong.iteye.com/blog/1698124

2 Java實現SSH的項目
Java 實現SSH協議的項目有很多,如JFTP,trilead SSH,JSCH,ganymed SSH等
下面我們主要說的是關於ganymed SSH的一些小使用。
Ganymed SSH-2 for Java是用純Java實現SSH-2協議的一個項目。可以通過它直接在Java程序中連接SSH服務器,實現基於SSH協議的服務訪問。 如遠程命令執行和shell訪問,本地和遠程端口轉發,本地數據流轉發,X11轉發,SCP,SFTP等功能。

3 Ganymed SSH
3.1 依賴說明
Ganymed SSH地址:http://www.ganymed.ethz.ch/ssh2/,下載ganymed-ssh2-build210.zip包,解壓後可見src(源代碼),ganymed-ssh2-build210.jar (jar包),javadoc(常用api文檔),examples(簡單使用案例)。可以通過簡單使用案例學習ganymed ssh的使用。

使用方法:將 ganymed-ssh2-build210.jar 加入到java項目的lib中即可使用。

3.2 實現簡單命令
    package com.example;

    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)
        {
                    //服務器ip
            String hostname = "172.30.5.162";
                    //用戶名
            String username = "xmong";
                    //密碼
            String password = "xmong";

            try
            {
                /* Create a connection instance */
                //構造一個連接器,傳入一個需要登陸的ip地址,連接服務
                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,執行linux命令
                Session sess = conn.openSession();


                sess.execCommand("pwd");

                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") */
                //得到腳本運行成功與否的標誌 :0-成功 非0-失敗
                System.out.println("ExitCode: " + sess.getExitStatus());

                /* Close this session */
                //關閉session和connection
                sess.close();

                /* Close the connection */

                conn.close();

            }
            catch (IOException e)
            {
                e.printStackTrace(System.err);
                System.exit(2);
            }
        }
    }


輸出結果:
    Here is some information about the remote host:
    /home/xmong
    ExitCode: null


3.2 實現SCP命令
    package com.test;

    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.SCPClient;
    import ch.ethz.ssh2.Session;
    import ch.ethz.ssh2.StreamGobbler;

    public class TestScp {


        public static void main(String[] args)
        {
            String hostname = "172.30.5.162";
            String username = "xmong";
            String password = "xmong";

            try
            {
                /* Create a connection instance */
                //構造一個連接器,傳入一個需要登陸的ip地址,連接服務
                Connection conn = new Connection(hostname);
                conn.connect();

                //用戶驗證,傳入用戶名和密碼
                boolean isAuthenticated = conn.authenticateWithPassword(username, password);

                if (isAuthenticated == false)
                    throw new IOException("Authentication failed.");

                //創建一個copy文件客戶端
                SCPClient scpClient = conn.createSCPClient();
                scpClient.put("D:/test.txt ", "./");//從本地複製文件到遠程目錄
                scpClient.get("./test.txt", "E:/");//從遠程獲取文件

                /**
                 * 通過SFTP遠程讀取文件內容
                 * test.txt文件內容為sftp---test
                 */
                SFTPv3Client sftpClient = new SFTPv3Client(conn);
                SFTPv3FileHandle sftpHandle = sftpClient.openFileRW("./test.txt");
                byte[] bs = new byte[11];
                int i = 0;
                long offset = 0;
                while(i!=-1){
                    i = sftpClient.read(sftpHandle, offset, bs, 0, bs.length);
                    offset += i;
                }
                System.out.println(new String(bs));

                //打開一個會話session,執行linux命令
                Session sess = conn.openSession();
                sess.execCommand("ls");
                System.out.println("Here is some information about the remote host:");

                //接收目標服務器上的控制台返回結果,輸出結果。
                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);
                }

                //得到腳本運行成功與否的標誌 :0-成功 非0-失敗
                System.out.println("ExitCode: " + sess.getExitStatus());

                //關閉session和connection
                sess.close();
                conn.close();

            }
            catch (IOException e)
            {
                e.printStackTrace(System.err);
                System.exit(2);
            }
        }

    }


輸出結果:
    sftp---test
    Here is some information about the remote host:
    test.txt
    ExitCode: 0


原文出處:Ganymed ssh小使用 - Java - ITeye博客
前一個主題 | 下一個主題 | | | |

討論串




Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|