Link to home
Start Free TrialLog in
Avatar of Robert Saylor
Robert SaylorFlag for United States of America

asked on

linux SFTP

From the Linux shell what command would I call to send a file to a remote server using SFTP?

Note: I am not able to use "scp" to do this. It has to be SFTP.
Avatar of Kent W
Kent W
Flag of United States of America image

sftp client should be installed. You connect to the remote machine, then give file commands.
Let's say you have a file in root dir, full path is
/root/test.txt

From the machine you want to transfer from -
> sftp user@machinename.or.ipaddress
You will be presented with an sftp> carrot / prompt
To send a file,
sftp>  put /root/test.txt /root/text.txt

That is, put the file /root/test.txt form the local fs into /root/test.txt on remote machine.

This works very similar to command line ftp. First you connect, then start giving the local-to-remote transfer commands.

Another option is to use rsync through  ssh.

rsync -avz -e ssh /local/path/tocopy/ /remote/path/tocopy/to

You can get more in-depth usage scenarios and command switches with a simple google search.
Avatar of Robert Saylor

ASKER

I understand the SFTP prompt but how can I do that from a command line in shell? IE: I want to automate sending a file. Can't use SSH. It connects to a remote Windows server so only SFTP works.
Ok, didn't see anything about the target being a windows system in the original post. That can be a bit different.
So that you get nomenclature correct, what I gave you was considered "command line shell". This commonly refers to manually given commands a human issues in real time while sitting at a CLI prompt. What you are looking for is an automated script, usually referred to as a bash script (or batch file in the windows world)  that you then add as a cron job to run automatically on some recurring schedule.

To give you the best answer, I'm assuming you are running some Windows based FTP server that supports SFTP? Keep in mind that "SFTP", today, refers to "ssh over ftp", and not just some secure ftp protocol that many Win based FTP programs support.

So, three  questions, - What SFTP server are you trying to connect to?
Is this Windows machine on the same network as your Linux box?
Have you successfully used SFTP to manually copy files to your Windows box in this same setup before (as in un-automated)?
the program /usr/bin/sftp connects fine to the windows server. SSH and SCP does not.

I am looking for a command to pass the parameters for me. Would expect work in this case?

The remote server really does not have any bearing but looking for a way to pass the command from the linux box.
On the local network could have a bearing, as this would allow you to use more than limited sftp, such as mounting the file system from the remote Linux box and simply issuing cp or rsync commands.

To do sftp automated, without having to handle a bunch of add on's like keychain, sshpass, and expect (all one-off applications), you can pipe this through lftp.

> lftp sftp://user:pass@remote.host.com -e "put filename.txt; bye"

Open in new window

Note the semi-colon after the file name, before the bye command.

To mirror, you can use something like
lftp -e 'mirror -R /local/path /remote/path/' -u user,password host.name.com

Open in new window


Put your final version in a batch file, then add to your crontab on whatever schedule you prefer.   If you need help doing that, chime back, please sir. :)
(Also, I'm asking for remote server software, because many expect different prompts, so it's not always clear-cut on the exact syntax to use)
ASKER CERTIFIED SOLUTION
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
scp is not possible but sftp is, do you get an error with scp?
Thank you this worked perfect!
file: test.sh

#!/usr/bin/expect
spawn /usr/bin/sftp user@hostname
expect "sftp> "
send "put test_file.csv \r"
expect "sftp> "
send "bye \r"
exit 0

Note: from command line: # expect test.sh