Help with Shell script for FTP

 
#!/usr/bin/ksh
export filename=/grid/PowerCenter/inbound/AT/filelist.txt
export SOURCE_DIR=/grid/PowerCenter/inbound/AT
export ICOMS_FTP_TGT_DIR1=/dw/input/ATU/ICOM_SERV1
export ICOMS_FTP_TGT_DIR2=/dw/input/AT/ICOM_SERV2
export FILE_MASK="ATRPU_RP_ATU"
echo "start"
ftp_data_file()
{
 
   # local parameters
   local FTP_FUNCTION=$1
   local FILE_NAME=$2
   local FTP_SERVER=$3           # remote delivery server
   local FTP_USER=$4    # ftp user name
   local FTP_PASS=$5     # ftp user pwd
   local FTP_TARGET_DIR=$6   # local target file directory
 
 # local variables
   local SOURCE_DIR=${SOURCE_DIR}   
 
 echo "FTPing the file from the server"
   # ftp file from remote server to local informatica server
 
 ftp -i -nv host <<EOF
 user ${FTP_USER} pass ${FTP_PASS}
 cd $FTP_TARGET_DIR
 ascii
 $FTP_FUNCTION $FILE_NAME
 bye
 EOF
   co_error_check $? "Failed to ${FTP_FUNCTION} file ${SOURCE_DIR}/${FILE_NAME} to ${FTP_TARGET_DIR}"   
}
# main function
echo "main before while"
while IFS="," read ipaddress username password
do
# ftp file from remote server to local server
 cd ${SOURCE_DIR}
 echo "ip address is ${ipaddress}"
 echo "username is ${username}"
 echo "password is ${password}"
 echo "ICOMS_FTP_TGT_DIR1 is ${ICOMS_FTP_TGT_DIR1}"
 echo "ICOMS_FTP_TGT_DIR2 is ${ICOMS_FTP_TGT_DIR2}"
 
ftp_data_file mput ${FILE_MASK}*.csv ${ipaddress} ${username} ${password} ${ICOMS_FTP_TGT_DIR1}
ftp_data_file mput ${FILE_MASK}*.csv ${ipaddress} ${username} ${password} ${ICOMS_FTP_TGT_DIR2}
done <$filename

It's really hard to script FTP in the shell. It's really easy to script FTP in Perl: Perl Net::FTP

#!/bin.ksh

function Init {
   comd="put myfile yourfile"
}

function FTP {

typeset host=$1
typeset acct=$2
typeset pass=$3
typeset comd=$4

ftp -v -n -d<<!
open ${host}
user root ${pass}
bin
hash
${comd}
bye
!

}

function Main {
   Init
   while read line; do
      host=$(echo "${line}" | cut -d, -f1,1)
      acct=$(echo "${line}" | cut -d, -f2,2)
      pass=$(echo "${line}" | cut -d, -f3,3)
      FTP ${host} ${acct} ${pass} "${comd}"
   done < filelist.txt
}
################################################################################
## main processing segment
################################################################################
Main

I have zero knowledge in perl. And the scripts i have to be implemented in Unix Shell

Thanks for the reply

---------- Post updated at 09:48 AM ---------- Previous update was at 09:47 AM ----------

Thanks Tinwalrus,

I will try it now and let u know.

There appear to be issues in the script with passing "" in a parameter. You need ftp to expand the "" not shell.

ftp_data_file mput ${FILE_MASK}\.csv ${ipaddress} ${username} ${password} ${ICOMS_FTP_TGT_DIR1}
ftp_data_file mput ${FILE_MASK}\
.csv ${ipaddress} ${username} ${password} ${ICOMS_FTP_TGT_DIR2}

Within the function:

local FILE_NAME="$2"

$FTP_FUNCTION "$FILE_NAME"

Just a visual check. Untested. There may be more.

#!/usr/bin/ksh
export filename=/grid/PowerCenter/AT/filelist.txt
export SOURCE_DIR=/grid/PowerCenter/AT
export ICOMS_FTP_TGT_DIR1=/dwinput/AT/ICOM_SERV1
export ICOMS_FTP_TGT_DIR2=/dwinput/AT/ICOM_SERV2
export FILE_MASK="ATRPU_RP_ATU"

function Init {
comd="mput ${FILE_MASK}*.csv ${ICOMS_FTP_TGT_DIR1}"
}

function FTP {

typeset host=$1
typeset acct=$2
typeset pass=$3
typeset comd=$4


local SOURCE_DIR=${SOURCE_DIR}
   
ftp -v -n -d<<!
open ${host}
user root ${pass}
bin
hash
${comd}
bye
!

}

function Main {
Init
while read line; do
host=$(echo "${line}" | cut -d, -f1,1)
acct=$(echo "${line}" | cut -d, -f2,2)
pass=$(echo "${line}" | cut -d, -f3,3)
FTP ${host} ${acct} ${pass} "${comd}"
done < $filename
}
################################################################################
## main processing segment
################################################################################
Main

---------- Post updated at 10:35 AM ---------- Previous update was at 10:21 AM ----------

function Init {
comd="mput ${SOURCE_DIR}/${FILE_MASK}*.csv ${ICOMS_FTP_TGT_DIR1}"
}

---------- Post updated at 10:49 AM ---------- Previous update was at 10:35 AM ----------

---------- Post updated at 10:54 AM ---------- Previous update was at 10:49 AM ----------

function Init {
comd="mput ${SOURCE_DIR}/${FILE_MASK}*.csv ${ICOMS_FTP_TGT_DIR1}"
}

function FTP {

typeset host=$1
typeset acct=$2
typeset pass=$3
typeset comd=$4


local SOURCE_DIR=${SOURCE_DIR}
  
cd ${SOURCE_DIR}

ftp -v -n -d<<!
open ${host}
user root ${pass}
bin
hash
${comd}
bye
!

}

function Main {
Init
while IFS="," read line; do
host=$(echo "${line}" | cut -d, -f1,1)
acct=$(echo "${line}" | cut -d, -f2,2)
pass=$(echo "${line}" | cut -d, -f3,3)
FTP ${host} ${acct} ${pass} "${comd}"
done < $filename
}
################################################################################
## main processing segment
################################################################################
Main

---------- Post updated at 11:05 AM ---------- Previous update was at 10:54 AM ----------

Hi Tinwalrus,

Can you help me where i am doing wrong in code.

Thanks

As others have pointed out, the ftp command is not designed for scripting. Use scp if you can. Otherwise use the ncftp set of commands.

There's no need for an external command (cut):

Main() {
Init
while IFS="," read host acct pass
do
  FTP "$host" "$acct" "$pass" "$comd"
done < $filename
}

Connection to account "root" appears to be failing.

What messages to you get from the command line with:

ftp -v -n
open host_name
user root root_password

Where:

host_name = name of your host computer (please do not tell us the real host name)

root_password = password to your root account (please do not tell us the real password)

function Main {
Init
while IFS="," read host acct pass
do
  FTP "$host" "$acct" "$pass" "$comd"
done < $filename
}

---------- Post updated at 05:32 AM ---------- Previous update was at 04:01 AM ----------

The function "FTP" in the script posted in post 6 has hard coded login as root.

Probably should be:

user ${acct} ${pass}

man .netrc maybe?

From this post.

If you have privilege to create executable scripts in your home directory you have enough privilege to create a .netrc file in your home directory. A .netrc file must be permissions 600.

To see dot files:
ls -la
Or just the dot files:
ls -la .??*

i think ftp works fine within a script, i use it all the time (if scp is not available).

if you are going to use mput, you will also need to put 'prompt' in the ftp section to turn off prompting

and methyl suggested, good to open ftp by itself (which is what the script does), do the same steps the script is

enter 'ftp'
enter 'open whatever_the_host_is'
enter 'user root whatever_password_is'

see what happens. setting up another user for the ftp account isnt a bad idea either

Hi

I have created .netrc file and given permissions to it as chmod 600 .netrc
As it is a . file, not able to see it on the directory(fine).

Please tell me now. How to use that file in my FTP Script as this file should be replaced with my filelist.txt

.netrc looks like below :

machine 192.168.100.1 login userid password userpass
machine 192.168.100.1 login userid password userpass