bash 如何在bash脚本中将(FTP)文件上传到服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1894347/
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
How to upload (FTP) files to server in a bash script?
提问by Andrew
I'm trying to write a bash script that uploads a file to a server. How can I achieve this? Is a bash script the right thing to use for this?
我正在尝试编写一个将文件上传到服务器的 bash 脚本。我怎样才能做到这一点?bash 脚本是否适合用于此目的?
回答by Brian Agnew
You can use a heredocto do this e.g.
您可以使用heredoc来执行此操作,例如
ftp -n $Server <<End-Of-Session
# -n option disables auto-logon
user anonymous "$Password"
binary
cd $Directory
put "$Filename.lsm"
put "$Filename.tar.gz"
bye
End-Of-Session
so the ftp process is fed on stdin with everything up to End-Of-Session
. A useful tip for spawning any process, not just ftp! Note that this saves spawning a separate process (echo, cat etc.). Not a major resource saving, but worth bearing in mind.
所以 ftp 进程是在标准输入上提供的,所有东西都高达End-Of-Session
. 生成任何进程的有用提示,而不仅仅是 ftp!请注意,这可以避免生成单独的进程(echo、cat 等)。不是主要的资源节省,但值得牢记。
回答by greggles
Below are two answers. First is a suggestion to use a more secure/flexible solution like ssh/scp/sftp. Second is an explanation of how to run ftp in batch mode.
下面是两个答案。首先是建议使用更安全/灵活的解决方案,如 ssh/scp/sftp。其次是解释如何在批处理模式下运行 ftp。
A secure solution:
一个安全的解决方案:
You really should use SSH/SCP/SFTP for this rather than FTP. SSH/SCP have the benefits of being more secure and working with public/private keys which allows it to run without a username or password.
您确实应该为此使用 SSH/SCP/SFTP 而不是 FTP。SSH/SCP 具有更安全和使用公钥/私钥的优点,允许它在没有用户名或密码的情况下运行。
You can send a single file:
您可以发送单个文件:
scp <file to upload> <username>@<hostname>:<destination path>
Or a whole directory:
或者整个目录:
scp -r <directory to upload> <username>@<hostname>:<destination path>
For more details on setting up keys and moving files to the server with RSYNC, which is useful if you have a lot of files to move, or if you sometimes get just one new file among a set of random files, take a look at:
有关使用 RSYNC 设置密钥和将文件移动到服务器的更多详细信息,如果您要移动大量文件,或者有时在一组随机文件中只获得一个新文件,这将非常有用,请查看:
http://troy.jdmz.net/rsync/index.html
http://troy.jdmz.net/rsync/index.html
You can also execute a single command after sshing into a server:
您还可以在 SSH 到服务器后执行单个命令:
From man ssh
从 man ssh
ssh [...snipped...] hostname [command] If command is specified, it is executed on the remote host instead of a login shell.
ssh [...snipped...] hostname [command] 如果指定了 command,它会在远程主机上执行,而不是在登录 shell 上执行。
So, an example command is:
所以,一个示例命令是:
ssh [email protected] bunzip file_just_sent.bz2
If you can use SFTP with keys to gain the benefit of a secured connection, there are two tricks I've used to execute commands.
如果您可以使用带有密钥的 SFTP 来获得安全连接的好处,那么我使用两个技巧来执行命令。
First, you can pass commands using echo and pipe
首先,您可以使用 echo 和 pipe 传递命令
echo "put files*.xml" | sftp -p -i ~/.ssh/key_name [email protected]
You can also use a batchfile with the -b
parameter:
您还可以使用带有-b
参数的批处理文件:
sftp -b batchfile.txt ~/.ssh/key_name [email protected]
An FTP solution, if you really need it:
一个 FTP 解决方案,如果你真的需要它:
If you understand that FTP is insecure and more limited and you really really want to script it...
如果您了解 FTP 不安全且更受限制,并且您真的很想编写它的脚本...
There's a great article on this at http://www.stratigery.com/scripting.ftp.html
在http://www.stratigery.com/scripting.ftp.html上有一篇很棒的文章
#!/bin/sh
HOST='ftp.example.com'
USER='yourid'
PASSWD='yourpw'
FILE='file.txt'
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
binary
put $FILE
quit
END_SCRIPT
exit 0
The "-n" to ftp ensures that the command won't try to get the password from the current terminal. The other fancy part is the use of a heredoc: the <<END_SCRIPT
starts the heredoc and then that exact same END_SCRIPT
on the beginning of the line by itself ends the heredoc. The binary
command will set it to binary mode which helps if you are transferring something other than a text file.
ftp 的“-n”确保该命令不会尝试从当前终端获取密码。另一个花哨的部分是使用heredoc:<<END_SCRIPT
开始heredoc,然后END_SCRIPT
在行的开头完全相同,结束heredoc。该binary
命令会将其设置为二进制模式,这在您传输文本文件以外的内容时会有所帮助。
回答by Paul Tomblin
Install ncftpput and ncftpget. They're usually part of the same package.
安装 ncftpput 和 ncftpget。它们通常是同一个包的一部分。
回答by Pelle Nilsson
The ftp
command isn't designed for scripts, so controlling it is awkward, and getting its exit status is even more awkward.
该ftp
命令不是为脚本设计的,因此控制它很笨拙,获得其退出状态更笨拙。
Curlis made to be scriptable, and also has the merit that you can easily switch to other protocols later by just modifying the URL. If you put your FTP credentials in your .netrc, you can simply do:
Curl是可编写脚本的,并且还有一个优点,您可以稍后通过修改 URL 轻松切换到其他协议。如果您将 FTP 凭据放在.netrc 中,则只需执行以下操作:
# Download file
curl --netrc --remote-name ftp://ftp.example.com/file.bin
# Upload file
curl --netrc --upload-file file.bin ftp://ftp.example.com/
If you must, you can specify username and password directly on the command line using --user username:password
instead of --netrc
.
如果必须,您可以使用--user username:password
代替直接在命令行上指定用户名和密码--netrc
。
回答by Shal
use this to upload a file to a remote location
使用它将文件上传到远程位置
#!/bin/bash
# is the file name
#usage:this_script <filename>
HOST='your host'
USER="your user"
PASSWD="pass"
FILE="abc.php"
REMOTEPATH='/html'
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
cd $REMOTEPATH
put $FILE
quit
END_SCRIPT
exit 0
回答by Chizhik
command in one line:
一行命令:
ftp -in -u ftp://username:password@servername/path/to/ localfile
回答by ennuikiller
#/bin/bash
# is the file name
# usage: this_script <filename>
IP_address="xx.xxx.xx.xx"
username="username"
domain=my.ftp.domain
password=password
echo "
verbose
open $IP_address
USER $username $password
put
bye
" | ftp -n > ftp_$$.log
回答by Martin Hvidberg
No need to complicate stuff - this should work:
无需使事情复杂化 - 这应该有效:
#/bin/bash
echo "
verbose
open ftp.mydomain.net
user myusername mypassword
ascii
put textfile1
put textfile2
bin
put binaryfile1
put binaryfile2
bye
" | ftp -n > ftp_$$.log
or you can use mput if you have many files ...
或者如果你有很多文件,你可以使用 mput ...
回答by Shal
Working Example to Put Your File on Root ...........see its very simple
将文件放在 Root 上的工作示例.......... 看到它非常简单
#!/bin/sh
HOST='ftp.users.qwest.net'
USER='yourid'
PASSWD='yourpw'
FILE='file.txt'
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put $FILE
quit
END_SCRIPT
exit 0
回答by Naveen Sharma
cd C:\Program Files (x86)\WinSCP
cd C:\Program Files (x86)\WinSCP
winscp.exe /console /command "open UserName:**@Server" "put File path"
winscp.exe /console /command "打开用户名:**@Server" "放置文件路径"