#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    12
    Rep Power
    0

    FTP using specific multiple directories


    Hi, How can I open the ftp connection 1 time and then get the files from the specified directories from
    my control file, put these to my local server in the specified location from the control file
    and then close the connection at the end?
    my script is opening and closing the connection for each file in the control file and hanging up often.
    Thanks!

    export TERM=vt100
    CurrDate=`date +%Y%m%d%H%M%S`
    Logfile=/home/Log.$CurrDate
    temp="/home//temp"
    controlfile="/home/ftp.ctl"
    newline=\\n
    filepattern="*"
    ipaddress="xxx.xx.xx.xx"
    userid="xxxxxxxx"
    password="xxxxxxxx"
    trap '' 1 2 3 4 5 6 13 15 18 24 29
    if ls "$controlfile" > /dev/null
    then
    for file in `cat $controlfile | cut -d: -f3 `
    do
    localdir=`cat $controlfile | grep "$file" | cut -d: -f2`
    remotedir=`cat $controlfile | grep "$file" | cut -d: -f3`
    tempdir=`cat $controlfile | grep "$file" | cut -d: -f1`

    cd $localdir
    if ping -c 3 $ipaddress > /dev/null
    then
    {
    echo "
    open $ipaddress
    user $userid $password
    cd $remotedir/Pending
    ascii
    dir *.in listfile
    mget *.in
    quit
    "
    } | ftp -i -n

    else
    clear
    rm $temp/filename
    rm $file/filelist
    exit 8
    fi
    if ls "$tempdir/listfile" > /dev/null
    then

    for x in `cat $tempdir/listfile | cut -c 40-100`

    do


    cd $localdir
    ls > filelist1

    d=`date`
    if ! cat filelist1 | grep "$x" > /dev/null
    then
    echo "Please check" $x "is missing or cannot be located ">> $Logfile
    temp=`not_successful`
    else

    echo "Got the file " $x >> $Logfile

    {
    echo "
    open $ipaddress
    user $userid $password
    cd $remotedir/Pending
    rename $x $remotedir/Archive/$x
    quit


    "
    } | ftp -i -n

    fi
    done

    rm $tempdir/listfile
    if ls | grep "filelist1" > /dev/null
    then
    rm $localdir/filelist1
    fi
    fi
    y=`date +%Y%m%d%H%M%S`
    echo "timestamp" $y$newline >> $Logfile
    done
    else
    echo "*********************Control file is missing****************"
    exit 8
    fi


    if temp=`not_successful`
    then
    echo "Please check $Logfile for files not transferred" |mailx -s "FTP ERROR" myemail@yahoo.com
    else
    echo "FTP was completed successfully " >> $Logfile
    echo "FTP was completed successfully "
    fi

    exit
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    Only had a short glance at your code. I am not into reading that much of code in my free time...

    Suggestion: open a named pipe (instead of your unnamed one aka "|") to the ftp server and redesign your loops. then you can supply several commands for one connection.
    eg.:
    Code:
    mknod ... /tmp/ftppipe (look up the syntax in "man mknod")
    ftp -i -n </tmp/ftppipe >/tmp/ftppipe &
    ... add ftp connect and login commands here ...
    for $i in ...; do
      echo "cd $i" >/tmp/ftppipe
      echo "get file.dat" >/tmp/ftppipe
    done
    echo "bye" >/tmp/ftppipe
    rm -f /tmp/ftppipe
    assume this to be some kind of pseudo-code and being untested. i take no responsibility if you delete all your files like this or whatever

IMN logo majestic logo threadwatch logo seochat tools logo