Solved

how to find if exists and file growth using FTP

Posted on 2011-02-18
6
1,588 Views
Last Modified: 2012-05-11
Hi experts,

I have server 1 (S1), server 2 (S2) (Data resides here)

I should connect from S1 using shell script to S2 using FTP.The follwing conditions should be checked after the connect was made.

1) Check if the required files exists (example abc.txt).If not write a log to S1 that they do not exist.
2) If file exists then check the file size for every 2 mins  to see if its growing.And if its not growing then move file from S2 to S1 and write the log in S1 and exit.

I cant create scripts or any logs in S2.
0
Comment
Question by:vkchaitu82
  • 3
  • 2
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34930341
You didn't give any information as to your environment. What kind of systems are you running on/against? Perhaps python would be a good, simple-to-implement solution for what you seek to do.
0
 

Author Comment

by:vkchaitu82
ID: 34930353
Both are Linux servers and have Korn shell
0
 
LVL 19

Expert Comment

by:simon3270
ID: 34930355
From S1, set the host (S2) you are connecting to, the user and password on S2, and the file name and directory (use "." for the directory if it is in the S2 user's home directory).

Then run the script on S1.  It first checks that the file exists on S2 and exits if it doesn't.  If it does, it monitors the file size very 120 seconds - if the size (in bytes) doesn't change, it retrieves the file and exits.
REMFILE=remote.file
REMDIR=/var/log/adm
REMUSER=u1
REMPASS=secret
REMHOST=S2

getftpdir() {
ftp -n $REMHOST 2>/dev/null <<EOF
user $REMUSER $REMPASS
cd $REMDIR
dir $REMFILE
quit
EOF
}

getftpfile() {
ftp -n $REMHOST 2>/dev/null <<EOF
user $REMUSER $REMPASS
cd $REMDIR
get $REMFILE
quit
EOF
}

# Get first entry to check existence, and initial size
getftpdir > /tmp/dirf.$$

if grep -i "no such file" /tmp/dirf.$$ >/dev/null 2>&1
then
  echo ERROR: File $REMDIR/$REMFILE does not exist on host $REMHOSTA for user $REMUSER
  rm /tmp/dirf.$$
  exit 1
fi

REMSIZE=$(awk '{print $5}' /tmp/dirf.$$)
REMNAME=$(awk '{print $NF}' /tmp/dirf.$$)

if [ "$REMNAME" != "$REMFILE" ]
then
  echo ERROR: File name $REMNAME does not match expected $REMFILE
  rm /tmp/dirf.$$
  exit 1
fi

while true
do
  sleep 120
  getftpdir > /tmp/dirf.$$
  if grep -i "no such file" /tmp/dirf.$$
  then
    echo ERROR: File $REMDIR/$REMFILE on host $REMHOSTA has been deleted while processing 
    rm /tmp/dirf.$$
    exit 1
  fi
  NEWSIZE=$(awk '{print $5}' /tmp/dirf.$$)
  if [ $NEWSIZE = $REMSIZE ]
  then
    # file has not grown in 120 seconds - retrieve it
    break
  fi
  # File has changed size - remember the new size
  REMSIZE=$NEWSIZE
done

getftpfile

rm /tmp/dirf.$$

exit 0

Open in new window

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:vkchaitu82
ID: 34935172
Hi simon3270,

Thanks for the solution. One more question...Is there a way to find get the First file that came in the directory  if there are multiple files present .

Example S2
filename      Creation date
file1.123      1/1/2011
file1.456      1/2/2011

I need the first file as it came first

I tried to use ls -tr in the FTP and taking the output and doing head -1 but its giving error..
0
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
ID: 34937886
What was the error?

I am using one Ubuntu machine to talk to another, and "ls -tr" on its won seems to work fine (produces a list of the files, in date order).  I think the problem you would have is that "ls -tr $REMFILE" doesn't work (it tries to write the list of all files in the S2 directory to a file called $REMFILE on S1).

One option would be to change ftpgetdir to do "ls -tr" instead of "dir $REMFILE", then to search the output for the desired file.  You then get the name for that file using the "REMNAME=$(awk '{print $NF}' /tmp/dirf.$$)" command, and use *that* filename on the "get" line in getftpfile().
0
 

Author Closing Comment

by:vkchaitu82
ID: 34980955
This is perfect and works..Thanks for your time...
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question