Home > Linux, Shell > Automation (1)

Automation (1)

Programmers should always try to be as lazy as they can. This post will explain several tricks used to retrieve automatically some password protected zipped file on a ftp server in an automated way.

Connection to the ftp server

Basically, the difficulty will be to establih a connection without entering the password.  The thing is just to write (or modifiy) a parameter file called .netrc in your home directory.

This file is a simple text file protected by permissions  set to 600 (chmod 600 .netrc).

It will contain for each (ftp) server you will be connected to, three lines :

  1. a name of the server (or ip address)
  2. a login
  3. a password

Once your .netrc is set, in order to connect to the server myftp.server.com, you ‘ll only have to write the following command in a terminal.

ftp myftp.server.com

And you ‘ll be automatically connected !

Retrieving the data

In an interactive ftp session , you would have to write basic command on the server to change directory and get data. In an automated script, you’ll have to write all this command in a text file and pass the later to ftp client.

So if we must move to the directory Mydir1 and get the file “file1”, we’ll have to create a file with the following instructions :

cat >ftp.in<<EOF
 open myftp.server.com
 cd Mydir1
 get file1.zip
 EOF

and then execute :

ftp <ftp.in

Or in a more straight forward way :

ftp -i<<EOF
 open myftp.server.com
 cd Mydir1
 get file1.zip
 EOF

Possible difficulties :

If we know that the name of the directory will change, let’s say Mydir1 can change to Mydir2, we may use in our script before submission a variable.

If the file name change is unpredictable (let says there is a suffixe giving the creation date in ddmmyyhhmmss) you’ll have no other choice than using mget instead of get. mget accept to retrieve file matching a pattern expressed with meta caracters.

Our script will become :

cat >ftp <<EOF
open myftp.server.com
cd Mydir$num
mget file*.zip
EOF

Unzipping

Now that the file is downloaded, we’ll have to unzip it, which can been done with the following command :

for fic in  file*.zip
do
unzip -P "PassWord" $fic
done

Putting all the later instructions in a shell script file allow me to spare some time (unfortunately this time has also been automaticaly used to work on another task…poor me !)

Advertisements
Categories: Linux, Shell
  1. AL
    December 19, 2011 at 10:38 am
  2. December 19, 2011 at 10:43 am

    😉 Touché !

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: