【发布时间】: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的调试模式吗?
-
我想处理检查是否提示我覆盖键或继续第一次执行时的情况。