Wednesday, February 06, 2008

Automating FTP File Transfer

This script shows you how to automate FTPing to remote server.

First of all create a text file, lets name it as automate.ftp

put the following commands into it.

#############################################
open hostname
user username password
cd desireddirectory
lcd localdirectory
bin
put sourcefile destinationfile
quit
#############################################

Text in bold are variables (It can be changed according your requirements)

use the command below to automatically send the files you put in the above automate.ftp

ftp -i -n < /automate.ftp

You can schedule this in cron if you need it regularly. You can also write a script to generate the above file if your file names changes. A typical expample is below.

Contents of sendftp.ksh which can be scheduled in the cron for executing regularly.


#############################################
DATE=`date +"%Y%m%d.%H%M%S"`

echo "open hostname" > /automate.ftp
echo "user username password" >> /automate.ftp
echo "cd destinationdirectory" >> /automate.ftp
echo "lcd localdirectory" >> /automate.ftp
echo "bin" >> /automate.ftp
echo "put "report.txt `hostname`.$DATE.report.txt >> /automate.ftp
echo "quit" >> /automate.ftp
ftp -i -n < /automate.ftp ############################################# Note: Here report.txt is the source file and destination file would be named as hostname with current date prefixed with report.txt

What is SSH Tunneling, How do I achieve that?




SSH tunneling otherwise called as port forwarding is a method of achieving connectivity to remote/blocked ports using SSH protocol.




In a scenario described below, you can make use of SSH tunneling.




Suppose you have, a desktop connects to a remote server which in turn has access to many other machines/devices on the remote network, and only SSH port is open for you to access through the firewalls. How will you connect to http service running on the remote network from your desktop?




The answer is SSH port forwarding (tunneling). Let us look at how to do this. Assuming, Your desktop name is desktop(10.0.0.1), remote gateway server is alpha(10.0.0.1,192.168.1.1) and server which runs the service on remote network is beta(192.168.1.2).


Here desktop does not have direct access to beta or gamma. It has to access alpha for reaching those machines. Using SSH client a person using desktop can access alpha. Once he is logged in, he can access beta or gamma using SSH client which is available on alpha. We use putty (SSH client) for the sake of explanation. You can use any client which has the tunneling ability. Open putty and select SSH (default port 22) and give these details. Click on add and save the session.






Note: you cannot use hostnames when you need to forward ports to the remote servers.

When you open this session, you will be prompted for username/passwords of Alpha. Once you are authenticated, the tunnel is set up to beta.

i.e Anything connecting on port 50000 on desktop will get forwarded to port 22 of beta. If you want to test this, open up another putty session to port 50000 of “localhost” as below. This will start a new SSH session from beta from your desktop.








All communication sent across to beta is secured using encryption technology supported by SSH. You can forward to port 80 instead of port 22 (i.e 192.168.1.2:80), if you want to access the http server running on beta.