bash 脚本中的期望脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25006406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:59:04  来源:igfitidea点击:

expect script inside bash script

linuxbashexpectscp

提问by John

I am really stumped! I DONT WANT TO USE RSA AUTH SO PLEASE ABSTAIN.

我真的很难过!我不想使用 RSA 授权,所以请弃权。

#!/bin/bash

echo "Enter the server password" 
read password 
cd /home/mike/ 

# this is included the code below -> 
/usr/bin/expect << EOD
set timeout -1
spawn scp file.txt server.com:/home
expect {
  timeout            { send_user "TIME OUT\n"; exit 1 }
  "*assword: "
}
send "$password\r"
expect {
  "s password: " { send_user "\nIncorrect Password. Login Failed.\n"; exit 1 }
  "100%"
}
sleep 1
send "exit\r"
expect eof

This works and file.txt gets transferred to the server but I get this warning message ->

这有效并且 file.txt 被传输到服务器,但我收到此警告消息 - >

" line 44: warning: here-document at line 22 delimited by end-of-file (wanted `EOD')"

When I add EOD at the end after 'expect eof' it gives me this error ->

当我在“expect eof”之后添加 EOD 时,它给了我这个错误 ->

send: spawn id exp4 not open
    while executing
"send "exit\r"

Any help would be appreciated. I am saying again I cant use pubkey auth so please dont suggest that.

任何帮助,将不胜感激。我再说一遍我不能使用公钥身份验证所以请不要建议。

回答by julumme

By replacing your last line:

通过替换你的最后一行:

expect eof

with:

和:

EOD

I don't get the complaint anymore. Note that you cannot have any tabs (and maybe spaces as well?) before your EODtag (which you defined in your initial /usr/bin/expectline.

我不再收到投诉了。请注意,在EOD标签(您在初始/usr/bin/expect行中定义的)之前不能有任何制表符(也可能有空格?)。

回答by mti2935

Instead of trying to script this using expect, you can simply use curlto copy files to/from a remote host via scp or sftp, and pass the authentication credentials to the curl command like so:

expect您可以简单地使用curl通过 scp 或 sftp 将文件复制到远程主机或从远程主机复制文件,而不是尝试编写脚本,并将身份验证凭据传递给 curl 命令,如下所示:

curl sftp://remotehost.domain.com/path/to/remote/file --user username:password -o /path/to/local/file