dongli8466 2017-11-24 01:10
浏览 208
已采纳

如何避免golang的“密码短语太短”

What I am doing is to create a ssh key from a golang program, using

exec.Command("ssh-keygen", "-f", "id_rsa", "-t", "rsa", "-P", "\"\"")

but the output fails

exit status 1: Saving key "id_rsa" failed: passphrase is too short (minimum five characters)

If I execute the command in the terminal it works perfect.

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]-----+

The complete code is:

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())
}
  • 写回答

1条回答 默认 最新

  • douyuan5600 2017-11-24 02:35
    关注

    You don't need to specify an empty -P attribute, just simply use:

    exec.Command("ssh-keygen", "-f", "id_rsa", "-t", "rsa")
    

    If for any reason you need to specify empty -P

    exec.Command("ssh-keygen", "-f", "id_rsa", "-t", "rsa", "-P", "")
    

    Also before creating the id_rsa file make sure it doesn't exist in the directory you are creating it, otherwise you will get exit error 1:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器