期望脚本生成 sftp 中的 EOF

EOF in expect script spawning sftp

提问人:osia6028 提问时间:12/19/2021 最后编辑:Rachid K.osia6028 更新时间:12/20/2021 访问量:1477

问:

我有一个脚本,将文件从 linux 服务器传输到 windows 服务器。我想记录与传输相关的数据,但 EOF 在 HEREDOC 构造中给了我错误。谁能告诉我前进的方向。

我的脚本是:

#!/usr/bin/expect
spawn sftp XXXX@XXXXXX <<EOF>> log.file
expect "password:"
send "ABC\n"
expect "sftp>"
send "cd /FIRST\r"
expect "sftp>"
send "lcd /home\r"
expect "sftp>"
send "mput /home/*First*\r"
send "bye\r"
interact
Linux sftp 期望 eof heredoc

评论


答:

1赞 Rachid K. 12/19/2021 #1

请改用 shell 脚本并调用传递给它的“-”,以使其从其标准输入中读取,该输入将是 HEREDOC(即 <<EOF ...EOF):expect

#!/bin/sh

/usr/bin/expect - <<EOF >> /tmp/log
spawn sftp XXXX@XXXXXX
expect "password:"
send "ABC\n"
expect "sftp>"
send "cd /FIRST\r"
expect "sftp>"
send "lcd /home\r"
expect "sftp>"
send "mput /home/*First*\r"
send "bye\r"
EOF
2赞 glenn jackman 12/20/2021 #2

#!/usr/bin/expect
log_file -a log.file
spawn sftp XXXX@XXXXXX
# ... the rest is all the same.

如果你实际上没有(作为人类)与 sftp 进程进行交互,你可以将其用作最后一行

expect eof