使用ganymed工具调用ssh2

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用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的主要内容,如果未能解决你的问题,请参考以下文章

Java 操作SSH2实现远程执行linux命令

Java 操作SSH2实现远程执行linux命令

Ganymed SSH-2 for Java学习笔记

Ganymed实现基本的自动化部署API

Java SSH远程执行Shell命令shell脚本实现(Ganymed SSH)

Java利用 ganymed-ssh2-build.jar来上传文件到linux以及下载linux文件以及执行linux shell命令