我正在做的是使用
从golang程序创建一个ssh密钥 exec.Command("ssh-keygen", "-f", "id_rsa", "-t", "rsa", "-P", "\"\"")
但输出失败
exit status 1: Saving key "id_rsa" failed: passphrase is too short (minimum five characters)
如果我在终端中执行命令,那就完美了。
Generating public/private rsa key pair.
Your identification has been saved in id_rsa.
Your public key has been saved in id_rsa.pub.
The key fingerprint is:
SHA256:HYixKOtEB42jAr+XNa9uQMZnvmLHa6Sirq+ietWaxzI admin@MacBook-Pro.local
The key's randomart image is:
+---[RSA 2048]----+
| .o . |
|. o... + . |
|.oooo o . . |
|o..++ = . . |
|. o+ B oS . |
| o. = + . |
| .o O o |
|. o E X |
|%*.o Xo. |
+----[SHA256]-----+
完整的代码是:
package main
import (
"bytes"
"fmt"
"os/exec"
)
func main() {
cmd := exec.Command("ssh-keygen", "-f", "id_rsa", "-t", "rsa", "-P", "\"\"")
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
return
}
fmt.Println("Result: " + out.String())
}
答案 0 :(得分:2)
您无需指定空的-P属性,只需使用:
exec.Command("ssh-keygen", "-f", "id_rsa", "-t", "rsa")
如果出于任何原因需要指定空-P
exec.Command("ssh-keygen", "-f", "id_rsa", "-t", "rsa", "-P", "")
在创建id_rsa文件之前,请确保它在您创建它的目录中不存在,否则您将收到退出错误1: