shell script to realize ftp upload and download files

Some time ago the need to work in our platform after the verification of certain information data extraction uploaded to the customer's FTP server for information related to them than for verification. As the host of this information contains only four, adopted strategy is to generate a collection of four files to a host, then the target ftp server on this host file upload.

1, establish a trust relationship between the host A to the other three host to copy files to remote

Generating a local authentication keys # A host may choose to generate type rsa or dsa keys, select where rsa
[linuxmi the root @ ~] # SSH-keygen -t rsa -P '' -f ~ / .ssh / id_rsa> / dev / null 2> & 1
# will authorized_keys file keys native copy of the three other host, since only three hosts, one by one to perform on the line, if the number of hosts and more, you can use expect scripts batch execution
[root @ ~ linuxmi] SSH-Copy-ID # -i ~ / .ssh / id_rsa.pub "-p [email protected] 22 is"
[linuxmi the root @ ~] SSH-Copy-ID # -i ~ / .ssh / id_rsa.pub "-p [email protected] 22 is"
[linuxmi the root @ ~] SSH-Copy-ID # -i ~ / .ssh / id_rsa.pub "-p [email protected] 22 is"
# this machine represents the establishment of a trust relationship to the target host is successful emergence of input B, and the root password C, D, and enter the password enter, there are suggested
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. Establish a script to upload files to the target FTP server

  Customer demand is the day before uploading data every day in the morning, we generate data files are based on today's date _ hostname _Result.csv type.

# The following shows the contents of the script
[root @ linuxmi getfile] CAT upload_csv.sh
#! / Bin / bash
# This script is used to generate regular daily upload data files to the client FTP server

# Specify the home directory where the file upload
SRCDIR = / tmp / test_jr / getfile / files /
directory # specify the desired upload to end the FTP server
DESDIR = / JRAQ_FILE /

# Indicates that the peer FTP server's user name and password of
the USER = finftp
PASSWD = "ABC @ 123"
IP # specify the destination FTP server
IP = 3.3.3.3
# specify the destination FTP server port, usually the default
PORT = 21

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

# Get the files 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 file get the right
[$? -eq 0] || echo "Copy romote files failed, pls check." >> $ SRCDIR / upload_file.log

# Upload files to FTP server
the FTP -ivn << EOF
    Open $ IP $ PORT
    the User the USER $ $ PASSWD
    binary
    cd $ DESDIR
    lcd $ SRCDIR
    PUT $ {} _ * _ targetDay Result.csv
    quit
EOF

#判定文件是否上传成功
[ $? -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, it is possible to use transcoding

  Csv file is due to be opened in Excel, Excel opens but utf-8 encoded file, all Chinese name is garbled, then you need to use transcoding, such as converting to a utf-8 format gbk encoded Excel It can be the perfect open.

[root @ linuxmi ~] iconv -futf8 -tgbk -c -o file2.csv file1.csv
file # file1 is to be transcoded
# file2 after transcoded file name
# -f --from-code from that format i.e. transformation
# -t --to-code into that format
# -o --output output filename
illegal character # -c ignore output increase this parameter is to prevent illegal file has some strings, if not this iconv after encountering illegal parameter string is terminated automatically transcoding, even if the character is not normal and there are transcoded.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159945.htm