SSH-keygen运行不正常(java)

时间:2014-01-13 07:39:52

标签: java shell ssh

我想在本地计算机上执行ssh-keygen。但是当我运行下面的代码时,ssh-keygen只运行到:

Generating public/private rsa key pair.

Enter file in which to save the key (/cygdrive/c/Users/USER/.ssh/id_rsa):

BUILD SUCCESSFUL (total time: 0 seconds) 
//ssh-keygen execution stop at here only.

我想要的结果是这样的:

生成公钥/私钥rsa密钥对。

输入要保存密钥的文件(/cygdrive/c/Users/USER/.ssh/id_rsa)://按enter键

输入密码(无密码短语为空)://按enter键

再次输入相同的密码://按enter键

//http://commons.apache.org/proper/commons-exec/tutorial.html

import java.io.IOException;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteWatchdog;

public class ApacheRunSSHKEygen {

public static void main(String[] args) throws IOException {

    try {


    String line = "C:\\ExecuteSSH\\ssh-keygen.exe";   // path to ssh-keygen.exe
    CommandLine cmdLine = CommandLine.parse(line);
    DefaultExecutor executor = new DefaultExecutor();

    //watchdog
    executor.setExitValue(1);
    ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
    executor.setWatchdog(watchdog);

    int exitValue = executor.execute(cmdLine);    //execute ssh-keygen
    }
    catch (Exception exc){     
       System.out.println("error" + exc);/*handle exception*/} 
    }     
    }

我的代码实际上有什么问题。我怎样才能正确运行ssh-keygen?提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您需要将-f选项传递给ssh-keygen,以使其跳过此互动问题。