如何在 Bash 脚本中将 (FTP) 文件上传到服务器? [英] How can I upload (FTP) files to server in a Bash script?

查看:21
本文介绍了如何在 Bash 脚本中将 (FTP) 文件上传到服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个将文件上传到服务器的 Bash 脚本.我怎样才能做到这一点?使用 Bash 脚本是否合适?

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?

推荐答案

下面是两个答案.首先是建议使用更安全/更灵活的解决方案,如 ssh/scp/sftp.二是讲解如何以批处理方式运行ftp.

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.SSH/SCP 的好处是更安全,并且可以使用公钥/私钥,无需用户名或密码即可运行.

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.

您可以发送单个文件:

scp <file to upload> <username>@<hostname>:<destination path>

或整个目录:

scp -r <directory to upload> <username>@<hostname>:<destination path>

有关使用 RSYNC 设置密钥和将文件移动到服务器的更多详细信息,如果您有很多文件要移动,或者如果您有时在一组随机文件中只得到一个新文件,这很有用,请采取看看:

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:

http://troy.jdmz.net/rsync/index.html

您也可以在 ssh 到服务器后执行单个命令:

You can also execute a single command after sshing into a server:

来自 man ssh

ssh [...snipped...] hostname [command] 如果指定了命令,则为在远程主机而不是登录 shell 上执行.

ssh [...snipped...] hostname [command] If command is specified, it is executed on the remote host instead of a login shell.

所以,一个示例命令是:

So, an example command is:

ssh username@hostname.example bunzip file_just_sent.bz2

如果您可以使用带有密钥的 SFTP 来获得安全连接的好处,那么我使用了两个技巧来执行命令.

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.

首先,您可以使用 echo 和管道传递命令

First, you can pass commands using echo and pipe

echo "put files*.xml" | sftp -p -i ~/.ssh/key_name username@hostname.example

您也可以使用带有 -b 参数的批处理文件:

You can also use a batchfile with the -b parameter:

sftp -b batchfile.txt ~/.ssh/key_name username@hostname.example

一个 FTP 解决方案,如果你真的需要它:

如果您了解 FTP 不安全且限制更多,并且您真的很想编写脚本...

An FTP solution, if you really need it:

If you understand that FTP is insecure and more limited and you really really want to script it...

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

-n to ftp 确保该命令不会尝试从当前终端获取密码.另一个花哨的部分是使用heredoc:<<END_SCRIPT 启动heredoc,然后在行首的完全相同的END_SCRIPT 自行结束赫雷多克.binary 命令会将其设置为二进制模式,这有助于您传输文本文件以外的内容.

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.

这篇关于如何在 Bash 脚本中将 (FTP) 文件上传到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆