首页 > 代码库 > 使用ganymed工具调用ssh2

使用ganymed工具调用ssh2

需要引入ganymed-ssh2-build210.jar包。

其实很简单。所以直接贴代码,代码说话。

package com.eshore.framework.util;



import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import ch.ethz.ssh2.log.Logger;
/**
 * shell脚本调用类
 * @author clear
 *
 */
public class SshBasic{
    
    //连接,登陆
    public Connection login(String hostname,int port,String username,String password){

        //获取连接
        Connection conn = new Connection(hostname, port);
        try {
            //连接
            conn.connect();
            //输入账号密码登陆
            boolean isAuthenticated = conn.authenticateWithPassword(username, password);
            //登陆失败,返回错误
            if(isAuthenticated == false){
                throw new IOException("isAuthentication failed.");
            }
        } catch (IOException e) {
            
            e.printStackTrace();
        }
        
        return conn;
    }
    //获取Session
    public Session getSession(Connection conn){
        Session sess = null;
        try {
            sess = conn.openSession();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return sess;
    }
    //获取控制台打印信息
    public String printCmd(String path,Connection conn,Session sess, String date, String city){
        String txt = "";
        try {
            sess.execCommand("chmod 755 "+path+" && "+path+" "+date+" "+city);
            //打印信息
            InputStream stdout = new StreamGobbler(sess.getStdout());
            //打印错误
            InputStream stderr = new StreamGobbler(sess.getStderr());
            BufferedReader brout = new BufferedReader(new InputStreamReader(stdout,"UTF-8"));
            BufferedReader brerr = new BufferedReader(new InputStreamReader(stderr,"UTF-8"));
            while(true){
                String line = brout.readLine();
                if(line==null){
                    break;
                }
                txt += line+"<br/>";
                System.out.println(line);
            }
            while(true){
                String line = brerr.readLine();
                if(line==null){
                    break;
                }
                txt += line+"<br/>";
                System.out.println(line);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return txt;
    }

    public static void main(String[] args) {
        
        SshBasic m = new SshBasic();
        //连接并登陆
        Connection conn = m.login("132.122.1.51", 22, "srglweb", "srglweb123");
        //获取Session
        Session sess = m.getSession(conn);
        //获取控制台信息
        String cmd = m.printCmd("shelltest/two.sh", conn,sess,"20140905","200");
        System.out.println("cmd:"+cmd);
        System.out.println("--->"+sess);
        //判断会话是否成功
        int result = sess.getExitStatus();//如果成功返回0
        System.out.println("result:"+result);
        sess.close();
        conn.close();
    }

}

 

使用ganymed工具调用ssh2