java - 如何让 Ant sshexec 使用非空密码

标签 java ant ssh

如何使用配置有密码短语的私钥来使用 Ant 的 sshexec 任务?

我找到了以下解决方法:

  • 从私钥中删除密码(最实用)
  • 将密码作为命令行选项传递,例如 -Dpass=mypass(不安全)
  • 在 Ant 作业定义文件中对密码进行硬编码(最差)

这些对我来说都不是一个好的选择,但第一个是可以接受的。

pageant 中加载私钥似乎根本没有帮助。从其他答案看来,Ant 似乎正在尝试访问它,但它不能,退回到密码身份验证,失败并显示:

com.jcraft.jsch.JSchException: Auth cancel
    at com.jcraft.jsch.Session.connect(Session.java:490)
    at com.jcraft.jsch.Session.connect(Session.java:162)
... (rest of the stack trace omitted)

另一个常见症状是当未安装 Unlimited Strength JCE 时,错误类似于:

com.jcraft.jsch.JSchException: The cipher 'aes256-cbc' is required, but it is not available.
    at com.jcraft.jsch.KeyPair.loadPPK(KeyPair.java:942)
    at com.jcraft.jsch.KeyPair.load(KeyPair.java:515)
... (rest of the stack trace omitted)

回到我的问题,如何使其正常工作?使用带有密码的私钥,无需在任何地方对其进行硬编码或在命令行中输入?最好使用 pageant,我可以在运行 Ant 任务之前输入密码并存储私钥。

最佳答案

如果您从命令行运行 ant 文件,它可能会要求您输入密码,而不是对其进行硬编码。重要的部分是提示输入密码,将其存储在属性中并在密码参数中引用该属性。

  <target name="copy">    
    <input message="Enter passphrase for keyfile:" addproperty="the.password">
        <handler classname="org.apache.tools.ant.input.SecureInputHandler" />
    </input>
    <upload-files host="the.host" />
  </target>

  <macrodef name="upload-files">
    <attribute name="host" />
    <sequential>
      <scp file="${the.file}"
           todir="@{host}:directoryname/${filename}"
           keyfile="${key.file}" passphrase="${the.password}" verbose="true"/>      
    </sequential>
  </macrodef>

要求:

  • Ant 1.7.1
  • Java 6,如果在 Java 5 上运行,它将回退到“正常”
  • 从命令行运行 ant 文件(根据源 eclipse 不处理 SecureInputHandler)

来源:http://www.dcepler.net/post.cfm/hiding-password-input-in-ant

关于java - 如何让 Ant sshexec 使用非空密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17361072/

相关文章:

java - 违反 EJB 规范

JavaFX:向嵌套 CheckBoxTreeItem[s] 的复选框添加 CTRL 单击功能

java - ant中使用AndroidManifest.xml版本代码

java - &lt;input message ant 显示输入的内容

java - 带有分页的 Spring-Data FETCH JOIN 不起作用

java - jdbc4.CommunicationsException - 神秘超时

grails - Grails应用程序:JS文件混淆

git - Git SSH私钥密码短语

linux - 无法重新连接到 NX session ,错误为 : "error in locking authority file"

linux - Ansible wait_for 重启