如何在 ruby​​ on Rails 中使用 Expect 脚本?

发布于 2024-10-24 01:07:18 字数 715 浏览 5 评论 0原文

你能帮我吗?我怎样才能在ruby中使用expect脚本?

spawn scp /tmp/*.txt remoteserver:/vg/
expect "password"
send "MY PASSWORD"
interact

因为,我需要通过 scp 命令将批量文件从一台服务器传输到另一台服务器。但是,当我使用通配符文件传输(/tmp/*.txt)时,它显示以下错误

没有这样的文件或目录 被信号 1 杀死。

然后,我更改了代码,如下所示。 (注:我直接使用文件名)。

spawn scp /tmp/first.txt remoteserver:/vg/
expect "password"
send "MY PASSWORD"
interact

所以,我刚刚跳入了 Rails。 <代码>

$files = Dir.glob("/tmp/*.txt")
for f in @files
[I need to use the expect script here]
expect {....#{f}...}
end

<代码> 谢谢

Can you please help me?. How can i use the expect script in ruby.

spawn scp /tmp/*.txt remoteserver:/vg/
expect "password"
send "MY PASSWORD"
interact

Because, i need to transfer the bulk files from one server to another server through scp command. But, when i use the wildcard file transfer (/tmp/*.txt) it shows the following error

No such file or directory
Killed by signal 1.

Then, i had changed my code like as follows. (Note: i have used the file name directly).

spawn scp /tmp/first.txt remoteserver:/vg/
expect "password"
send "MY PASSWORD"
interact

So , I have just jump into rails.

$files = Dir.glob("/tmp/*.txt")
for f in @files
[I need to use the expect script here]
expect {....#{f}...}
end


Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

治碍 2024-10-31 01:07:18

您确实想为此使用 ssh 密钥。设置起来并不难,所以你不用担心。

您可能希望您的期望脚本如下所示:

set scp_command [linsert $argv0 scp]
lappend scp_command remote_server:/vg/
eval spawn $scp_command

expect password
send "MYPASSWORD\r"
expect eof

Ruby 部分类似于

system "expect", "expect_script.exp", Dir.glob("/tmp/*.txt")

You do want to use ssh keys for this. It's not hard to set up, then you don't have to worry about it.

You probably want your expect script to look like this:

set scp_command [linsert $argv0 scp]
lappend scp_command remote_server:/vg/
eval spawn $scp_command

expect password
send "MYPASSWORD\r"
expect eof

and the Ruby part would be something like

system "expect", "expect_script.exp", Dir.glob("/tmp/*.txt")
独闯女儿国 2024-10-31 01:07:18

Glenn 提到使用 ssh 密钥 - 当您使用 Expect 脚本时,它们肯定会让您的生活更轻松。

几年前,我写了一篇关于如何在 Windows 上通过 putty 生成和使用 ssh 密钥的博客文章。

http:// soniahamilton.wordpress.com/2008/09/05/how-to-use-putty-with-ssh-keys-on-windows/

Glenn mentioned using ssh keys - they certainly make your life easier when you're working with Expect scripts.

A few year ago I wrote a blog post on how to use generate and use ssh keys with putty on Windows.

http://soniahamilton.wordpress.com/2008/09/05/how-to-use-putty-with-ssh-keys-on-windows/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文