[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


I wish to ftp a file from one UNIX server to another UNIX...

<< Back to: Anonymous FTP: Frequently Asked Questions (FAQ) List

Question by Figo Sub
Submitted on 8/20/2003
Related FAQ: Anonymous FTP: Frequently Asked Questions (FAQ) List
Rating: Rate this question: Vote
I wish to ftp a file from one UNIX server to another UNIX server by creating a UNIX script. I require assistance on how to prepare the UNIX script, where, by just running the script at command line on the host server, I want to change directory on host server, ftp to remote server, change directory on remote server and finally transfer the file from the host server to the remote server.


Answer by Madhulika
Submitted on 12/10/2003
Rating:  Rate this answer: Vote
#!/bin/ksh

Email1="myname@compname.com"

#Path of Log file
logdir="/log/ftp"

# Path of ftp executable
ftp="/usr/bin/ftp"

# Give port or mention server name
IP="111.111.111.111"

#User ID to login into remote server
user="ftp"

#Password to login in remote server
pass="ftp"

#Mention the path where the file to FTP is lying in the current server
ifile="/mainfold/level2fold/filetoftp"

#Mention the path where the FILE need to FTPied in the remote server
ofile="xyz/filename"

#Mention type of data transfer "asc" or "bin"
type="asc"
# this will take the name of your file
myname=`basename $0`

#selects the mode
verbose="verbose"

#date of FTP - Current date
dd=`date +%d`

# Creating a log file with datestamp
log="$logdir/$myname.$dd.log"
host=`hostname`
rc=0
boj=`date`

#ftp block starting - all will be written to log file

exec 1>$log 2>&1
echo "---------------------------------------------------"
echo "              Begin FTP Parameters  "
echo "---------------------------------------------------"
echo "Email Sent To        $Email1 "
echo "Destination Machine: $IP"
echo "User ID:             $user"
echo "Password:            ##############"
echo "Destination File:    $ofile"
echo "---------------------------------------------------"
echo "                End FTP Parameters  "
echo "---------------------------------------------------"
echo "                Begin FTP Session  "
echo "---------------------------------------------------"
echo "open $IP
      quote USER $user
      quote PASS $pass
      $verbose
      $type
      put $ifile $ofile
      close
      quit" |$ftp -n
echo "---------------------------------------------------"
echo "                End FTP Session  "
echo "---------------------------------------------------"


# FTP block ends

#Check the log file for FTP status

foo=`grep -i "cannot" $log`
if [ "$?" -eq "0" ] ; then
   rc=1
   status="Destination file does not exist\n"
fi
foo=`grep -i "does not" $log`
if [ "$?" -eq "0" ] ; then
   rc=1
   status="${status}Source file does not exist\n"
fi
foo=`grep -i "killed" $log`
if [ "$?" -eq "0" ] ; then
   rc=1
   status="${status}File transfer process has abended\n"
fi
foo=`grep -i "space" $log`
if [ "$?" -eq "0" ] ; then
   rc=1
   status="${status}Ran out of disk space before completion of copy\n"
fi

if [ "$rc" -eq "0" ] ; then
   status="Successful"
fi

# find out the time to ftp.
eoj=`date`
echo "\nJob start time:   $boj"
echo "Job end time:     $eoj"
echo "\nResult code:      $rc ($status)"

#Mail the status to the email address specified.
mailx -s "FTP results from $myname" $Email1 <$log

exit 0

 

Answer by abdul
Submitted on 8/2/2004
Rating: Not yet rated Rate this answer: Vote
#!/bin/ksh

Email1="myname@compname.com"

#Path of Log file
logdir="/log/ftp"

# Path of ftp executable
ftp="/usr/bin/ftp"

# Give port or mention server name
IP="111.111.111.111"

#User ID to login into remote server
user="ftp"

#Password to login in remote server
pass="ftp"

#Mention the path where the file to FTP is lying in the current server
ifile="/mainfold/level2fold/filetoftp"

#Mention the path where the FILE need to FTPied in the remote server
ofile="xyz/filename"

#Mention type of data transfer "asc" or "bin"
type="asc"
# this will take the name of your file
myname=`basename $0`

#selects the mode
verbose="verbose"

#date of FTP - Current date
dd=`date +%d`

# Creating a log file with datestamp
log="$logdir/$myname.$dd.log"
host=`hostname`
rc=0
boj=`date`

#ftp block starting - all will be written to log file

exec 1>$log 2>&1
echo "---------------------------------------------------"
echo "              Begin FTP Parameters  "
echo "---------------------------------------------------"
echo "Email Sent To        $Email1 "
echo "Destination Machine: $IP"
echo "User ID:             $user"
echo "Password:            xxxxxxxxxxxxx"
echo "Destination File:    $ofile"
echo "---------------------------------------------------"
echo "                End FTP Parameters  "
echo "---------------------------------------------------"
echo "                Begin FTP Session  "
echo "---------------------------------------------------"
echo "open $IP
      quote USER $user
      quote PASS $pass
      $verbose
      $type
      put $ifile $ofile
      close
      quit" |$ftp -n
echo "---------------------------------------------------"
echo "                End FTP Session  "
echo "---------------------------------------------------"


# FTP block ends

#Check the log file for FTP status

foo=`grep -i "cannot" $log`
if [ "$?" -eq "0" ] ; then
   rc=1
   status="Destination file does not exist\n"
fi
foo=`grep -i "does not" $log`
if [ "$?" -eq "0" ] ; then
   rc=1
   status="${status}Source file does not exist\n"
fi
foo=`grep -i "killed" $log`
if [ "$?" -eq "0" ] ; then
   rc=1
   status="${status}File transfer process has abended\n"
fi
foo=`grep -i "space" $log`
if [ "$?" -eq "0" ] ; then
   rc=1
   status="${status}Ran out of disk space before completion of copy\n"
fi

if [ "$rc" -eq "0" ] ; then
   status="Successful"
fi

# find out the time to ftp.
eoj=`date`
echo "\nJob start time:   $boj"
echo "Job end time:     $eoj"
echo "\nResult code:      $rc ($status)"

#Mail the status to the email address specified.
mailx -s "FTP results from $myname" $Email1 <$log

exit 0

 

Answer by LeoInafuku
Submitted on 12/8/2006
Rating: Not yet rated Rate this answer: Vote
Is there a way to keep the FTP commands in a separate variable? Or does it needs to be hard coded for every ftp session that i want to create?

 

Answer by UA-Agent
Submitted on 1/31/2007
Rating: Not yet rated Rate this answer: Vote
Great Work My Friend.
You are Da Best :)
Thanks

 

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

 

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.

 

<< Back to: Anonymous FTP: Frequently Asked Questions (FAQ) List


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.