将标准输入通过管道传输到“expect”脚本

发布于 2024-11-17 09:07:03 字数 455 浏览 2 评论 0原文

我正在使用 expect 通过 ftp 上传文件。该文件通过管道传输到我的 bash 脚本中。

#!/bin/bash
HOST='example.com'
USER='XXX'
PASSWD='XXX'

expect << EOT
spawn ftp $HOST
expect "Name*:"
send "$USER\r"
expect "Password:"
send "$PASSWD\r"
expect "ftp>"
send "binary\r"
expect "ftp>"
send "prompt\r"
expect "ftp>"
send "put - $1\r"  ####
expect "ftp>"
send "bye\r"
expect eof
EOT

在突出显示的行上,我希望 ftp 能够访问主脚本 stdin。

谢谢

I am uploading a file via ftp using expect. The file is piped into my bash script.

#!/bin/bash
HOST='example.com'
USER='XXX'
PASSWD='XXX'

expect << EOT
spawn ftp $HOST
expect "Name*:"
send "$USER\r"
expect "Password:"
send "$PASSWD\r"
expect "ftp>"
send "binary\r"
expect "ftp>"
send "prompt\r"
expect "ftp>"
send "put - $1\r"  ####
expect "ftp>"
send "bye\r"
expect eof
EOT

On the highlighted line I want ftp to get access to the main script stdin.

Thank you

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

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

发布评论

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

评论(1

酒几许 2024-11-24 09:07:03

我相信您正在寻找的关键是 Expect 的 interact 命令。您会得到这样的脚本:

#!/usr/bin/env expect

# Set these in the Tcl way, not the bash way
set HOST "example.com"
set USER "XXX"
set PASSWD "YYY"

# You should recognize this bit...
spawn ftp $HOST
expect "Name*:"
send "$USER\r"
expect "Password:"
send "$PASSWD\r"
expect "ftp>"
send "binary\r"
expect "ftp>"
send "prompt\r"
expect "ftp>"

# New stuff starts here
send "put - [lindex $argv 0]\r"
interact {
    "ftp>" { return }
}
send "bye\r"
wait
exit

我重写了您的脚本,因此它不使用此处文档,因为这干扰内容的阅读(此处文档呈现为stdin...)并将其切换为使用一些更惯用的做事方式(惯用的参数访问是主要的)。

也就是说,如果我真的在做这种事情,我会考虑使用 ftp 来自 Tcllib 的包,因为它直接与协议对话,而不是使用可能的协议-有问题的子流程。 (事实上​​,如果您要在 Windows 上执行此操作,您必须这样做,因为 Expect 和 FTP.EXE 在该平台上的工作方式存在一些怪癖)。

I believe the key to what you are looking for is Expect's interact command. You'd get a script like this:

#!/usr/bin/env expect

# Set these in the Tcl way, not the bash way
set HOST "example.com"
set USER "XXX"
set PASSWD "YYY"

# You should recognize this bit...
spawn ftp $HOST
expect "Name*:"
send "$USER\r"
expect "Password:"
send "$PASSWD\r"
expect "ftp>"
send "binary\r"
expect "ftp>"
send "prompt\r"
expect "ftp>"

# New stuff starts here
send "put - [lindex $argv 0]\r"
interact {
    "ftp>" { return }
}
send "bye\r"
wait
exit

I've rewritten your script so it doesn't use a here document, because that would have interfered with the reading of the content (here-docs are presented as stdin…) and switched it to use a few more idiomatic ways of doing things (idiomatic argument access being the main one).

That said, if I was doing this sort of thing for real, I'd look into using the ftp package from Tcllib as that talks the protocol directly instead of using a possibly-problematic subprocess. (Indeed, if you were going to be doing this on Windows, you'd have to do it that way because of quirks of how Expect and FTP.EXE work on that platform).

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