ftp file upload and download by shell script

Posted by mjahkoh on Sun, 06 Oct 2019 16:16:09 +0200

Some time ago, we need to upload some information verification data from our platform to the FTP server of our customers, so that they can compare and verify the relevant information. Since only four hosts contain this information, the strategy is to aggregate the generated four files onto one host, and then upload the files to the target FTP server on that host.

1. Establish the trust relationship between host A and the other three hosts so as to copy files remotely.

#Generating host A Local authentication key, optionally generated rsa perhaps dsa Type of secret key, selected here rsa
[root@A ~]#ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa >/dev/null 2>&1
#In the authorized_keys file of the other three hosts that copy the secret keys of this machine, since there are only three hosts, one can execute one by one. If the number of hosts is large, the expect script can be used to execute them in batches.
[root@A ~]#ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 22 root@192.168.1.B"
[root@A ~]#ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 22 root@192.168.1.C"
[root@A ~]#ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 22 root@192.168.1.D"
#When the root password of B, C and D is entered, enter the password and return to the train. When the following prompt appears, the trust relationship between the local computer and the target host is successfully established.
Now try logging into the machine, with "ssh '-p 22 root@192.168.1.B'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

2. Create scripts to upload files to the target FTP server

Customer's requirement is to upload the data of the previous day every morning, and the data files we generate are of the type of date host name Result.csv.

#The following shows the content of the script
[root@A getfile] cat upload_csv.sh
#!/bin/bash
#This script is used to upload the generated data files to the client FTP server at regular intervals every day.

#Specify the home directory where the uploaded files are located
SRCDIR=/tmp/test_jr/getfile/files/
#Specify the required upload counterpart FTP Directories of servers
DESDIR=/JRAQ_FILE/

#Specify opposite ends FTP Server username and password
USER=finftp
PASSWD="ABC@123"
#Designated target FTP Server IP
IP=3.3.3.3
#Designated target FTP Server ports are generally default
PORT=21

#Specify the date of the file to be uploaded
targetDay=`date -d "-1 days" +"%Y%m%d"`

#Get the file to upload
cd $SRCDIR
for host in 192.168.1.B 192.168.1.C 192.168.1.D
do
    scp root@$host:$SRCDIR/$targetDay_*_Result.csv ./
done
#Determine whether the document is acquired correctly
[ $? -eq 0 ] || echo "Copy romote files failed, pls check." >>$SRCDIR/upload_file.log

#Upload files to FTP The server
ftp -ivn <<EOF
    open $IP $PORT
    user $USER $PASSWD
    binary
    cd $DESDIR
    lcd $SRCDIR
    put ${targetDay}_*_Result.csv
    quit
EOF

#Determine whether the file was uploaded successfully
[ $? -eq 0 ] && echo "Upload $targetDay's files to romote FTP server successful." >>$SRCDIR/upload_file.log || echo "Upload files failed, pls check." >>$SRCDIR/upload_file.log

3. Transcoding that may be used

Since the csv file can be opened with Excel, when all the utf-8 encoded files are opened by Excel, all Chinese names will display the garbled code. At this time, transcoding is needed, such as converting utf-8 format into gbk encoding, Excel can be opened perfectly.

[root@A ~]iconv -futf8 -tgbk -c -o file2.csv file1.csv
#file1 Is a document to be transcoded
#file2 is the name of the transcoded file
#- f --from-code is transformed from that format
#- t --to-code into that format
#- o --output output output file name
#- c ignores the output illegal characters plus this parameter to prevent some illegal strings in the file. If iconv does not add this parameter, it will automatically terminate the transcoding after encountering the illegal strings, even if there are normal untranslated characters.

Topics: Linux ftp ssh Excel encoding