LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-09-2017, 12:30 PM   #1
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Rep: Reputation: Disabled
Using SFTP to transfer from linux to Windows


How can I, in one line transfer a file from linux to Windows using SFTP?
 
Old 01-09-2017, 12:34 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Well first you need an SFTP server on your Windows box with the necessary firewall exceptions added. After that, you need an sftp uploading tool on your Linux box since the built-in sftp won't let you do it all in one line (I don't think). There are many options, my preference is "lftp".
 
Old 01-09-2017, 12:36 PM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
To initiate the sftp from the Linux side to the Windows side you'd have to setup an sshd to listen for sftp connections on the Windows side first. You can do this with Cygwin. Cygwin can be found at:
https://www.cygwin.com/
There are many articles on how to configure sshd for sftp in Cygwin - just do a web search.

To initiate the sftp from the Windows side to the Linux side there are various tools you can use including WinSCP. WinSCP can be found at:
https://winscp.net/eng/download.php
WinSCP uses an explorer style menu to drag files from one system to the other. There is also a WinSCP console you can run that gives you a command line interface to run sftp commands manually.
 
Old 01-09-2017, 12:39 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
I'd make the opposite assumption from the previous two posts. Since you wish to transfer a file from linux to Windows using SFTP, I'd say put the OpenSSH server on the Linux box. It will be many thousands of times easier. Then you'll still have to figure out a client for the Windows side of things, but at least the server will be done the easy way.

Either way, SFTP can 'put' as well as 'get'
 
Old 01-09-2017, 12:50 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
From the OP's previous threads there is a ssh server running on Windows and can use scp and smbclient to transfer files. What are you trying to accomplish with sftp?
 
Old 01-09-2017, 12:52 PM   #6
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
Well I was told that on the Winodws side they have Globescape EFT. I'm tricky to do this through the command line,. Here's my code:

sftp username@hostname
cd dir/dir
lchdir/export/home/fileishere/file.txt
put file.xlsx

I need to get this to work in one line. Is this possible?
 
Old 01-09-2017, 12:56 PM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by trickydba View Post
I need to get this to work in one line. Is this possible?
If you use keys for authentication, you can use batch mode -b for that. Batch mode can take SFTP commands via stdin or via a file to be read.
 
Old 01-09-2017, 12:58 PM   #8
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
sftp was designed to prompt humans for detailes such as user name, password, local directory, remote directory, file name, ...

scp was designed for automated machines to transfer data among each other (as long as public key encryption is enabled).

regardless of client or server operating system, as long as the ssh client and server are configured correctly, the general syntax would be:
Code:
scp user1@host1:/whatever/floats/your/boat user2@host2:whatever/grinds/your/gears

Last edited by schneidz; 01-09-2017 at 12:59 PM.
 
1 members found this post helpful.
Old 01-09-2017, 01:10 PM   #9
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@Turbocapitaist..... Can you please give me an example
 
Old 01-09-2017, 01:22 PM   #10
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Here is an example using a file.

Code:
$ cat <<EOF > cmds.sftp
> cd /home/tc/Uploads/
> lcd /home/tc/
> put file.ods
> EOF
cd /home/tc/Uploads/
lcd /home/tc/
put file.ods

$ ssh-add ~/.ssh/some_key_rsa

$ sftp -b cmds.sftp -i ~/.ssh/some_key_rsa tc@server.example.com
If you use - instead of a file name it will read from stdin and then you can use echo via pipe.

Code:
$ ssh-add ~/.ssh/some_key_rsa

$ echo -e "cd /home/tc/Uploads/\nlcd /home/tc/\nput file.ods\n" \
        | sftp -b - -i ~/.ssh/some_key_rsa tc@server.example.com
Either way, you have to have the key set up so that it is in the agent if it has a passphrase (recommended) or without a passphrase (not recommended). Because batch mode only works when there is no interactive session.

Last edited by Turbocapitalist; 01-09-2017 at 01:25 PM.
 
1 members found this post helpful.
Old 01-09-2017, 01:24 PM   #11
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Quote:
Originally Posted by schneidz View Post
sftp was designed to prompt humans for detailes such as user name, password, local directory, remote directory, file name, ...

scp was designed for automated machines to transfer data among each other (as long as public key encryption is enabled).
Actually you can do sftp as well as scp with ssh trusts in place. Most of my automated scripts do sftp rather than scp.
 
2 members found this post helpful.
Old 01-09-2017, 01:29 PM   #12
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@Turbocapitalist............I was kinda hoping to get a one-liner that would handle moving a file from linux to windows. The destination server does not need a password
 
Old 01-09-2017, 01:32 PM   #13
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Quote:
Originally Posted by Turbocapitalist View Post
Either way, you have to have the key set up so that it is in the agent if it has a passphrase (recommended) or without a passphrase (not recommended). Because batch mode only works when there is no interactive session.
ssh keys are definitely the most secure way to go and should always be what you try to get in place for automated processes.

However, there is a utility that allows you to send the password, called sshpass. Obviously this has a security risk as it requires you to store your password in clear text somewhere to be passed. But it does work well if you absolutely can't use keys for some reason and has simple syntax:
shpass -p <password> sftp <target server name or IP>
If you're using RHEL or CentOS you can find the package in Fedora's EPEL.
 
Old 01-09-2017, 01:35 PM   #14
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@Turbocapitalist.....Im sorry, your code seems a little confusing

And no password is needed, keys already installed
 
Old 01-09-2017, 01:38 PM   #15
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Both examples are from any non-Windows OS with the usual OpenSSH SFTP client to whatever SFTP server you have, as long as it follows the standards.

The stdin example is one line. The backslash is not necessary and is added for readability. The file-based example is also one line, but does need the file filled with SFTP commands. Both work as long as you can use keys for authentication. If you are using expect instead of keys then you are probably SOL.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot transfer files between windows and ubuntu - SFTP host disconnected and connect ns_chrys Linux - Networking 7 09-29-2013 10:48 AM
[SOLVED] Transfer files in sftp xeon123 Linux - Networking 7 12-17-2010 11:04 AM
SFTP throwing "End of file" when doing SFTP transfer in perl exceed1 Programming 5 03-06-2010 01:34 PM
Can transfer only 2MB using sftp nIMBVS Debian 1 11-26-2005 03:26 PM
sftp: how to transfer the whole directory? kpachopoulos Linux - General 1 11-06-2005 08:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration