zoukankan      html  css  js  c++  java
  • ganymed-ssh2使用

    通过maven库获取ganymed-ssh2-262.jar,这是一个实现了ssh2协议的工具包,可以远程连接linux机器,执行命令,有些工作全靠它了

    示例代码如下:

    <!--首先要建立连接,传入ip(默认端口22),登录用户名和密码-->
    private
    static Connection getConnection(String hostname, String username, String password) throws Exception { Connection conn = null; try { conn = new Connection(hostname); conn.connect(); boolean isAuthenticated = conn.authenticateWithPassword(username, password); if (isAuthenticated == false) { throw new IOException("Authentication failed."); } } catch (Exception e) { throw new IOException("username or password error."); } return conn; }
    <!--执行一条命令,传入connect相关参数,命令和超时时间-->
    public
    static String execRemoteCommand(String hostname, String username, String password, String command, long timeout) throws Exception { Connection conn = getConnection(hostname, username, password); StringBuilder sb = new StringBuilder(); Session session = null; try { session = conn.openSession(); session.requestPTY("vt100", 80, 24, 640, 480, null); session.execCommand(command); InputStream stdout = new StreamGobbler(session.getStdout()); BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); long start = System.currentTimeMillis(); char[] arr = new char[512]; int read; int i = 0; while (true) { read = br.read(arr, 0, arr.length); if (read < 0 || (System.currentTimeMillis() - start) > timeout * 1000) { break; } sb.append(new String(arr, 0, read)); i++; } } finally { if (session != null) { session.close(); } if (conn != null) { conn.close(); } } return sb.toString(); }
    <!--执行多条命令,传入connect相关参数,命令和超时时间-->
    public
    static String execRemoteCommand(String hostname, String username, String password, String[] command, long timeout) throws Exception { Connection conn = getConnection(hostname, username, password); StringBuilder sb = new StringBuilder(); Session session = null; try { for (int t = 0; t < command.length; t++) { session = conn.openSession(); session.requestPTY("vt100", 80, 24, 640, 480, null); session.execCommand(command[t]); InputStream stdout = new StreamGobbler(session.getStdout()); BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); long start = System.currentTimeMillis(); char[] arr = new char[512]; int read; int i = 0; while (true) { read = br.read(arr, 0, arr.length); if (read < 0 || (System.currentTimeMillis() - start) > timeout * 1000) { break; } sb.append(new String(arr, 0, read)); i++; } session.close(); } } finally { if (conn != null) { conn.close(); } } return sb.toString(); }

    最近用这个工具包做了个远程下载的功能

    OutputStream out = response.getOutputStream();
    Connection conn = getConnection(sshcfg.getHost(), sshcfg.getUsername(), sshcfg.getPassword());
    SCPInputStream ins=null;
    try {
      SCPClient scpClient = conn.createSCPClient();
      ins = scpClient.get(fpath);
      //InputStream stdout = new StreamGobbler(ins);
      byte[] arr = new byte[512];
      int read;
      while (true) {
        read = ins.read(arr);
        if (read < 0) break;
        out.write(arr);
       }
       //ins.close();
    
    } finally {
      if(ins!=null)ins.close();
      if (conn != null) {
          conn.close();
      }
    }

     注意:new InputStreamReader(stdout)这个的使用的是默认的字符编码,如果执行”cat **.log“而文件中有中文,应当指定编码,比如:new InputStreamReader(stdout,"GBK"),否则有可能出现乱码

  • 相关阅读:
    黄聪:WordPress wp_head()优化:去除不必要的元素标签(转)
    黄聪:IE6下用控制图片最大显示尺寸
    黄聪:wordpress wp_head()函数 浏览器顶部 空白28px 解决办法(转)
    黄聪:在Photoshop中创建多种样式的网格背景图案(转)
    黄聪:如何WP中获取文章分类名称、分类ID、归档分类链接
    黄聪:Wordpress如何不显示(只显示)置顶文章
    黄聪:淘宝用户在宝贝详情页想看到什么
    黄聪:Windows7立体声混音设置方法(stereo mix)(转)
    黄聪:wordpress博客用Slimbox2实现lightbox效果(免插件)(转)
    黄聪:tor 解决 连接中继目录failed 没有可用的链路
  • 原文地址:https://www.cnblogs.com/yhzh/p/5056260.html
Copyright © 2011-2022 走看看