SSH keygen error: 'passphrase too short: have 2 bytes, need >4'

SSH keygen error: 'passphrase too short: have 2 bytes, need >4'

我正在尝试自动生成 SSH RSA key.When 下面的命令是 运行 在命令行上它可以正确生成密钥:

ssh-keygen -f /root/.ssh/id_rsa -t rsa -N ''

当我尝试在 python 2 脚本中复制它时出现问题。

 if not os.path.isfile('/root/.ssh/id_rsa.pub'):
    fullcmd = ['ssh-keygen', '-f', '/root/.ssh/id_rsa', '-t', 'rsa', '-N', '\'\'']
    sshcmd = subprocess.Popen(fullcmd,
                shell= False,
                stdout= subprocess.PIPE,
                stderr= subprocess.STDOUT)
    out = sshcmd.communicate()[0].split('\n')
    for lin in out:
        print lin

当我 运行 这个脚本时,我得到这个错误:

> passphrase too short: have 2 bytes, need >4
> Generating public/private
> rsa key pair. Saving the key failed: /root/.ssh/id_rsa.

为什么当我在命令行而不是通过 python 脚本执行它时它会起作用?

fullcmd = ['ssh-keygen', '-f', '/root/.ssh/id_rsa', '-t', 'rsa', '-N', '\'\'']

'' 是用于传递空字符串的 shell 语法。您是直接调用命令,而不是通过 shell,所以不要传递单引号。只传递空字符串本身。

fullcmd = ['ssh-keygen', '-f', '/root/.ssh/id_rsa', '-t', 'rsa', '-N', '']