Bawn
Bawn

Reputation: 509

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

I am trying to automate the process of generating an SSH RSA key.When the command below is run on the command line it generates the key correctly:

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

The issue arises when I try to replicate this in a python 2 script.

 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

When I run this script I get this error:

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

Why dose it work when I execute it on the command line and not through the python script?

Upvotes: 1

Views: 2845

Answers (1)

John Kugelman
John Kugelman

Reputation: 361555

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

'' is shell syntax for passing an empty string. You're invoking the command directly, not via a shell, so don't pass single quotes. Just pass the empty string itself.

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

Upvotes: 7

Related Questions