Understanding FTP connection code


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Understanding FTP connection code
# 1  
Old 12-22-2009
Understanding FTP connection code

Hello all,

I am trying to understand the control flow in the below code, my understanding is that echo command is basically used to print certain lines, can we login into the FTP server using echo commands.

Code:
echo "open $HOSTNAME" > $execdir/ftp_CostPriceFIPR.log
echo user $USERID $PASSWORD >> $execdir/ftp_CostPriceFIPR.log
echo "quote site lrecl=1000" >> $execdir/ftp_CostPriceFIPR.log
echo "lcd $srcdir" >> $execdir/ftp_CostPriceFIPR.log

echo "put EODCostPriceToFIPR 'TST.PLCHLDR.COSTPRC.UPLOAD'" >> $execdir/ftp_CostPriceFIPR.log

echo "bye" >> $execdir/ftp_CostPriceFIPR.log

ftp -n -i < $execdir/ftp_CostPriceFIPR.log > $execdir/ftp_CostPriceFIPR_err.log


how it is different from the below code, what is the difference between above ftp command and below one

Code:
ftp -v -n 10.xy.pq.rs << cmd
user xyz_user #$@%@$
cd /u03/app/informatica/7.1.4/server/SrcFiles

Thanks a lot.

Regards,
Ariean
# 2  
Old 12-22-2009
This has an error note the change in red:
Code:
ftp -v -n 10.xy.pq.rs << cmd
user xyz_user #$@%@$
cd /u03/app/informatica/7.1.4/server/SrcFiles
cmd

Both the here document (the code fixed) and the file input are both being read as stdin to the ftp process. Not different there. The file version merely has extra overhead:
opening & closing $execdir/ftp_CostPriceFIPR.log several times. It is also going to be harder to maintain longterm. Unless there is someoverriding reason to use a file, consider the here doc.

another option:
use echo which eliminates the $execdir/ftp_CostPriceFIPR.log as the middleman:

Code:
echo "
  user plpl xxxxxx
  cd somewhere
  get somefile
  put anotherfile
  exit
 " ftp -v -n 10.xy.pq.rs > ftplog

Life is full of choices when write unix code....

Last edited by jim mcnamara; 12-22-2009 at 12:09 PM..
# 3  
Old 12-28-2009
Understanding FTP connection code & process of FTPing

Jim/Hello All,

Thanks for your reply, its very hard for a newbie like me debugging a shell script which is already written, can you please help me understand the below script lines hope you would n't mind since i think it might be a peice of cake for you based on your experience. For reference please check the complete script at the end, I didn't understand the below points as of now where i am stuck.

1) After the function "fn_printLog" is defined the script is reading the filename from the parameter file
Code:
cat $DIR2/ftp_feeds.par | while read LINE

and parsing the variables and calling the function, in the below 2 portions of the code, was this function called 4 times for XTEST=11 and XTEST=12 ?? and i believe when script says
Code:
export XTEST=11

he is declaring and defining the variable at the same time,please correct me if i am wrong.

Code:
export XTEST=11
fn_printLog "Starting"
		      ftp -v -n -i -g<$DIR/ftp_$FEED.log>>$DIR/ftp_tmp.log
fn_printLog "Completed"

Code:
export XTEST=12
fn_printLog "Starting"
		      ftp -v -n -i -g<$DIR/ftp_$FEED.log>>$DIR/ftp_tmp.log
fn_printLog "Completed"

2) What exactly this command is doing here "ftp -v -n -i<$DIR/ftp_$FEED.log" i know this is reading the log file and doing FTP process as per instructions specified in it. what role does these arguments do (-v -n -i) play in this process.

Below is the complete script which i am debugging and understanding to figure out the head and tail of this script. Many Thanks and appreciate your help.

Code:
#!/bin/ksh
# This is a generic FTP script. The input parameter to the script is the Feedname.
# ftp_$FEED.log 
# This file will store the current file ftp information and 
# gets overwritten whenever it reads the new line from the parameter file.
# This file is kept in /product/apps/informatica/v7/pc/ScriptLogs
# ###############################################################################################
#
#
# ftp_$FEED_list.log
# Stores all the ftp file information. Before the program is run, if there is already a file 
# then the file is moved to the same filename (overwritten), previously the logs were moved into
# /product/apps/informatica/v7/pc/ScriptLogs/ArchiveScriptLogs with the date and time appended.
# over a period of time, as these logs are not being used the code has been commented (see below) 
# This file is kept in /product/apps/informatica/v7/pc/ScriptLogs
##################################################################################################
#
#
# ftp_$FEED_err.log
# Stores all the ftp error output information. Before the program is run, if there is already a file 
# then the file is moved to the same filename (overwritten), previously the logs were moved into
# /product/apps/informatica/v7/pc/ScriptLogs/ArchiveScriptLogs with the date and time appended.
# over a period of time, as these logs are not being used the code has been commented (see below)
# This file is kept in /product/apps/informatica/v7/pc/ScriptLogs
####################################################################################################
# Only to Handle Corporate_Action ( xcitek.txt rename)
Corporate_Action_Handler()
{
	set -vx
	echo "XTEST=21"
	if [ -f $SOURCE/$FNAME ] ; then
		mv $SOURCE/$FNAME $SOURCE/xcitek.txt
	fi
}

# main 

if [ $# -ne 1 ] ; then
   echo "Usage : generic_ftp.sh Feedname"
   echo "example : generic_ftp.sh Genesis"
   exit 1
fi
set -x
FEED=$1
DIR=/product/apps/informatica/v7/pc/ScriptLogs
#DIR1=/product/apps/informatica/v7/pc/ScriptLogs/ArchiveScriptLogs(Commented --- Jyothi Jakkaraju 04/14/2004)
DIR2=/product/apps/informatica/v7/pc/ExtProc

export XTEST=99
export DATE=`date +'%Y%m%d-%H%M%S'`
export FTP_OUT_FILE=$DIR/debug_logs/ftp.$FEED.$DATE.ftp_out
export FTP_DEBUG_FILE=$DIR2/ftp_debug.txt
function fn_printLog
{
    set -vx
    typeset msg_str=$1
    if [ ! -f "$FTP_DEBUG_FILE" ] ; then
        return 0
    elif ! `echo "$FTP_OUT_FILE" | egrep -si "$FEED"` ; then
        return 0
    fi

    #echo "$msg_str at `date +'%Y%m%d-%H%M%S':` " >> $FTP_OUT_FILE
    echo "--------------------------------------------------" >> $FTP_OUT_FILE
    echo "`date +'%Y%m%d-%H%M%S'` $msg_str $FEED: (XTEST='$XTEST')  " >> $FTP_OUT_FILE
    echo "--------------------------------------------------" >> $FTP_OUT_FILE

	case $msg_str in
        'Starting' )  cat $DIR/ftp_$FEED.log >> $FTP_OUT_FILE ;;
        'Completed' ) cat $DIR/ftp_tmp.log >> $FTP_OUT_FILE ;;
        'End' )       ;;
    esac
    return 0

	if [ -f "$DIR/ftp_tmp.log" ] ; then
        cat $DIR/ftp_tmp.log >> $FTP_OUT_FILE
	elif [ -f "$DIR/ftp_$FEED.log" ] ; then
        cat $DIR/ftp_$FEED.log >> $FTP_OUT_FILE
	fi
}

# Below code : to remove the logs if they exist, so that fresh log exists each time
#
if [ -f $DIR/ftp_$FEED"_err".log ] ; then
      rm -f $DIR/ftp_$FEED"_err".log
fi

if [ -f $DIR/ftp_list_$FEED.log ] ; then
     rm -f $DIR/ftp_list_$FEED.log
fi

cat $DIR2/ftp_feeds.par | while read LINE
do
   #get the feedname
    FEEDNAME="`echo $LINE | awk -F: '{print $1}'`"
    if [ $FEED = $FEEDNAME ] ; then
        FILE="`echo $LINE | awk -F: '{print $2}'`"
        SOURCE="`echo $LINE | awk -F: '{print $3}'`"
        HOSTNAME="`echo $LINE | awk -F: '{print $4}'`"
        USERID="`echo $LINE | awk -F: '{print $5}'`"
        PASSWORD="`echo $LINE | awk -F: '{print $6}'`"
        FTP_FOLDER="`echo $LINE | awk -F: '{print $7}'`"
        COSTCENTER="`echo $LINE | awk -F: '{print $8}'`"
	PUT_GET="`echo $LINE | awk -F: '{print $9}'`" 
        TRANSFER_MODE="`echo $LINE | awk -F: '{print $10}'`"
       # echo $TRANSFER_MODE


        # if the feed has Cost Center then Append the Cost Center Information 
        if [ "$COSTCENTER" ] ; then

           UID1=`echo $USERID | awk -F# '{print $1}'`
           echo $UID1
           UID2=`echo $USERID | awk -F# '{print $2}'`
           echo $UID2
           echo ${UID1}'\\''\\'${UID2}

           FILENAMED=`ls -tr  $SOURCE/${FILE}${COSTCENTER}*D | tail -1`
           FILENAMES=`ls -tr  $SOURCE/${FILE}${COSTCENTER}*S | tail -1`
           echo $FILENAMED
           echo $FILENAMES
           FNAMED=`basename $FILENAMED`
           FNAMES=`basename $FILENAMES`
           
		   # echo $FNAMED 
		   # echo $FNAMES

		   # open connect to the target host      
  		    echo "open $HOSTNAME" > $DIR/ftp_$FEED.log
          if  [ $UID2 ] ; then
          # connect to the target host  with the userid and password
           echo user ${UID1}'\\''\\'${UID2} $PASSWORD >>  $DIR/ftp_$FEED.log
          else
           # connect to the target host with the userid (UID1) and password
             echo user ${UID1} $PASSWORD >> $DIR/ftp_$FEED.log
          fi


          if [ "$FTP_FOLDER" ] ; then
             # change directory to target directory
             echo "cd $FTP_FOLDER" >> $DIR/ftp_$FEED.log
          fi

          # change directory to local source directory
            echo "lcd $SOURCE" >> $DIR/ftp_$FEED.log

          # PUT_GET the file
            echo "$PUT_GET $FNAMED" >> $DIR/ftp_$FEED.log
            echo "$PUT_GET $FNAMES" >> $DIR/ftp_$FEED.log

          # after the ftp transfer quit
            echo "bye" >> $DIR/ftp_$FEED.log

            cat $DIR/ftp_$FEED.log  >> $DIR/ftp_list_$FEED.log   

		    # execute the ftp command
		    ###ftp -n -i -g<$DIR/ftp_$FEED.log>>$DIR/ftp_$FEED"_err".log      
                    count=1
export XTEST=11
fn_printLog "Starting"
		      ftp -v -n -i -g<$DIR/ftp_$FEED.log>>$DIR/ftp_tmp.log
fn_printLog "Completed"
		      result=$(egrep -ic "Login failed|No such file or directory|Lost connection|Not connected|Permission denied" $DIR/ftp_tmp.log)
		    if [ $result = 0 ];
		    then
		    		###No errors write to err.log file and delete ftp_tmp file
		      		cat $DIR/ftp_tmp.log >>$DIR/ftp_$FEED"_err".log
			        rm $DIR/ftp_tmp.log
		    else
		    while [ $count -le 5 ]
		      do
		      sleep 10
	              rm $DIR/ftp_tmp.log
export XTEST=12
fn_printLog "Starting"
		      ftp -v -n -i -g<$DIR/ftp_$FEED.log>>$DIR/ftp_tmp.log
fn_printLog "Completed"
		      result=$(egrep -ic "Login failed|No such file or directory|Lost connection|Not connected|Permission denied" $DIR/ftp_tmp.log)
		      count=`expr $count+1`
		      if [ $result = 0 ];
		      then
		      		cat $DIR/ftp_tmp.log >>$DIR/ftp_$FEED"_err".log
			        rm $DIR/ftp_tmp.log
				count=6
                      fi

		      if [ $count -ge 5 ];
		      then 
		      		cat $DIR/ftp_tmp.log >>$DIR/ftp_$FEED"_err".log
			        rm $DIR/ftp_tmp.log
				count=6
		      fi
		      done
		    fi
        else

             echo userid = $USERID
         # Checking if the userid has backslash in it 
           	UID1=`echo $USERID | awk -F# '{print $1}'`
                echo UID1 = $UID1
                UID2=`echo $USERID | awk -F# '{print $2}'`
                echo UID2 = $UID2
                echo UID = ${UID1}'\\''\\'${UID2}

	 # open connect to the target host      
  	        echo "open $HOSTNAME" > $DIR/ftp_$FEED.log

         # connect to the target host  with the userid and password
         #  echo "user $USERID $PASSWORD" >> $DIR/ftp_$FEED.log
             
          if  [ $UID2 ] ; then
          # connect to the target host  with the userid and password
           echo user ${UID1}'\\''\\'${UID2} $PASSWORD >>  $DIR/ftp_$FEED.log
	    elif [ $FEEDNAME != "Corporate_Action" ]; then
           # connect to the target host with the userid (UID1) and password
             echo user ${UID1} $PASSWORD >> $DIR/ftp_$FEED.log
          fi

                 if [ $TRANSFER_MODE =  "B"  ] ; then
                    echo "binary" >> $DIR/ftp_$FEED.log
                 else
                    echo "ascii" >> $DIR/ftp_$FEED.log
                  fi 

          # get baase name  and check if file exist only for PUT 
		if [ $PUT_GET == "put" ]; then
			if [ $FEEDNAME = "Anvil_Impact" ]; then
			 FILENAME=`ls -tr $SOURCE/${FILE} | tail -1`
                        else 
			 FILENAME=`ls -tr $SOURCE/${FILE}* | tail -1`
			fi
			 FNAME=`basename $FILENAME`
		elif [ $PUT_GET == "mput" ]; then 
			 FNAME="${FILE}"
			 echo "1=$FNAME"
			 #FILENAME=`$SOURCE/${FILE}*`
			 #FNAME=`basename $FILENAME`
		else
			 FNAME=$FILE
		fi	   
			echo $FNAME          

          if [ "$FTP_FOLDER" ] ; then
             # change directory to target directory
             echo "cd $FTP_FOLDER" >> $DIR/ftp_$FEED.log
          fi
         
          # change directory to local source directory
            echo "lcd $SOURCE" >> $DIR/ftp_$FEED.log
        
          # PUT_GET the file
            echo "$PUT_GET $FNAME" >> $DIR/ftp_$FEED.log
        
          # after the ftp transfer quit
            echo "bye" >> $DIR/ftp_$FEED.log
        
           cat $DIR/ftp_$FEED.log  >> $DIR/ftp_list_$FEED.log
           
	   # execute the ftp command
           if [ $PUT_GET == "mput" ]; then
		    ###ftp -n -i<$DIR/ftp_$FEED.log>>$DIR/ftp_$FEED"_err".log      
                    count=1
export XTEST=13
fn_printLog "Starting"
		      ftp -v -n -i<$DIR/ftp_$FEED.log>>$DIR/ftp_tmp.log
fn_printLog "Completed"
		      result=$(egrep -ic "Login failed|No such file or directory|Lost connection|Not connected|Permission denied" $DIR/ftp_tmp.log)
		    if [ $result = 0 ];
		    then
		    		###No errors write to err.log file and delete ftp_tmp file
		      		cat $DIR/ftp_tmp.log >>$DIR/ftp_$FEED"_err".log
			        rm $DIR/ftp_tmp.log
		    else
		    while [ $count -le 5 ]
		      do
		      sleep 10
	              rm $DIR/ftp_tmp.log
export XTEST=14
fn_printLog "Starting"
		      ftp -v -n -i<$DIR/ftp_$FEED.log>>$DIR/ftp_tmp.log
fn_printLog "Completed"
		      result=$(egrep -ic "Login failed|No such file or directory|Lost connection|Not connected|Permission denied" $DIR/ftp_tmp.log)
		      count=`expr $count+1`
		      if [ $result = 0 ];
		      then
		      		cat $DIR/ftp_tmp.log >>$DIR/ftp_$FEED"_err".log
			        rm $DIR/ftp_tmp.log
				count=6
                      fi

		      if [ $count -ge 5 ];
		      then 
		      		cat $DIR/ftp_tmp.log >>$DIR/ftp_$FEED"_err".log
			        rm $DIR/ftp_tmp.log
				count=6
		      fi
		      done
		    fi
		    elif [ $FEEDNAME == "Corporate_Action" ]; then
		    			
                    count=1
		      ftp  -i -g<$DIR/ftp_$FEED.log>>$DIR/ftp_tmp.log
		      result=$(egrep -ic "Login failed|No such file or directory|Lost connection|Not connected|Permission denied" $DIR/ftp_tmp.log)
		    if [ $result = 0 ];
		    then
		    		###No errors write to err.log file and delete ftp_tmp file
		      		cat $DIR/ftp_tmp.log >>$DIR/ftp_$FEED"_err".log
			        rm $DIR/ftp_tmp.log
		    else
		    while [ $count -le 5 ]
		      do
		      sleep 10
	              rm $DIR/ftp_tmp.log
		      ftp  -i -g<$DIR/ftp_$FEED.log>>$DIR/ftp_tmp.log
		      result=$(egrep -ic "Login failed|No such file or directory|Lost connection|Not connected|Permission denied" $DIR/ftp_tmp.log)
		      count=`expr $count+1`
		      if [ $result = 0 ];
		      then
		      		cat $DIR/ftp_tmp.log >>$DIR/ftp_$FEED"_err".log
			        rm $DIR/ftp_tmp.log
				count=6
                      fi

		      if [ $count -ge 5 ];
		      then 
		      		cat $DIR/ftp_tmp.log >>$DIR/ftp_$FEED"_err".log
			        rm $DIR/ftp_tmp.log
				count=6
		      fi
		      done
		    fi
           else
		    ###ftp -n -i -g<$DIR/ftp_$FEED.log>>$DIR/ftp_$FEED"_err".log      
                    count=1
		    if [ $PUT_GET == "mget" ]; 
	            then 
export XTEST=15
fn_printLog "Starting"
		      ftp -v -n -i <$DIR/ftp_$FEED.log>$DIR/ftp_tmp.log
fn_printLog "Completed"
		    else
export XTEST=16
fn_printLog "Starting"
		      ftp -v -n -i -g<$DIR/ftp_$FEED.log>>$DIR/ftp_tmp.log
fn_printLog "Completed"
		    fi
		    result=$(egrep -ic "Login failed|No such file or directory|Lost connection|Not connected|Permission denied" $DIR/ftp_tmp.log)
		    if [ $result = 0 ];
		    then
		    		###No errors write to err.log file and delete ftp_tmp file
		      		cat $DIR/ftp_tmp.log >>$DIR/ftp_$FEED"_err".log
			        rm $DIR/ftp_tmp.log
		    else
		    while [ $count -le 5 ]
		      do
		      sleep 10
	              rm $DIR/ftp_tmp.log
export XTEST=17
fn_printLog "Starting"
		      ftp -v -n -i -g<$DIR/ftp_$FEED.log>>$DIR/ftp_tmp.log
fn_printLog "Completed"
		      result=$(egrep -ic "Login failed|No such file or directory|Lost connection|Not connected|Permission denied" $DIR/ftp_tmp.log)
		      count=`expr $count+1`
		      if [ $result = 0 ];
		      then
		      		cat $DIR/ftp_tmp.log >>$DIR/ftp_$FEED"_err".log
			        rm $DIR/ftp_tmp.log
				count=6
                      fi

		      if [ $count -ge 5 ];
		      then 
		      		cat $DIR/ftp_tmp.log >>$DIR/ftp_$FEED"_err".log
			        rm $DIR/ftp_tmp.log
				count=6
		      fi
		      done
		    fi
           fi
# Only for Corporate_Action
		if [ $FEED == "Corporate_Action" ]; then
		   Corporate_Action_Handler;
		fi
# -----------------------------------------------------------

        fi
    fi
done
fn_printLog "End"

Thanks,
Ariean
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

FTP Connection die out

Hi, I will ftp aroung 80 files after connecting to an FTP Server. But after 2 minutes of connection, it is timed out and connection is dying. Server had a 2 minute connection timeout if connection is idle. But my question, Isn't tranfering files not considered as an activity. Is the connection... (7 Replies)
Discussion started by: vasuarjula
7 Replies

2. Solaris

Solaris 10 ftp connection problem (connection refused, connection timed out)

Hi everyone, I am hoping anyone of you could help me in this weird problem we have in 1 of our Solaris 10 servers. Lately, we have been having some ftp problems in this server. Though it can ping any server within the network, it seems that it can only ftp to a select few. For most servers, the... (4 Replies)
Discussion started by: labdakos
4 Replies

3. HP-UX

ftp first connection closed

Hi, Have anyone seen this problem, there is one remote side where their ftp connection to our server will always fail with connection closed by remote host and the second connection will be working. is this an OS issue or network issue? Thanks Robert (4 Replies)
Discussion started by: robertngo
4 Replies

4. Shell Programming and Scripting

FTP connection problem

I am getting the error in this command when i am pitting it in shell script ftp -nvi jcmpcor1.abc.com >/work/test98.dat << EOD error:- invalid command. but when i am writting it from outside it work fine. (7 Replies)
Discussion started by: ravi214u
7 Replies

5. UNIX for Dummies Questions & Answers

FTP Connection

Hi, Anyone encounter whereby when you FTP from an Unix server (Solaris 8) to another server (Window Server 2003), you tend to wait a long while when you do a "ls" and you get a timeout after that. However, when you FTP again and do a "ls", the result of "ls" shown immediately. All connection are... (5 Replies)
Discussion started by: ahlude
5 Replies

6. Solaris

I want to hard code username and password for an FTP connection

Hi all i want to do FTP via running a shell script and i want to hard code username and password for that particular server.. by writing it in a file can u help me in this regard.. Thank u Naree (2 Replies)
Discussion started by: naree
2 Replies

7. UNIX for Dummies Questions & Answers

ftp - Connection close

Hi Can someone help me what is the problem, when i try to login via ftp, though i entered the correct password, i got an error message Connection refused. please help. thanks (3 Replies)
Discussion started by: kaibiganmi
3 Replies

8. SCO

ftp connection

I have problem with ftp connection on SCO UNIX 5.0.7, it is work slowly. How I can make accelerate. (6 Replies)
Discussion started by: draganmi
6 Replies

9. Solaris

ftp connection problem

Hi all, i am not able to connect the ftp through userA. Can you please tell me what are the possible ways to troubleshoot this issue? Regards krishna (2 Replies)
Discussion started by: krishna176
2 Replies

10. Cybersecurity

FTP Connection

I just started a new job and inherited a Fedora core 3 server. I wanted to open the ftp ports and I wanted to verify this code before I mess things up. From my reading the two lines below should allow ftp access can anyone confirm my attempt. iptables -A INPUT -p tcp - sport 21 -m state - state... (0 Replies)
Discussion started by: mungaz
0 Replies
Login or Register to Ask a Question