Solved

how to find if exists and file growth using FTP

Posted on 2011-02-18
6
1,605 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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.
Part One of the two-part Q&A series with MalwareTech.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

617 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