0

I want to execute ssh-keygen in my local computer. but when i run the code below, ssh-keygen only run until :

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.

Result i want is like this:

Generating public/private rsa key pair.

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

Enter passphrase (empty for no passphrase): //press enter

Enter same passphrase again: //press 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*/} 
    }     
    }

What is actually wrong in my code. how can i run the ssh-keygen properly? thank you in advance for the helps.

Learner
  • 101
  • 3
  • 15
  • Why not try doing this in pure Java? See [this question](http://stackoverflow.com/questions/3706177/how-to-generate-ssh-compatible-id-rsa-pub-from-java) for guidance. – mpontillo Jan 13 '14 at 07:43
  • thank you mike, but i still need to figure this out. what is wrong actually. i suspect the problem come from here String line = "C:\\ExecuteSSH\\ssh-keygen.exe"; // path to s ssh-keygen.exe CommandLine cmdLine = CommandLine.parse(line); DefaultExecutor executor = new DefaultExecutor(); maybe the code only allow few output line to be executed. – Learner Jan 13 '14 at 08:06
  • 1
    I suggested the pure Java approach because shelling out to run other programs has a lot of disadvantages. For one thing, you're writing non-portable code. Your code will only work on Windows machines running Cygwin with your `ssh-keygen.exe` in `C:\ExecuteSSH`. Also (just theorizing here) it's possible that `ssh-keygen.exe` is trying to read directly from the terminal to get the passphrase, bypassing the standard input/output streams, and so it stops at the point where you have to enter the password. I don't see the part in your code where you press `` on behalf of the user, though. – mpontillo Jan 13 '14 at 16:45

1 Answers1

0

You need to pass the -f option to ssh-keygen to make it skip this interactive question.

mirabilos
  • 5,123
  • 2
  • 46
  • 72