0
votes

I am able to run ssh-keygen with code below, but i still unclear on how to execute command inside SSH-keygen.

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 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {

    try {

//        String line = "AcroRd32.exe /p /h " + file.getAbsolutePath();
    String line = "C:\\ExecuteSSH\\ssh-keygen.exe -N";
    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);
    }

    catch (Exception exc){

       System.out.println("error" + exc);/*handle exception*/} 
    }


}

the output is :

ssh-keygen: option requires an argument -- N

usage: ssh-keygen [options]

Options:

-a trials Number of trials for screening DH-GEX moduli.

-B Show bubblebabble digest of key file.

-b bits Number of bits in the key to create.

....... .......

-y Read private key file and print public key.

if the code and command is right, the output after that should prompted for passphrase. like below

Generating public/private rsa key pair.

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

Enter passphrase:

1
guys, at least can i have the link direction document to how to run cmd command line. thanks :)Learner
The output is telling what you did wrong: The -N option must be followed by an argument. If you want the command to prompt for a passphrase, don't include -N at all.VGR
thanks VGR, but why when i run the ssh-keygen.exe, it freeze after Generating public/private rsa key pair. Enter file in which to save the key (/cygdrive/c/Users/USER/.ssh/id_rsa): any idea?Learner

1 Answers

0
votes

The shell is complaining that you didn't pass a passphrase with the "-N" argument. You can add a passphrase and file to your command to make it work correctly, something like this

String line = "C:\\ExecuteSSH\\ssh-keygen.exe -N \"my_passphrase_is_poopydiapers\" -f /tmp/mykey";

You could also leave the "-N" off and just exec ssh-keygen.exe with no arguments to get the output you said you expected in your post. However, then you'd have to feed the passphrase and file in to DefaultExecutor through STDIN, which would be much more difficult than specifying that info on the commandline.