简体   繁体   中英

How can I get an ssh key data from a command line argument in java program?

I'm trying to get an ssh private key data from command line.

./myProgram --prvKey '-----BEGIN RSA PRIVATE KEY-----\nxxx\n-----END RSA PRIVATE KEY-----\n'

In my Git class, I use the key data as below:

protected JSch createDefaultJSch(FS fs) throws JSchException {
    String prvKeyData = Main.getPrvKeyData();
    System.out.println(prvKeyData);
    JSch jSch = super.createDefaultJSch(fs);
    jSch.addIdentity("monitoring-configs", prvKeyData.getBytes(), null, "passphrase".getBytes());
    return jSch;
      }

But this fails with an invalid ssh key error message.

I checked that it works well when putting that string into the code directly.

And I also checked that the System.out.println result is exactly the same as the input I gave(containing \n ).

I guess that bash does something with backslashes, so that in my Java program, the \n is not understood properly.

How can I debug and resolve this?

If you can change the input string, just use enter instead of '\n' sequences.

/myProgram --prvKey '-----BEGIN RSA PRIVATE KEY-----
xxx
-----END RSA PRIVATE KEY-----
'

Then the java argument will contain new lines instead of the string "\n"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM