预计脚本问题
我试图通过期望完成一项简单的工作。我想在 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 正则表达式。
你能帮忙吗? 谢谢。
I am trying to accomplish a simple job via expect. I want to create ssh keys using the "ssh-keygen" command on Linux VMs. My below expect code looks to be straight forward but it is not working:
#!/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"
I do not want to use any pass phrase. hence typing "\r"
for "Enter" key action.
I tried running this code with "#!/usr/bin/expect -d"
, and I find that it never matches the strings I have mentioned. something like below:
...
expect: does "" (spawn_id exp6) match exact string "Enter file in which to save the key (/root/.ssh/id_rsa): "? no
....
SO I would presume as it is not able to match the pattern, my script is failing.
The question is, why it is not able to match the pattern. I am using "-exact"
and still it fails to match the patter. I tried to play around with "-re"
but I think I am not good at TCL regex.
Could you help.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
生成的程序发送的输出可能比您想要匹配的输出多。这就是正则表达式匹配如此有用的原因。
试试这个:
The spawned program is likely sending more output than exactly what you're trying to match. That's why regular expression matching is so helpful.
Try this:
我想你很快就会退出。这对我有用:
I suppose you're exiting to quickly. This one works for me: