本shell基于ftp客户端,在终端运行ftp -h 后,结果如下
Usage: { ftp | pftp } [-46pinegvtd] [hostname]
-4: use IPv4 addresses only
-6: use IPv6, nothing else
-p: enable passive mode (default for pftp)
-i: turn off prompting during mget
-n: inhibit auto-login
-e: disable readline support, if present
-g: disable filename globbing
-v: verbose mode
-t: enable packet tracing [nonfunctional]
-d: enable debugging
测试环境:deepin 20.8
源码
ftp.sh
可以实现从本地向服务器,或从服务器到本地批量传输文件
#!/bin/bash
shell_name=$0
cmd=$1
read username password server port < <(echo $2|awk -F '[@:]' '{print $1,$2,$3,$4}')
port=${port:-"21"}
src_dir=$3
dst_dir=$4
usage(){
cat >&1 <<-EOF
Usage: ${shell_name} <cmd> <username:password@server>[:port] <src> <dist>
cmd:
get from ftp server get download file.
put from local put file to ftp server.
src:
src if cmd is get,it is ftp server,otherwise local server.
dst:
dst if cmd is get,it is local server,otherwise ftp server.
EOF
}
get(){
ftp -n -v <<-EOF
open $server $port
user $username $password
binary
cd $src_dir
lcd $dst_dir
prompt
mget *
bye
EOF
}
put(){
ftp -n -v <<-EOF
open $server $port
user $username $password
binary
cd $dst_dir
lcd $src_dir
prompt
mput *
bye
EOF
}
if [[ $# < 3 || $# > 4 ]];then
echo "wrong number of parameters."
usage
exit
fi
case $cmd in
"get")
get
;;
"put")
put
;;
*)
usage
;;
esac
用法
直接运行,结果如下
wrong number of parameters.
Usage: ./ftp.sh <cmd> <username:password@server>[:port] <src> <dist>
cmd:
get from ftp server get download file.
put from local put file to ftp server.
src:
src if cmd is get,it is ftp server,otherwise local server.
dst:
dst if cmd is get,it is local server,otherwise ftp server.
若未提供端口号,则默认端口号为21
1,从远程ftp服务器批量下载文件
比如假设,ftp服务器的ip为192.168.1.103,端口号为2221,用户名和密码均为admin,现在欲从服务器的/Download目录下载所有文件到本地/tmp目录下
./ftp.sh get admin:admin@192.168.1.103:2221 /Download /tmp
2,从本地主机批量上传文件到ftp服务器
上传本地目录当下所有文件到服务器
./ftp.sh put admin:admin@192.168.1.103:2221 . /Download