预期脚本问题 [英] Expect script issue

查看:93
本文介绍了预期脚本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过期望完成一项简单的工作.我想在Linux VM上使用"ssh-keygen"命令创建ssh密钥.我的低于预期的代码看起来很简单,但没有用:

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"

我不想使用任何密码短语.因此,在"Enter"键操作中键入"\r". 我尝试使用"#!/usr/bin/expect -d"运行此代码,但发现它与我提到的字符串不匹配.如下所示:

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
....

所以我想,因为它无法匹配模式,所以我的脚本失败了. 问题是,为什么它不能匹配模式.我正在使用"-exact",但仍然无法匹配模式.我曾尝试玩"-re",但我认为我不擅长TCL regex.

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.

您能帮忙吗? 谢谢.

推荐答案

生成的程序发送的输出可能比精确地要匹配的输出更多.这就是正则表达式匹配如此有用的原因.

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.

尝试一下:

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"

这篇关于预期脚本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆