【问题标题】:expect: RSA key generation using expect script is not working期望:使用期望脚本生成 RSA 密钥不起作用
【发布时间】:2019-01-28 23:49:13
【问题描述】:

我正在尝试使用期望脚本使用 ssh-keygen 命令生成 SSH 密钥,但它似乎无法正常工作(如果密钥已经存在,它无法创建新密钥或覆盖)我无法找出问题在这。

 #!/usr/bin/expect
spawn ssh-keygen
expect { -re "Enter file in which to save the key.*:\s?" {send "\n\r"; exp_continue}
         -re "Overwrite.*\?\s?" {send "y\r"; exp_continue}
         -re "Enter passphrase.*:\s?" {send "\n\r"; exp_continue}
         -re ".*#\s?" { puts "matched propmt"}
}

脚本执行的输出:

./ssh-key-gen.sh
spawn ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): [root@localhost scripts]#

【问题讨论】:

  • Expect 带有一个 autoexpect 二进制文件。运行它,然后在结果提示符处运行您的目标命令以完成。完成后,输入exit。将生成一个包含完整期望脚本的script.exp 文件。我发现这是解决这类问题的好工具。
  • 你试过开启Expect的调试模式吗?
  • 我想处理检查是否提示我覆盖键或继续第一次执行时的情况。

标签: linux tcl expect


【解决方案1】:

问题是send "\n\r" 响应“输入保存密钥的文件”。 \n 接受默认文件名,然后将 \r 应用于“覆盖(y/n)?”迅速的。对该问题的空响应被解释为“否”,因此命令终止。摆脱那个\n,它就可以工作了。

但是这样做不是更容易吗:

set file [file normalize ~/.ssh/id_rsa]
file delete $file
exec ssh-keygen -f $file -N {}

这可以在纯 Tcl 中完成,无需期望。

【讨论】:

  • #!/usr/bin/expect spawn ssh-keygen expect { debug 1 -re "输入保存密钥的文件。*:\s?" {发送“\r”; exp_continue} -re "覆盖。*\?\s?" {发送“y\r”; exp_continue} -re "输入密码。*:\s?" {发送“\r”; exp_continue} -re ".*#\s?" { puts "匹配的道具"} }
  • 我在发布之前自己尝试过,我建议的更改对我有用。除此之外,我只能建议将exp_internal 1 放在脚本的顶部。
  • 谢谢,问题是我在期望块开始的同一行中有“-re”语句与模式不匹配,将正则表达式移动到下一行后,它工作正常.
猜你喜欢
  • 2019-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-01
  • 2015-08-25
  • 1970-01-01
相关资源
最近更新 更多