我试图通过期望完成一项简单的工作。我想在 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"
我不想使用密码。印地语打字"\r"
用于“Enter”键操作。
我尝试运行这段代码"#!/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 正则表达式。
你能帮忙吗。
谢谢。
生成的程序可能会发送比exactly你想要匹配什么。这就是正则表达式匹配如此有用的原因。
尝试这个:
spawn ssh-keygen -t rsa
expect -re {Enter file in which to save the key (/root/.ssh/id_rsa): $}
send -- "\r"
expect -re {Enter passphrase (empty for no passphrase): $}
send -- "\r"
expect -re {Enter same passphrase again: $}
send -- "\r"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)