简体   繁体   English

如何从 java 程序中的命令行参数获取 ssh 关键数据?

[英]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.我正在尝试从命令行获取 ssh 私钥数据。

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

In my Git class, I use the key data as below:在我的 Git class 中,我使用如下关键数据:

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.但这会因无效的 ssh 密钥错误消息而失败。

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 ).而且我还检查了System.out.println结果与我给出的输入完全相同(包含\n )。

I guess that bash does something with backslashes, so that in my Java program, the \n is not understood properly.我猜想 bash 用反斜杠做了一些事情,所以在我的 Java 程序中, \n没有被正确理解。

How can I debug and resolve this?我该如何调试和解决这个问题?

If you can change the input string, just use enter instead of '\n' sequences.如果您可以更改输入字符串,只需使用 enter 而不是 '\n' 序列。

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

Then the java argument will contain new lines instead of the string "\n"然后 java 参数将包含新行而不是字符串“\n”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Java中将此程序作为命令行参数运行时出现错误,如何解决此问题? - Getting an error running this program as a command line argument in Java, how can I fix this? 如何将命令行参数传递给 Maven 程序的执行? - How can I pass a command line argument to the execution of a Maven program? 如何使用Java执行SSH命令行? - How can I execute SSH command line using Java? 如果子进程是Java程序(带有命令行参数),如何从subprocess.check_output获取输出? - How can I get output from subprocess.check_output if the subprocess is a java program(with Command-line arguments)? Java命令行参数程序 - Java Command Line Argument Program 我如何在java中将char作为命令行参数 - how can i take in a char as an command line argument in java 如何使Java从命令行参数输出唯一整数? - How do I get Java to output unique integers from a command-line argument? 您可以将 ArrayList 作为命令行参数传递给 Java 程序吗 - Can you pass an ArrayList as a command line argument for a java program 如何从Java程序在Linux中打开另一个命令行应用程序? - How can I open another command line application in Linux from a Java program? 如何修改此Java程序以从命令行中指定的文件中读取名称? - How can I modify this Java program to read in the names from a file designated in the command line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM