Need a Shell Script for FTP

Hello Brothers,

I am new in shell script.I need a shell script that will run in Linux Server. Script will connect to windows FTP server before connection script will check the connection from linux server to windows server, if connection is ok then show a message and get specific file and compare the local and remote file size if file size is perfect than dispaly successful message. If connection fails or file size is differ display a error message.

Thanks
Maruf

What have you tried?

FTP is very simple and stupid. You may end up connecting and quitting to check, connecting and retrieving a file and quitting to retrieve a file and compare it locally, since ftp has no "if" itself, it will all be done in the shell.

Hi,

Could you please sample script for that? I only know about the simple ftp commands that only bring the file from one server to another. I cant check file size also connection.

Please help.

Thanks

What have you tried?

Hi,

I have tried below

HOST=''
ftpuser=''
ftppass=''
FILE='XXXX.*'
SrcDir='G:\KKKK.*'
DstDi='/tmp/'
NOW=$(date +"%m-%d-%Y")
ftp -nvd $HOST << EOF >/tmp/ftp.$NOW.log
quote USER $ftpuser 
quote PASS $ftppass
hash
prompt off
cd $DstDi
lcd $SrcDir
bin
mget $FILE
rm -f $FILE
bye
EOF

Thanks

1 Like

So:


# Check if you can connect.
ftp -nvd $HOST <<EOF
quote user $ftpuser
quote pass $ftppass
hash
prompt off
cd $DstDi
ls $FILE
EOF

if [ "$?" -ne 0 ]
then
        echo "Couldn't connect" >&2
        exit
fi

# Get the remote file
ftp -nvd $HOST << EOF >/tmp/ftp.$NOW.log
quote USER $ftpuser 
quote PASS $ftppass
hash
prompt off
cd $DstDi
lcd $SrcDir
bin
get $FILE $FILE.tmp
bye
EOF

if [ "$?" -ne 0 ]
then
        echo "Couldn't retrieve $FILE" >&2
        exit
fi

if [ "$(wc -l "${DstDir}/$FILE")" -ne "$(wc -l "${DstDir}/$FILE.tmp")" ]
then
        echo "Sizes of original and new file differ" >&2
        exit
fi

echo "Retrieved new file into ${DstDir}/$FILE.tmp" >&2

Hi,

What does this portion do? specially "echo "Couldn't connect" >&2" line

if [ "$?" -ne 0 ]
then
        echo "Couldn't connect" >&2
        exit
fi

Thanks

prints "couldn't connect" into file descriptor two, i.e. standard error, the traditional place for human readable error messages.

When you run something in a terminal it traditionally gets two independent connections to the terminal, stdout and stderr. stdout is meant for data, so that if you do command | anothercommand | etc they won't be piping their own error messages into each other, but dumping them direct to the screen for you to read.

1 Like

Hi Corona,

Now i am getting below

220 Microsoft FTP Service
---> SYST
215 Windows_NT
Remote system type is Windows_NT.
---> USER ftpuser
331 Password required for ftpuser.
---> PASS 12345678
230 User logged in.
Hash mark printing on (1024 bytes/hash mark).
Interactive mode off.
---> CWD /tmp/
550 The system cannot find the file specified.
---> TYPE I
200 Type set to I.
---> PASV
227 Entering Passive Mode (192,168,112,1,220,60).
---> RETR XXXX.*
550 The filename, directory name, or volume label syntax is incorrect.
---> QUIT
221 Goodbye.

Remove all indentation before any 'EOF's. Those have to be at the beginning of the line.

command <<EOF
    something
    something
    EOF
command <<EOF
    something
    something
EOF

Please do not edit your posts out when I'm answering them.

Please tell me the values of your variables. (Not the username and password, obviously.)

Where did you get the FTP script you started with?

Hi,

Noted with thanks
Below is the values of the some variables

FILE='XXXX.*' [get multiple file]
SrcDir='D:\TEST\' [Windows PC]
DstDi='/tmp/' [Linux Pc]

Thanks

I didn't realize you were getting multiple files... Your description was that of retrieving a single file.

Where are you keeping the original files on the local server?

Hi,

Yes local server[linux]. everyday file is generated like XXXX.yymmdd format but the directory is same.

Thanks

The same directory? g:\ ? Linux doesn't exactly have that...

Hi

I am going to clear you...File generated in windows pc that is remote[format is XXX.yymmdd]..linux pc is local for me...script run in linux pc..

And where are the originals kept on the Linux server? /tmp/ seems unlikely.

Hi,

Yes..file kept in linux /tmp/ folder.

---------- Post updated at 08:05 PM ---------- Previous update was at 01:33 PM ----------

Hi,

Any update ?

Thanks

---------- Post updated at 08:18 PM ---------- Previous update was at 08:05 PM ----------

If possible please help me...

I am not here 24 hours a day.

Keeping important files in /tmp/ is a horrifying idea... That gets cleaned out automatically. One accidental reboot and they're gone.

If /tmp/ is on the local machine, why are you doing lcd g:\...? I think you have them backwards.

if ! mkdir /tmp/$$
then
        echo "Couldn't create /tmp/$$" >&2
        exit 1
fi

echo "Storing into /tmp/$$" >&2

SrcDir='G:\KKKK.*'
DstDi='/tmp/'

trap "rm -Rf /tmp/$$" EXIT # Clean up temp folder when the script quits by any means

# Check if you can connect.
ftp -nvd $HOST <<EOF
quote user $ftpuser
quote pass $ftppass
hash
prompt off
cd $SrcDir
ls
EOF

if [ "$?" -ne 0 ]
then
        echo "Couldn't connect" >&2
        exit
fi

# Get the remote file
ftp -nvd $HOST << EOF >/tmp/ftp.$NOW.log
quote USER $ftpuser 
quote PASS $ftppass
hash
prompt off
cd $SrcDir
lcd /tmp/$$
bin
mget $FILE
bye
EOF

if [ "$?" -ne 0 ]
then
        echo "Couldn't retrieve $FILE" >&2
        exit
fi

OLDIFS="$IFS" # Special IFS variable controls splitting of unquoted variables
IFS="/"

for FILE in /tmp/$$/*
do
        if [ ! -f "$FILE" ]
        then
                echo "No files retrieved" >&2
                exit 1
        fi

        set -- $FILE        # Splits /path/to/file into $1="", $2="path", $3="to", $4="file"
        shift $(( $# - 1 )) # $1="file", all the rest disappears

        if [ "$(wc -c < "${DstDi}/$1")" -ne "$(wc -c < "/tmp/$$/$1")" ]
        then
                echo "Sizes of original and new file differ" >&2
                exit
        fi
done

IFS="$OLDIFS"

This downloads new files into /tmp/$$/, where $$ is some random number.

Hi,

I am not in ofc now. I will check the below tomorrow and revert back by tomorrow.

Thanks