开发者

Pipe stdin into "expect" script

开发者 https://www.devze.com 2023-03-14 23:19 出处:网络
I am uploading a file via ftp using expect. The file is piped into my bash script. #!/bin/bash HOST=\'example.com\'

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消