bash SFTP使用bash脚本发送文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11744547/
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
SFTP to send file with bash script
提问by Bing
I'm using key authentication, so password is not an issue. I have a file whose name I know and I simply want to send it to another machine over sftp.
我正在使用密钥身份验证,因此密码不是问题。我有一个文件,我知道它的名字,我只想通过 sftp 将它发送到另一台机器。
I tried searching but couldn't find this (seemingly simple) question anywhere. Perhaps my Google-fu is simply failing me today.
我尝试搜索,但在任何地方都找不到这个(看似简单的)问题。也许我的 Google-fu 今天让我失望了。
In short: I'm on my local machine, want to send a file (test.txt) to a remote machine. Authorized keys are already provided. Basically I want to automate these three steps:
简而言之:我在我的本地机器上,想将文件 (test.txt) 发送到远程机器。已提供授权密钥。基本上我想自动化这三个步骤:
sftp root@remote:/root/dropoff
put test.txt
quit
Is there a simple bash command I can use to automate this? The only option I've seen is using a bash script to perform the put/quit and using the -b option to run it. Is there anything cleaner than that? (I'm not interested in using any other applications/tools.)
是否有一个简单的 bash 命令可以用来自动执行此操作?我见过的唯一选项是使用 bash 脚本来执行放置/退出并使用 -b 选项来运行它。还有比这更干净的吗?(我对使用任何其他应用程序/工具不感兴趣。)
Thanks!
谢谢!
采纳答案by jordanm
You said that you are not interested in other tools, but scp
is a much better choice for unattended file transfers. Here is an scp example:
你说你对其他工具不感兴趣,但scp
对于无人值守的文件传输来说是一个更好的选择。这是一个 scp 示例:
scp test.txt root@remote:/root/dropoff
回答by Mike McMahon
I know this is an old one, but you can also pass arguments to a command with a Here Document
我知道这是一个旧的,但您也可以将参数传递给带有Here Document的命令
You can put the following into a script:
您可以将以下内容放入脚本中:
# The following is called a HERE document
sftp <user>@<remote> << SOMEDELIMITER
put test.txt
... # any commands you need to execute via sftp
quit
SOMEDELIMITER
each additional command will be fed into the command preceeding the <<
and SOMEDELIMTER
can be anything you want it to be.
每个附加命令都将输入到 之前的命令中,<<
并且SOMEDELIMTER
可以是您想要的任何命令。
scp is a great option, however sftp was the only tool I was able to get working when pushing from linux to windows and you're stuck using FreeSSHD in service mode!
scp 是一个很好的选择,但是 sftp 是我从 linux 推送到 windows 时能够开始工作的唯一工具,而您在服务模式下使用 FreeSSHD 时卡住了!
回答by user3061675
I don't know how sftp was configurable when this question was asked. Anyway, 6 years later, you can put sftp-commands like PUT into a file and then reference this file in your initial sftp-call. The makes the whole process completely non-interactive and easily configurable:
当问到这个问题时,我不知道 sftp 是如何配置的。无论如何,6 年后,您可以将 PUT 等 sftp 命令放入文件中,然后在初始 sftp 调用中引用该文件。这使得整个过程完全非交互式且易于配置:
sftp -i /path/to/ssh-key -b /path/to/sftp-commands.txt root@remote:/root/dropoff
....Where sftp-commands.txt just contains
....其中 sftp-commands.txt 只包含
put test.txt; quit