SFTP to send file with bash script

后端 未结 3 1862
忘掉有多难
忘掉有多难 2021-02-04 04:25

I\'m using key authentication, so password is not an issue. I have a file whose name I know and I simply want to send it to another machine over sftp.

I tried searching

相关标签:
3条回答
  • 2021-02-04 04:59

    I don't know how sftp was configurable when this question was asked. Anyway, 6 years later, you can put sftp-commands like PUT into a file and then reference this file in your initial sftp-call. The makes the whole process completely non-interactive and easily configurable:

    sftp -i /path/to/ssh-key -b /path/to/sftp-commands.txt root@remote:/root/dropoff
    

    ....Where sftp-commands.txt just contains

    put test.txt; quit
    
    0 讨论(0)
  • 2021-02-04 05:11

    You said that you are not interested in other tools, but scp is a much better choice for unattended file transfers. Here is an scp example:

    scp test.txt root@remote:/root/dropoff
    
    0 讨论(0)
  • 2021-02-04 05:21

    I know this is an old one, but you can also pass arguments to a command with a Here Document

    You can put the following into a script:

    # The following is called a HERE document
    sftp <user>@<remote> << SOMEDELIMITER 
      put test.txt
      ... # any commands you need to execute via sftp
      quit
    SOMEDELIMITER
    

    each additional command will be fed into the command preceeding the << and SOMEDELIMTER can be anything you want it to be.

    scp is a great option, however sftp was the only tool I was able to get working when pushing from linux to windows and you're stuck using FreeSSHD in service mode!

    0 讨论(0)
提交回复
热议问题