Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SFTP inline put without interaction

I am trying to automate an application deployment as part of this I need to upload a file to a server. I have created a minimal user and configured chroot for the SFTP server but I can't work out how to upload a file non interactive.

At present I am doing scp myfile [email protected]:newBuilds/

I tried sftp [email protected] myfile (newBuilds is the chroot dir) but this didn't upload anything but it did connect.

The reason for favouring this aproach and NOT using scp is that its a lot more difficult to restrict scp access (from the information I have learned).

like image 554
Alex Edwards Avatar asked May 14 '26 01:05

Alex Edwards


2 Answers

If you are using OpenSSH server, chrooting works for both SCP and SFTP.

For instructions see:
https://www.techrepublic.com/article/chroot-users-with-openssh-an-easier-way-to-confine-users-to-their-home-directories/

So I believe your question is irrelevant.


Anyway, sftp (assuming OpenSSH) is not really designed for command-line-only upload. You typically use -b switch to specify batch file with put command.

sftp [email protected] -b batchfile

With batchfile containing:

put /local/path /remote/path

If you really need command-line-only upload, see:

  • Single line sftp from terminal or
  • Using sftp like scp

So basically, you can use various forms of input redirection like:

sftp [email protected] <<< 'put /local/path /remote/path'
  • Or simply use scp, instead of sftp. Most servers support both. And actually OpenSSH scp supports SFTP protocol since 8.7. Since OpenSSH 9.0 it even uses SFTP by default. In 8.7 through 8.9, the SFTP has to be selected via -s parameter. See my answer to already mentioned Single line sftp from terminal.
like image 78
Martin Prikryl Avatar answered May 15 '26 13:05

Martin Prikryl


You can pass inline commands to SFTP like this:

sftp -o PasswordAuthentication=no user@host <<END
lcd /path/to/local/dir
cd /path/to/remote/dir
put file
END
like image 45
demisx Avatar answered May 15 '26 13:05

demisx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!