如何让Ant sshexec使用非空密码

时间:2013-06-28 09:05:34

标签: 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任务之前存储私钥。

1 个答案:

答案 0 :(得分:1)

如果你从命令行运行你的ant文件,你可能会要求你输入密码而不是硬编码。重要的部分是提示输入密码,将其存储在属性中并在passphrase-argument中引用此属性。

  <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-file(根据源eclipse不处理SecureInputHandler)

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

相关问题