Automate FTP

Hi,

Currently, i am using sftp manully to transfer files between two secure servers. Can anyone provide me a sample shell script which can automate the sftp process?

Hate to be short, but use the search button (upper right hand corner over there!). There are a tremendous amount of examples on this board.

I have finally added a post in the faq section on automated ftp.

Navigate

our home page -> Answers to Frequently Asked Questions -> Automate FTP / Scripting FTP Transfers

Perderabo

The information provided was awesome. Being a begginner in Unix, a lot of stuff was difficult for me to understnad. Just wanted to enquire whether the script can be used to include 'SFTP' instead of 'FTP'.

Thanks

Yeah, but you'll need to adjust the script so that it answers the questions sftp asks. Just run sftp by hand and make a note of what you do. Then adjust the script accordingly. Or have the script in a second window and adjust it as you run.

Perderabo,

I made few modifcations to your script to incorporate sftp instead of ftp. I am getting the following error

$ ./coolFtp.sh
$ Warning: tcsetattr failed in ssh_rl_set_tty_modes_for_fd: fd 1: I/O error

The script i am using is :

HOST=<Ip Address>
USER=<user>
PASSWD=<pswd>

exec 4>&1
sftp >&4 2>&4 |&

print -p open $HOST
print -p user $USER $PASSWD
print -p cd rksample
print -p binary
print -p lcd
print -p lcd rktest
print -p put rkftp.txt

exit

Am i doing anything wrong here?

sftp does not support "open" or "user" commands. At least mine does not. I tried the script that you posted. I get a usage statement. This means that my sftp is different than yours and I won't be able to spoonfeed you a answer.

My sftp is unwilling to accept a password via stdin. It seem to open /dev/tty and do a read. I have to arrange for ssh to work without a password. This involved running "ssh-keygen -t rsa". Then I had to copy my ~/.ssh/id_ra.pub file into my .shh/authorized_key2 file on the ftp server. Then on the client system, I ran ssh-agent and used ssh-add to add the new identity. At this point, both ssh and sftp to the ftp server worked without asking for a password.

At this point, this script worked:

 #! /usr/bin/ksh
exec 4>&1
sftp ${USER}@${HOST} >&4 2>&4 |&
print -p get testfile
print -p bye
wait
exit 0

But looking at the manpage for sftp I see that it also supports a batchfile option. That may be another approach worth persuing.

Hi,

I am trying to avoid the passowrd prompt while sftp between two servers. I am trying to sftp from A to B and also from B to A.

I was successful in generating the keys and uploading the public key to server B. So A to B was successful.

But when i am trying to do the same from B to A, its not working. Its prompting for password.

This is what i did:

ssh-keygen -t dsa

scp .ssh/id_dsa.pub xyz@X.XXX.XXX.XX:/.ssh/authorized_keys

Now i log out and log in and try sftp to A from B, i am prompted for password...

Any suggestions?

Thanks in adv

There is a Perl module that handles this Net::SFTP. All modern *NIX distros come with Perl. You may have to install the modules. I'm not sure if you have root permissions. After installing the needed modules, this task could be done in about 4 lines of code.

\#!/usr/bin/perl
use Net::SFTP;
my %args = \("user", "USERNAME", "password", "PASSWORD"\);
my $sftp = Net::SFTP-&gt;new\($host, %args\);
$sftp-&gt;get\("REMOTE_FILENAME", "LOCAL_FILENAME"\);

http://search.cpan.org/~drolsky/Net-SFTP-0.08/lib/Net/SFTP.pm

Hi

I am trying to make a shell script to automate the ftp process. The FTP command works fine but the script is not able to enter the username and password automatically. It stops at the username prompt and I have to do things manually from there on. Any solution??

One more thing - is there any command which would do the work of the RETURN KEY? Like if i want to skip the user name prompt by pressing the return key...can this action be coded in a shell script by a command?

Thanks

Hi

as i observed further, the script execution stops as soon as it enters the ftp mode. no command executes after the ftp command. however if i use 'bye' to exit the ftp mode, the rest of the commands in the shell script execute automatically. So do we have to write a separate shell script for ftp mode? is it possible? a shell script to execute just in the ftp modE?
Please helpppp

Best client utility around for FTP scripting operations is lftp (LFTP - sophisticated file transfer program ), it handles many flavors of FTP including SFTP and has username/password mgmt.