Wednesday, February 17, 2010

FTP automation on Windows and Unix

Automation is one thing that I like the most, it makes my job easier on a daily basis - makes you sit back and relax and sprawl whilst a critical task is being completed by the script but in the long run the same script would be more than sufficient to get me fired out of my Organization since every thing is taken care by the machine against man - let me not sound more or less like a Cyborg from a James Cameron's famous movie ;-).


Here are two scripts that would enlable you to automate an FTP process from a source host to a destination.



###############
## U N I X  ###
###############

##########################
### FTP_Automation.ksh ###
##########################
#!/usr/bin/ksh

if [ "$#" -ne 3 ]
then
echo " USAGE: "
echo " ------ "
echo "\n"
echo " ksh FTP_Automation.ksh TARGET_HOST_OR_IP PASSWORD_OF_USERNAME USERNAME"
echo "\n"
echo " Examples : "
echo " ---------- "
echo "\n"
echo " ksh FTP_Automation.ksh unix_host_2 manager oracle"
echo "\n"
echo " ksh FTP_Automation.ksh 10.10.11.12 manager oracle"
echo "\n"
exit 1
fi

export target_host=$1
export username=$3
export password=2
ftp -niv << E_O_F
open $target_host
user $username $password
hash
bin
cd $HOME/unix2
put $HOME/unix1/file1
put $HOME/unix1/file2
put $HOME/unix1/file3
bye
E_O_F


#### The files are being copied from $HOME/unix1 directory
#### on unix_host_1 host (server) to $HOME/unix2 directory
#### on unix_host_2 host (server).


#### The files will be copied to $HOME/unix2 directory
#### Please change the locations and the file names in the 
#### script if you want the files to be copied to some other 
#### location.


###########
## E N D ##
###########




unix_host_1:$> ksh FTP_Automation.ksh 10.10.11.12 pass root





#####################
## W I N D O W S  ###
#####################

##########################
### FTP_Automation.txt ###
##########################

user user_name
pass_word
bin
hash
put "C:\Documents and Settings\oracle_and_unix\Desktop\file1.txt"
put "C:\Documents and Settings\oracle_and_unix\Desktop\file2.txt"
bye

#########
# E N D #
#########




##########################
### FTP_Automation.bat ###
##########################

ftp -n -s:C:\Documents and Settings\oracle_and_unix\Desktop\FTP_Automation.txt 10.10.11.12

#########
# E N D #
#########


The batch file FTP_Automation.bat implicitly calls the FTP_Automation.txt, the batch file (FTP_Automation.bat) can be scheduled in Scheduled Tasks of 'Control Panel' or simply run from the command prompt or even a double click on the batch file would inititate the ftp copy of files.

2 comments:

Anonymous said...

thanks it helped me a lot

Anonymous said...

thanks it helped me a lot