我正在尝试自动生成SSH RSA密钥的过程。当在命令行上运行以下命令时,它会正确生成密钥:
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脚本时它会起作用?
答案 0 :(得分:7)
fullcmd = ['ssh-keygen', '-f', '/root/.ssh/id_rsa', '-t', 'rsa', '-N', '\'\'']
''
是用于传递空字符串的shell语法。您是直接调用命令,而不是通过shell调用,因此不要传递单引号。只需传递空字符串。
fullcmd = ['ssh-keygen', '-f', '/root/.ssh/id_rsa', '-t', 'rsa', '-N', '']