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"),否则有可能出现乱码

  • 相关阅读:
    20155327 2017-2018-2 《Java程序设计》第9周学习总结
    20155327《Java程序设计》第八周学习总结
    实验二 Java面向对象程序设计
    20155327 李百乾 Exp4 恶意代码分析
    20155327结对编程练习
    20155327第七周学习总结
    2017-2018-1 20155310 20155337 实验五 通讯协议设计
    # 2017-2018-1 20155337《信息安全系统设计基础》第十三周学习总结
    # 20155337 2017-2018 1 课上测试、课下作业、实验
    # 课下测试ch02
  • 原文地址:https://www.cnblogs.com/yhzh/p/5056260.html
Copyright © 2011-2022 走看看