-
Notifications
You must be signed in to change notification settings - Fork 14
/
upload_to_ftp.sh
executable file
·82 lines (72 loc) · 2.16 KB
/
upload_to_ftp.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash
#
# Uploads files to an anonymous ftp server
#
# $1 : FTP server host
# $2 : Remote FTP folder where files are going to be stored
# $3 : Local FTP folder where to get the files from
# $4 : Local files extension.
upload_to_ftp ()
{
# Uploads files from local folder to a given FTP server.
# Params:
# $1 : FTP server host
# $2 : Remote FTP folder where files are going to be stored
# $3 : Local FTP folder where to get the files from
# $4 : Local files extension.
#
FTP_USERNAME="anonymous"
FTP_PASSWORD="anonymous@anonymous.com"
FTP_SERVER=$1 # Ftp server
FTP_DIR=$2 # Dest folder where files are going to be stored
LOCAL_DIR=$3 # Local folder where files are stored
LOCAL_EXTENSION=$4 # Extension of the files to be uploaded
echo 'FTP_SERVER:'$FTP_SERVER
echo 'FTP_DIR:'$FTP_DIR
echo 'LOCAL_DIR:'$LOCAL_DIR
echo 'LOCAL_EXTENSION:'$LOCAL_EXTENSION
echo 'About to upload these files,'
ls -lart $LOCAL_DIR/*.$LOCAL_EXTENSION
cd $LOCAL_DIR
for f in *.$LOCAL_EXTENSION ;
do
if [[ "$f" == *.$LOCAL_EXTENSION ]]
then
echo 'Uploading "'$f'" to "'$FTP_SERVER'"';
FTP_LOG=`mktemp`
ftp -inv $FTP_SERVER > $FTP_LOG 2>&1 <<-EOF
user $FTP_USERNAME $FTP_PASSWORD
binary
cd $FTP_DIR
mput $f
quit
EOF
echo 'FTP command executed'
echo ' >>> '
cat $FTP_LOG
echo ' <<< '
# Now check the output of the ftp cmd
# to see if we've been succesful.
FTP_SUCCESS_MSG="226 Transfer complete"
if fgrep "$FTP_SUCCESS_MSG" $FTP_LOG ;then
echo 'Success uploading the file using FTP'
else
echo 'Error: FTP problem' && exit 1
fi
fi
done
}
EXPECTED_ARGS=4
# $1 : FTP server host
# $2 : Remote FTP folder where files are going to be stored
# $3 : Local FTP folder where to get the files from
# $4 : Local files extension.
if [ $# -ne $EXPECTED_ARGS ]
then
echo "Usage: $0 <ftp-server> <ftp-dir> <local-path> <files-extension>"
echo "<ftp-server> ftp to store the rpms."
echo "<ftp-dir> ftp folder path."
echo "<local-path> local path."
echo "<files-extension> local files extension."
else
upload_to_ftp $1 $2 $3 $4