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

您没有登录

» Java开发网 » Java Security  

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

KK

CJSDN高级会员


发贴: 3666
积分: 93
于 2006-04-26 04:35 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
譬如:
ssh user@host \ls

必须输入密码才能继续

如果不在sshd上设置key的话,怎么才能让ssh不问密码呢?

如果这条路不通的话,怎么写1个可以自动写密码的bash呢?


jigsaw edited on 2006-04-26 04:38

No one knows except both of us.
909090909090909090909090909090909090909090b8533ce76c8d6c241868968a0408c338b4ffbf
ISO/IEC 9899:1999
作者 Re:怎么让SSH不prompt password? [Re:jigsaw]
davidself

猫哥

CJSDN高级会员


发贴: 1506
积分: 333
于 2006-04-26 09:14 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
这种情况我只知道用密钥交换,用shell写密码难道会安全吗?


--108的上铺--
davidself@twitter
作者 Re:怎么让SSH不prompt password? [Re:jigsaw]
jigsaw

KK

CJSDN高级会员


发贴: 3666
积分: 93
于 2006-04-26 13:43 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
我不怕安全问题 我只要方便。。。如果能在shell里搞定是最好了
如果不行,那只有搞密钥去了



No one knows except both of us.
909090909090909090909090909090909090909090b8533ce76c8d6c241868968a0408c338b4ffbf
ISO/IEC 9899:1999
作者 Re:怎么让SSH不prompt password? [Re:jigsaw]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2006-04-26 20:54 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
use keys.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:怎么让SSH不prompt password? [Re:jigsaw]
jigsaw

KK

CJSDN高级会员


发贴: 3666
积分: 93
于 2006-04-26 23:03 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
搞定了 写程序搞定的 =) 用了第三方的包(免费)


No one knows except both of us.
909090909090909090909090909090909090909090b8533ce76c8d6c241868968a0408c338b4ffbf
ISO/IEC 9899:1999
作者 Re:怎么让SSH不prompt password? [Re:jigsaw]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2006-04-27 00:53 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
jsch?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:怎么让SSH不prompt password? [Re:jigsaw]
jigsaw

KK

CJSDN高级会员


发贴: 3666
积分: 93
于 2006-04-27 02:01 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


http://www.ganymed.ethz.ch/ssh2/

感觉比jsch的API友好 所以就用了这个



No one knows except both of us.
909090909090909090909090909090909090909090b8533ce76c8d6c241868968a0408c338b4ffbf
ISO/IEC 9899:1999
作者 Re:怎么让SSH不prompt password? [Re:jigsaw]
menzy



版主


发贴: 754
积分: 113
于 2006-04-30 08:32 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
研究一下, 楼上整一个BP案例出来吧


作者 Re:怎么让SSH不prompt password? [Re:jigsaw]
jigsaw

KK

CJSDN高级会员


发贴: 3666
积分: 93
于 2006-05-02 03:30 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
wot is BP case?


No one knows except both of us.
909090909090909090909090909090909090909090b8533ce76c8d6c241868968a0408c338b4ffbf
ISO/IEC 9899:1999
作者 Re:怎么让SSH不prompt password? [Re:jigsaw]
menzy



版主


发贴: 754
积分: 113
于 2006-05-02 12:43 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
best practices


作者 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

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