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.
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.
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.
# 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
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.
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.
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.