Automating FTP from Unix Shell Script

Recently I faced an issue about FTP. I was supposed to ask the name and location of the file as one of the input to the user and get the file at current host where I am running the script. The situation was bit complex. I mean, the inputs from the user were from a GUI tool and that was getting passed to oneĀ  of the shell script. One of the challenge that I faced was FTPing the file from some other server to the current server. Because normal FTP command ask for user ID and password and is interactive.

But with little investigation I was able to get the correct command options for FTP that can be used inside a shell script and its a totally non-interactive version. Here is the FTP code I used.

EMSUI_SOURCE_LOCATION=$1
echo "echo \"open statj11" >> $HOME/ftp.sh
echo "quote USER ora1818" >> $HOME/ftp.sh
echo "quote PASS ORA1818" >> $HOME/ftp.sh
echo "bin" >> $HOME/ftp.sh
echo "get $EMSUI_SOURCE_LOCATION $HOME/dump/GC_AGENT/`basename $EMSUI_SOURCE_LOCATION`" >> $HOME/ftp.sh
echo "close" >> $HOME/ftp.sh
echo "quite\" | ftp -n" >> $HOME/ftp.sh

sh $HOME/ftp.sh

Basically I am connecting to statj11 server as ora1818 user. The password for this user is ORA1818. EMSUI_SOURCE_LOCATION is the variable which has name and location of the file to be FTPed. I am taking the name and location (complete path) of the file to be FTPed from the user. (EMSUI_SOURCE_LOCATION=$1)

`basename $EMSUI_SOURCE_LOCATION` will give you the name of the file to be FTPed, which I am using as the name of my FTPed file.

All the above commands are echoed to a file called $HOME/ftp.sh and then I am executing this script which will FTP the required file.

Hope this helps !!

Advertisement