预计脚本问题

发布于 2024-12-09 16:45:25 字数 807 浏览 0 评论 0原文

我试图通过期望完成一项简单的工作。我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪若未夕 2024-12-16 16:45:25

生成的程序发送的输出可能比您想要匹配的输出多。这就是正则表达式匹配如此有用的原因。

试试这个:

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"

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:

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"
与酒说心事 2024-12-16 16:45:25

我想你很快就会退出。这对我有用:

#!/usr/bin/expect

spawn ssh-keygen -t rsa
expect "Enter file in which to save the key (/root/.ssh/id_rsa): "
send  "\r"
expect "Enter passphrase (empty for no passphrase): "
send  "\r"
expect "Enter same passphrase again: "
send  "\r"
expect

I suppose you're exiting to quickly. This one works for me:

#!/usr/bin/expect

spawn ssh-keygen -t rsa
expect "Enter file in which to save the key (/root/.ssh/id_rsa): "
send  "\r"
expect "Enter passphrase (empty for no passphrase): "
send  "\r"
expect "Enter same passphrase again: "
send  "\r"
expect
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文