【问题标题】:"ssh-keygen -t rsa" using pexpect in python does not generate the public file as expected在 python 中使用 pexpect 的“ssh-keygen -t rsa”不会按预期生成公共文件
【发布时间】:2018-02-17 19:24:46
【问题描述】:

在 python 中使用 pexpect 执行“ssh-keygen -t rsa”命令时,该命令不会按预期生成“id_rsa.pub”文件,有时 .ssh 文件夹将为空,有时包含“已知主机”文件。

以下是python代码sn-p:

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()

在解释器上逐行执行时也是如此。一切都很好。它会按预期生成 id_rsa.pub 文件。

尝试了几次运行,但仍然无法找出相同的原因。 我添加了:

child.delaybeforesend=5

只是为了确保密钥生成完成。 请帮忙。

下面是参考命令提示符:

[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:

根据我的情况:

当我运行代码 sn-p 时,.ssh 文件夹为空或包含“known_hosts”文件。

当我在解释器上逐行运行相同的代码时,它会生成 id_rsa 和 id_rsa.pub

【问题讨论】:

  • ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa?

标签: python ssh pexpect


【解决方案1】:

您没有等待 ssh-keygen 正在发送的提示,因此 pexpect 不知道要发送密码。调整提示。

【讨论】:

  • Is'nt "res = child.expect([pexpect.EOF,pexpect.TIMEOUT,file_prompt])" 假设这样做?
  • 您的 pwd_prompt 不正确匹配。
猜你喜欢
  • 1970-01-01
  • 2019-01-20
  • 2016-09-15
  • 2017-07-30
  • 2011-07-13
  • 2019-05-05
  • 1970-01-01
相关资源
最近更新 更多