0 and 1’s

remsh and ftp – Automate copying files between machines

Posted in unix by Rama Krishna on January 12, 2009

Update on 26′th Aug 2009.

Just use rcp or scp

Ignore the rest of the blog article.

Consider a scenario where you need to get files from multiple remote machine to your machine and do a task like comparing or auditing, etc, the way it is done is is to do a remote login to a machine and do a ftp for copying a file from the remote machine to your machine

rlogin <remote-machine-name> -l <user>

ftp <my-machine-name>

....
...

and then doing the actual task. It is highly repetitive task to login to different machines and ftping the files. Also, as far as i know automating to do a remote login and ftping the file is impossible using shell script. Please let me know if there is a way to do it via scripts.

I have found a better way to do it using remsh command and using automated ftp script.

Consider the following scenario. There are two unix boxes. One, machine001 and another machine002. For machine machine001, you have direct access using m001username and m001password and to machine002 you do a remote login from machine001 using the user id m002username.

Suppose, you need to get a file file001 from machine002 to machine001.

We do it in following way.

machine001>rlogin machine002 -l m002username
blah blah messages
machine002>ftp machine001
>username m001username
>password ************
>put file001 dir001/file001
>bye
machine002>exit
machine001>ls dir001
file001

Doing the same using ftp and remsh is explained below. The automated ftp script is as follows. It accepts target machine name, username, password, the source file and destination file. This is a simple shell script whose functionality can be greatly extended.

automateFtp.sh

#!/bin/ksh
numOfArgs=$#
if [[ $numOfArgs = "5" ]]
then
        machineName=$1
        username=$2
        password=$3
        sourcefile=$4
        destFile=$5
#Start the ftp process to sent the file
        ftp -n -v ${machineName} <<-ENDTAG
        user ${username} ${password}
        put ${sourcefile} ${destFile}
        quit
ENDTAG
#End of ftp
else
        echo "Insufficient number of arguments"
fi

Just copy this file to the machine whose file needs be copied over to your machine and invoke the remote script using the remsh command.

In the above scenario, the command would be

remsh machine002 -l m002username automateFtp.sh machine001 m001username m001password
file001 dir001/file001

i.e., generally

remsh <from_machine> -l <from_machine_login_name> automateFtp.sh <to_machine>
<to_machine_username> <to_machine_password> <from_machine_file_name><to_machine_file_name>

Since the shell script is a simple one, it can be modified to try out in various scenarios.

This helps in automation to great extent. Just copy automateFtp.sh to each of the machines and invoke them from your machine. Now the task is just reduced to invoking a command. This can also be automated.
Since you pass the username and password at execution time, they are safe from prying eyes.

Reblog this post [with Zemanta]
Tagged with: , , ,

Leave a Reply