How to download files from FTP site in one command line without user interaction (Windows)

13

3

I know there is an FTP command which can be run from command line on Windows, and it downloads a file from an FTP site. User + password are specified in "that" one-line cmd. These options + password passtrough should be on that command line.

echo open 192.168.1.64 21> ftp.txt
echo anonymous>> ftp.txt
echo ftp@ftp.com>> ftp.txt
echo bin >> ftp.txt
echo get test.txt >> ftp.txt
echo bye >> ftp.txt

ftp -s:ftp.txt

soundhax

Posted 2013-01-10T18:01:21.743

Reputation: 343

Do you need to download via FTP or do you just need to download those file with any program? (Rsync can read from a file with names to sync and from the command line. Scp can also do that from the command line. Both are not as ancient as plain-text,no_true_security FTP). – Hennes – 2013-01-10T18:16:59.507

Answers

7

I found the way:

echo open 192.168.0.1 >> ftp &echo user admin w00t >> ftp &echo binary >> ftp &echo get file.zip >> ftp &echo bye >> ftp &ftp -n -v -s:ftp &del ftp

soundhax

Posted 2013-01-10T18:01:21.743

Reputation: 343

This did not work when I tried it (I ended up using WinSCP's command line client instead). Which version of Windows was it tested on?

– Peter Mortensen – 2016-05-06T16:50:40.633

Could you explain how this line works? Can't find any documentation. I'm trying to make it work with psftp – stallingOne – 2016-11-10T16:11:56.643

@stallingOne Think of the & as a line breaker... it is actually multiple commands. Basically he builds a script, line by line, using echo, and then tell ftp to run it. He's naming the script file ftp too, which makes it confusing. – Havenard – 2020-01-28T18:18:12.223

Poor choice of file name. "ftp" is both the file name of the script he's building and the name of the exe. So the path resolution at ftp -n -v -s:ftp is a bit hinky. (Each use of "ftp" refer to different things) – Chad Schouggins – 2020-02-10T22:02:59.560

13

Try this: Batch files - Unattended FTP downloads

WGET ftp://ftp.mydomain.com/path/file.ext  

for anonymous downloads

or:

WGET ftp://user:password@ftp.mydomain.com/path/file.ext  

when authentication is required.


As @XavierStuvw pointed out via edits and comments, swapping WGET to a lowercase wget would work in linux.

wget ftp://user:password@ftp.mydomain.com/path/file.ext

CalvT

Posted 2013-01-10T18:01:21.743

Reputation: 641

wget ftp://user:password@ftp.mydomain.com/path/file.ext also works with Linux systems -- with a lower-case wget thus – XavierStuvw – 2017-03-08T16:55:39.723

@XavierStuvw that is correct - but this question is about Windows - I will edit the answer though – CalvT – 2017-03-08T16:56:57.203

1@CalvT No objection. The envisioned benefit of the comment is that Linux-users may land on this page after an internet search and oversee the system it applies to. It happened to me, in fact. – XavierStuvw – 2017-03-08T16:59:32.147

@XavierStuvw fair enough - I thought that the title would point them elsewhere - I've edited the answer, but will make it more prominent :) – CalvT – 2017-03-08T17:00:31.153

1@CalvT The title is indeed precise, but search engines often visualize the whole answer frame and it's easy to splash into that. Of course this should not discount the reader from being mindful, but in this case it was coincidental that Linux and Windows instructions are basically the same. Well done, thanks. – XavierStuvw – 2017-03-08T17:05:00.570

i dont see any command wich has user and password switch option in ONE line there. – soundhax – 2013-01-10T18:26:26.877

-s switch was in sentence as i remember – soundhax – 2013-01-10T18:29:56.537

2

Welcome to Super User! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.

– Canadian Luke – 2013-01-10T18:51:30.797

2

Note that you can ask for the syntax of a command in DOS by using the /? switch. For example:

C:\>ftp /?

Transfers files to and from a computer running an FTP server service
(sometimes called a daemon). Ftp can be used interactively.

FTP [-v] [-d] [-i] [-n] [-g] [-s:filename] [-a] [-A] [-x:sendbuffer] [-r:recvbuf
fer] [-b:asyncbuffers] [-w:windowsize] [host]

  -v              Suppresses display of remote server responses.
  -n              Suppresses auto-login upon initial connection.
  -i              Turns off interactive prompting during multiple file
                  transfers.
  -d              Enables debugging.
  -g              Disables filename globbing (see GLOB command).
  -s:filename     Specifies a text file containing FTP commands; the
                  commands will automatically run after FTP starts.
  -a              Use any local interface when binding data connection.
  -A              login as anonymous.
  -x:send sockbuf Overrides the default SO_SNDBUF size of 8192.
  -r:recv sockbuf Overrides the default SO_RCVBUF size of 8192.
  -b:async count  Overrides the default async count of 3
  -w:windowsize   Overrides the default transfer buffer size of 65535.
  host            Specifies the host name or IP address of the remote
                  host to connect to.

Notes:
  - mget and mput commands take y/n/q for yes/no/quit.
  - Use Control-C to abort commands.

In your case, you'll want to use the -s switch to feed it a script, including the login responses.

For example:

  1. Create a script file (c:\scriptfile.txt) with the following contents:

    open
    servername_or_ip
    username
    password
    get
    /fullpath/thefile.txt
    c:\fullpath\thefile.txt
    quit
    
  2. execute ftp with the -s switch and specify the script filename

    C:\>ftp -s:c:\scriptfile.txt
    

yosh m

Posted 2013-01-10T18:01:21.743

Reputation: 2 048

On Windows XP (yes, yes, I know), this results in "Unknown host /?." – Peter Mortensen – 2016-04-05T19:30:02.080