Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java Security  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
话题被移动
该话题已被移动 - floater , 2006-04-27 21:35
如果您尚不清楚该话题被移动的原因,请参考论坛规则以及本版公告或者联系本版版主。
作者 Re:怎么让SSH不prompt password? [Re:jigsaw]
jigsaw

KK

CJSDN高级会员


发贴: 3666
积分: 93
于 2006-05-02 19:41 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
i do it in an ad-hoc way..


package com.corp.prj.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.log4j.Logger;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

/**
* Provides static methods for running SSH, scp as well as local commands.
*
*/
public class CommandRunner {
  private static final Logger logger = Logger.getLogger(CommandRunner.class);

  private CommandRunner() {
  }
  
  /**
   * Get remote file through scp
   * @param host
   * @param username
   * @param password
   * @param remoteFile
   * @param localDir
   * @throws IOException
   */
  public static void scpGet(String host, String username, String password,
      String remoteFile, String localDir) throws IOException {
    if (logger.isDebugEnabled()) {
      logger.debug("spc [" + remoteFile + "] from " + host + " to " + localDir);
    }
    Connection conn = getOpenedConnection(host, username, password);
    SCPClient client = new SCPClient(conn);
    client.get(remoteFile, localDir);
    conn.close();
  }
  
  /**
   * Put local file to remote machine.
   * @param host
   * @param username
   * @param password
   * @param localFile
   * @param remoteDir
   * @throws IOException
   */
  public static void scpPut(String host, String username, String password,
      String localFile, String remoteDir) throws IOException {
    if (logger.isDebugEnabled()) {
      logger.debug("spc [" + localFile + "] to " + host + remoteDir);
    }
    Connection conn = getOpenedConnection(host, username, password);
    SCPClient client = new SCPClient(conn);
    client.put(localFile, remoteDir);
    conn.close();
  }
  
  /**
   * Run SSH command.
   * @param host
   * @param username
   * @param password
   * @param cmd
   * @return exit status
   * @throws IOException
   */
  public static int runSSH(String host, String username, String password,
      String cmd) throws IOException {
    if (logger.isDebugEnabled()) {
      logger.debug("running SSH cmd [" + cmd + "]");
    }

    Connection conn = getOpenedConnection(host, username, password);
    Session sess = conn.openSession();
    sess.execCommand(cmd);

    InputStream stdout = new StreamGobbler(sess.getStdout());
    BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

    while (true) {
      // attention: do not comment this block, or you will hit NullPointerException
      // when you are trying to read exit status
      String line = br.readLine();
      if (line == null)
        break;
      if (logger.isDebugEnabled()) {
        logger.debug(line);
      }
    }

    sess.close();
    conn.close();
    return sess.getExitStatus().intValue();
  }
  
  /**
   * return a opened Connection
   * @param host
   * @param username
   * @param password
   * @return
   * @throws IOException
   */
  private static Connection getOpenedConnection(String host, String username,
      String password) throws IOException {
    if (logger.isDebugEnabled()) {
      logger.debug("connecting to " + host + " with user " + username
          + " and pwd " + password);
    }

    Connection conn = new Connection(host);
    conn.connect(); // make sure the connection is opened
    boolean isAuthenticated = conn.authenticateWithPassword(username,
        password);
    if (isAuthenticated == false)
      throw new IOException("Authentication failed.");
    return conn;
  }
  
  /**
   * Run local command
   * @param cmd
   * @return exit status
   * @throws IOException
   */
  public static int runLocal(String cmd) throws IOException {
    if (logger.isDebugEnabled()) {
      logger.debug("running local cmd [" + cmd + "]");
    }

    Runtime rt = Runtime.getRuntime();
    Process p = rt.exec(cmd);

    InputStream stdout = new StreamGobbler(p.getInputStream());
    BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

    while (true) {
      String line = br.readLine();
      if (line == null)
        break;
      if (logger.isDebugEnabled()) {
        logger.debug(line);
      }

    }
    return p.exitValue();
  }
}




No one knows except both of us.
909090909090909090909090909090909090909090b8533ce76c8d6c241868968a0408c338b4ffbf
ISO/IEC 9899:1999

话题树型展开
人气 标题 作者 字数 发贴时间
23026 怎么让SSH不prompt password? jigsaw 105 2006-04-26 04:35
20287 Re:怎么让SSH不prompt password? davidself 30 2006-04-26 09:14
22364 Re:怎么让SSH不prompt password? jigsaw 4156 2006-05-02 19:41
20241 Re:怎么让SSH不prompt password? jigsaw 47 2006-04-26 13:43
20438 Re:怎么让SSH不prompt password? floater 9 2006-04-26 20:54
20183 Re:怎么让SSH不prompt password? jigsaw 25 2006-04-26 23:03
20378 Re:怎么让SSH不prompt password? floater 5 2006-04-27 00:53
20109 Re:怎么让SSH不prompt password? jigsaw 65 2006-04-27 02:01
20127 Re:怎么让SSH不prompt password? menzy 18 2006-04-30 08:32
20032 Re:怎么让SSH不prompt password? jigsaw 15 2006-05-02 03:30
20086 Re:怎么让SSH不prompt password? menzy 14 2006-05-02 12:43

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923