简体   繁体   English

在python中使用pexpect的“ ssh-keygen -t rsa”未按预期生成公共文件

[英]“ssh-keygen -t rsa” using pexpect in python does not generate the public file as expected

While executing the "ssh-keygen -t rsa" command using pexpect in python, the command doesn't generate the "id_rsa.pub" file as expected, sometimes the .ssh folder will be empty and sometimes contains the "known hosts" file. 在python中使用pexpect执行“ ssh-keygen -t rsa”命令时,该命令未按预期生成“ id_rsa.pub”文件,有时.ssh文件夹为空,有时包含“已知主机”文件。

Following is the python code snippet: 以下是python代码段:

import pexpect
command = 'ssh-keygen -t rsa'
print command
child = pexpect.spawn(command)
child.delaybeforesend=5
file_prompt = 'Enter file'
pwd_prompt = 'passphrase'
empty_string = '\n'
res = child.expect([pexpect.EOF,pexpect.TIMEOUT,file_prompt])
child.sendline(empty_string)
res = child.expect([pexpect.EOF,pexpect.TIMEOUT,pwd_prompt])
child.sendline(empty_string)
res = child.expect([pexpect.EOF,pexpect.TIMEOUT,pwd_prompt])
child.sendline(empty_string)
res = child.expect([pexpect.TIMEOUT,'$'])
child.close()

The same when executed line by line on the interpreter..everything goes fine. 在解释器上逐行执行时,效果相同。一切都很好。 It generates the id_rsa.pub file as expected. 它会按预期生成id_rsa.pub文件。

Tried out several runs but still could not figure out the reason for the same. 尝试了几次运行,但仍无法弄清相同的原因。 I added the : 我添加了:

child.delaybeforesend=5

only to make sure that the key generation is completing. 仅确保密钥生成已完成。 Please help on this. 请帮忙。

Below is the reference command prompt : 下面是参考命令提示符:

[tecmint@tecmint.com ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/tecmint/.ssh/id_rsa): [Pressenter 
key]
Created directory '/home/tecmint/.ssh'.
Enter passphrase (empty for no passphrase): [Press enter key]
Enter same passphrase again: [Press enter key]
Your identification has been saved in /home/tecmint/.ssh/id_rsa.
Your public key has been saved in /home/tecmint/.ssh/id_rsa.pub.
The key fingerprint is:
5f:ad:40:00:8a:d1:9b:99:b3:b0:f8:08:99:c3:ed:d3 tecmint@tecmint.com
The key's randomart image is:

With my context: 根据我的情况:

.ssh folder is either empty or contains "known_hosts" file when i run the code snippet. 当我运行代码片段时,.ssh文件夹为空或包含“ known_hosts”文件。

and when I run the same code , line by line on the interpreter, it generates the id_rsa and the id_rsa.pub 当我在解释器上逐行运行相同的代码时,它将生成id_rsa和id_rsa.pub

You are not waiting for the prompt ssh-keygen is sending so pexpect doesn't know to send the passphrase. 您不必等待ssh-keygen发送提示,因此pexpect不知道发送密码。 Adjust your prompt . 调整提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM