zoukankan      html  css  js  c++  java
  • 使用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();
        }
    
    }
  • 相关阅读:
    SuperSocket 1.4系列文档(16) 在SuperSocket中启用传输层加密(TLS/SSL)
    SuperSocket 1.4系列文档(10) SuperSocket中的日志功能
    UIPageControl实现自定义按钮
    ios 某些代码网址,app打包成ipa
    笔记隐藏状态栏,播放音乐,获取文件路径,nsthread,文件文件夹操作,plist 时间
    使用NSTimer实现倒计时,Iphone幻灯片效果+背景音乐,
    如何让你的iPhone程序支持多语言环境(本地化)
    iPhone电子书toolbar的实现
    iphone界面如何实现下拉列表
    使用NSTimer与iphone的简单动画,实现飘雪效果
  • 原文地址:https://www.cnblogs.com/cxxjohnson/p/7163118.html
Copyright © 2011-2022 走看看