【发布时间】:2011-12-05 12:12:23
【问题描述】:
我正在尝试通过期望完成一项简单的工作。我想在 Linux VM 上使用“ssh-keygen”命令创建 ssh 密钥。我下面的期望代码看起来很简单,但它不起作用:
#!/usr/bin/expect
spawn ssh-keygen -t rsa
expect -exact "Enter file in which to save the key (/root/.ssh/id_rsa): "
send -- "\r"
expect -exact "Enter passphrase (empty for no passphrase): "
send -- "\r"
expect -exact "Enter same passphrase again: "
send -- "\r"
我不想使用任何密码短语。因此为“Enter”键操作输入"\r"。
我尝试使用"#!/usr/bin/expect -d" 运行此代码,但我发现它与我提到的字符串不匹配。如下所示:
...
expect: does "" (spawn_id exp6) match exact string "Enter file in which to save the key (/root/.ssh/id_rsa): "? no
....
所以我假设它无法匹配模式,我的脚本失败了。
问题是,为什么它不能匹配模式。我正在使用"-exact",但它仍然无法匹配模式。我试图玩弄"-re",但我认为我不擅长 TCL 正则表达式。
你能帮忙吗? 谢谢。
【问题讨论】: