我想写将文件上载到服务器的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。 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:
troy.jdmz/rsync/index.html
您也可以ssh方式连接到服务器后,执行一个命令:
You can also execute a single command after sshing into a server:
从男人SSH
SSH [...剪断...]主机名[命令]如果指定命令,它是 远程主机,而不是一个登录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.
首先,你可以使用命令传递回声和管
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是不安全的,比较有限的,你真的真的想脚本吧...
If you understand that FTP is insecure and more limited and you really really want to script it...
在www.stratigery/scripting.ftp.html
#!/bin/sh HOST='ftp.example' 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在-n到FTP确保命令不会尝试从当前终端获取密码。其他花哨的部分是使用定界符组成:&LT;&LT; END_SCRIPT 开始定界符,然后把相同的 END_SCRIPT 上由本身的行的开始结束定界符
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.
更多推荐
如何上传在bash脚本(FTP)的文件服务器?
发布评论