12

I want to upload a file from my Linux server to some FTP server. I know we use put and get commands to upload and download files and the sftp command to connect to the FTP server.

But I wanted to do all this in one shell script and I have no idea how to connect to FTP using the sftp command within some script to upload some file. This is what I know but I don't know how it will work inside one sh script.

sftp -v -oIdentityFile=path user@server
put localPath ftpPath

Can anyone help me?

Elvin
  • 881
  • The sftp command is not for connecting to ftp, it's for connecting to sftp which serves a similar purpose but is not the same protocol. – jordanm Dec 18 '13 at 03:57
  • This answer might help: http://askubuntu.com/a/331078/69599 – Ketan Dec 18 '13 at 04:20
  • do you want ftp connection or sftp ? does are two very different protocol – Kiwy Apr 07 '14 at 11:54

7 Answers7

22

sftp is a shell command. It reads SFTP commands on its standard input.

You can use a here document to pass input to a command.

sftp -v -oIdentityFile=path user@server <<EOF
put localPath ftpPath
EOF

You can use variables inside the here document.

local_path=/path/to/local/file
remote_path=/somewhere/or/other
sftp -v -oIdentityFile=path user@server <<EOF
put $local_path $remote_path
EOF

This is not the simplest way to copy a file. SFTP lets you browse and transfer files, but there is also a shell command to directly copy one file (or even a directory, recursively).

scp -o IdentityFile=path localPath user@server:remotePath
  • scp is definitively the way to go! This makes it a lot easier to detect errors in your script as the sftp command may continue to process commands even if a previous command failed. – Alexis Wilke Apr 13 '15 at 04:26
5

You can specify a batch file using the -b option.

-b batchfile

Ripped from sftp man page:

Batch mode reads a series of commands from an input batchfile instead of stdin. Since it lacks user interaction it should be used in conjunction with non-interactive authentication. A batchfile of ‘-’ may be used to indicate standard input. sftp will abort if any of the following commands fail: get, put, rename, ln, rm, mkdir, chdir, ls, lchdir, chmod, chown, chgrp, lpwd, df, symlink, and lmkdir. Termination on error can be sup‐pressed on a command by command basis by prefixing the command with a ‘-’ character (for example, -rm /tmp/blah*).

7ochem
  • 141
Jeff Smith
  • 1,561
1

Have you considered using rsync?

Uploading files via rsync is arguably the most preferred way and also, the easiest to script. It uses either SSH, RSH or its own protocol ("rsync://...") for target and source transfer.

It is more than likely, that rsync is installed on your system. If you can access it over SSH, you can use scp syntax:

rsync -alPvvz local_path user@host:remote_path

If you need oIdent, I suggest you read this unix.SE question, where this is explaines.


I understand it is not exactly what you asked for, but given that rsync is pretty much an industry standard, it is at least something to consider.

polemon
  • 11,431
0

SFTP programs uses ssh protocol to access, manage, and transfer files to remote systems.It uses different protocols to transfer files. The user names and the data transfer is encrypted during communication. You can't use ftp client to connect to the sftp server or sftp client to connect to the ftp server that only supports ftp.

ifexploit
  • 661
0

To upload file1 to 127.0.0.1 use

ftp -u root@127.0.0.1: file1

You will be prompted for a password. To work around this, you can fill out your .netrc:

cd
cat >.netrc<<EOF
machine 127.0.0.1
login user
password password
EOF
chmod 600 .netrc
0

If you have sftp access to the machine you maybe also have ssh access to the machine.

If you have ssh access you should check if the rsync command is available.

rsync has some advantages over ftp (secure connection, uploads only the parts of the file that have changed - very fast under some circumstances, easy recursive mirroring, compression).

https://en.wikipedia.org/wiki/Rsync

It can be easily used in shell scripts. The only thing you have to do is to setup "key based authentication"

-1

You can use ftp commands inside a shellscript in the following way:

ftp -dv ${SSHHOST} >$dirFtp/$fichFtpLog <<_FINFTP_
    ascii
    put ${dirwork}/${fichSalida} ${rutaSubida}/${fichero}
    close
    quit
_FINFTP_

dirwork/fichSalida ... are shellscript vars as example. You have to note that in order to use ftp in this way, you must configure .netrc so that your login be transparent to the ftp command.

As a resume, you can issue commands as if you were using the ftp command between FINFTP tags (you can use the tag you want).

periket2000
  • 2,368
  • 1
    we'r speaking of secure ftp not ftp or ftp over ssl – Kiwy Apr 07 '14 at 11:53
  • Ok I can't programm here your needs, what's important is the concept. You can do it the same way, connect as sftp and issue commands the same way as you do in ftp. – periket2000 Apr 07 '14 at 11:58