Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

.ksh ftp scripting help.

Options
  • 22-10-2008 1:42pm
    #1
    Registered Users Posts: 75 ✭✭


    Hey guys,
    Need some help on an ftp script i'm trying to put toghether.

    Basicly the script reads in a date from input, pulls the required files from archive, drops the date/time stamp and then send the files out.

    I want to do some error checking on my ftp command.. I had it set up so that it would dump everything out to a log file and then I would grep that log file for known errors. i.e
     
            ls -rt $SFILE | awk '
                    BEGIN { print "user username pwd"
                            print "binary"
                            print "cd /home/test_files" }
                    {
                            srcfile=$1
                            destfile=$1
                            sub("-[0-9]+$","",destfile)
                            print "put",srcfile,destfile
                    }
                    END { print "bye" }
            '| ftp -nv Servername >> $LOG
    	  err=$?
            if (( 0 != $( grep -i 'Not connected' $LOG | wc -l ) ))  ||
               (( 0 != $( grep -i 'not authorized' $LOG | wc -l ) ))  ||
               (( 0 != $( grep -i 'file access permissions do not allow' $LOG | wc -l ) ))  ||
               (( 0 != $( grep -i 'Login failed' $LOG | wc -l ) ))  ||
               (( 0 != $( grep -i 'Mismatched' $LOG | wc -l ) ))  ||
               (( 0 != $( grep -i 'ABOR' $LOG | wc -l ) ))  ||
               (( 0 != $( grep -i ' No such file' $LOG | wc -l ) )) ||
               (( 0 != $( grep -i 'Allocated to another' $LOG | wc -l ) ))  ||
               (( 0 != $( grep -i 'not found' $LOG | wc -l ) )) then
               echo "Problem with FTP process, `date`" >> $LOG
               echo "Problem with FTP process, files were not sent out"
               echo ""
               echo "Please check $LOG for more information"
               exit 1
            else
    
    However, this isn't very efficient as the grep happens after the ftp process has happened. There's the potential to lock out an account this way. I'd rather catch an invalid pwd in realtime and any other errors during the ftp.
    I'm not very familiar with awk and need some help.

    Can if statements be performed inside the awk command?
    Is it the same syntax as ksh script?
    Any suggestions?


Comments

  • Registered Users Posts: 868 ✭✭✭brianmc


    Jarrath wrote: »
    Hey guys,
    Need some help on an ftp script i'm trying to put toghether.


    If statements, while loops and for loops in awk have the same syntax as their equivalents in C, C++, Java, Perl. So an "if" would be a simple...
    if ( boolean expression ) {
        stuff
    } else {
        other stuff
    }
    

    All code of course must be within code blocks (you seem to have that sussed though).


    Probably a more efficient way to interact with your ftp session would be to run it in the background either as a co-process or automated using named pipes.

    To work with a co-process...
    ftp -nv Servername |&    # This will start the co-process
    
    read -p VARNAME     # This will read a line of output from the co-process
    
    print -p "Stuff you want to send"    # This will print stuff into the standard input of the co-process
    
    

    Problems with co-processes are that you need to be pretty sure of how the dialogue is going to go...

    I.E. If "ftp" prints out three lines of text once started before it lets you enter a command then you must do "read -p" three times before you do the "print -p".

    Ftp though, should be pretty predictable.

    There are other issues with lines that don't end with a newline and so on, but from memory I think you should do ok working with the ftp command.


    Edit:

    Edit to say that this is assuming that it is definitely Korn shell that you are working with. co-processes are Korn Shell specific.


  • Registered Users Posts: 868 ✭✭✭brianmc


    Jarrath wrote: »
    Hey guys,
    Need some help on an ftp script i'm trying to put toghether.

    Just another thought. If the co-processes sound weird and awkward, perhaps try something like the following...
    MKNOD=/usr/sbin/mknod    # Location of your mknod command may vary
    PIPE=/tmp/pipe.$$
    
    ${MKNOD} ${PIPE} p    # Create a named pipe
    
    ftp -nv Servername < ${PIPE} > ${LOG}   # Set ftp reading from the pipe, writing to a Logfile
    
    exec 3>${PIPE}    # Create a connection for writing to the pipe
    
    
    # Now you can write to the ftp command and read from the Logfile...
    # So...
    
    echo "user username pwd" >&3
    sleep 2
    
    if grep -i "Login failed" ${LOG}
    then
        # login failed... react as appropriate.
    fi
    
    echo binary >&3
    
    echo "cd /home/test_files" >&3
    
    if grep -i "some other error" ${LOG}
    then
        # Something else went wrong... etc. etc.
    fi
    
    # blah blah blah
    
    echo "bye" >&3
    
    exec 3>&-      # this closes the connection, although it probably closed when ftp ended.
    
    wait    # This causes the script to wait for any background jobs (ftp) to finish before it goes any further
    
    


Advertisement