Persistent non-interactive FTP session for usage in shell scripts

1.6k views Asked by At

I would like to script a complex scenario of file transfers from Linux to a Windows FTP server (detecting new/changed files and then transfer) and I am unable to find a command-line ftp client which supports sending files over a persistent (one time login) session on non-interactive mode.

ssh/scp with password-less authentication is not an option, since I have no access to the Windows FTP server.

For example using lftp

`lftp -u user,pass -p 21 -e "mput newfile.txt;"` 10.0.0.100 

will upload my file to the server, but requires authentication/handshaking to happen on every request, which takes a long time and severely slows down the transfers.

I am wondering whether there is a way to authenticate once, establish a persistent connection and then sending the files over this connection. lftp and other clients are able to do this in interactive mode (connect and then perform multiple commands until quitting).

Therefore:

  • Is there an FTP client on Linux which may operate like this in non-interactive mode?
  • Alternatively, is there a way to simulate this behaviour, scripting over the interactive mode? Maybe something utilizing expect with netcat/telnet and/or named pipes?
1

There are 1 answers

5
glenn jackman On

Untested, but you can do something like this Tcl:

package require ftp
set conn [ftp::Open server user pass]
while true {
    after 5000          ;# sleep 5 seconds
    foreach newfile [glob -nocomplain *] {
        puts "sending $newfile"
        ftp::Put $newfile
        file delete $newfile
    }
}

The expect would be similar. untested:

set timeout -1
spawn lftp -u user,pass server
expect "> "
while true {
    sleep 5
    foreach newfile [glob -nocomplain *] {
        puts "sending $newfile"
        send "put $newfile\r"
        expect "> "
        file delete $newfile
    }
    send "\r"          ;# "hit enter" as a keep-alive
}