Опубликован: 06.08.2012 | Доступ: свободный | Студентов: 1194 / 25 | Оценка: 5.00 / 5.00 | Длительность: 53:41:00
Лекция 24:

Basic network access: clients

ftp

ftp is the Internet File Transfer Program, and is the standard way to transfer large files long distances across the Net. It works for small files and short distances too, but you may find that scp or NFS are better alternatives in these areas.

One serious drawback in duplicating files across the net is that you need to have permission to access the remote system. Traditionally, you need a user ID to access a system. Of course, the file server could have a specific user ID without a password, but that would throw the system open to attack from crackers. ftp solves this problem by recognizing the special user ftp. This user name used to be anonymous, but it turned out to be a problem to spell. ftp servers still accept the name anonymous as well. Login is special: you don't need a password, but by convention, to help the system administrators with their bookkeeping, you should enter your real user ID in place of the password when logging in as ftp.A typical session might look like:

$ ftp ftp.freebsd.org
Connected to ftp.beastie.tdk.net.
cd 220 ftp.beastie.tdk.net FTP server (Version 6.00LS) ready.
331 Guest login ok, send ident as password.
Password:    password does not echo
230 Guest login ok, access restrictions apply.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> bin    to ensurebinary transfer
200 Type set to I.
230- The FreeBSD mirror at Tele Danmark Internet.
Moremessagesomitted Name (grog): ftp
331 Guest login ok, send your email address as password.
ftp> cd /pub/FreeBSD/ports/distfiles
250 CWD command successful.
ftp> get xtset-1.Ctar.gz
local: xtset-1.0.tar.gz remote: xtset-1.0.tar.gz
229 Entering Extended Passive Mode (|||58059|)
150 Opening BINARY mode data connection for 'xtset-1.0.tar.gz'   (4239 bytes).
100%  |*************************************|    4239    5.49 KB/s  00:00
226 Transfer complete.
4239 bytes received in 00:00 (5.49 KB/s)
ftp> ^D
221 Goodbye.

There are a number of things to note about this transfer:

  • The server may have multiple names, and the one you use may not be its canonical name (the name specified in the server's DNS A record—see page 363). By convention, the first part of the name of an ftp server is ftp. Here we connected to the server with the name ftp://ftp.FreeBSD.org, but the canonical name of the server is ftp://ftp.beastie.tdk.net.
  • Some versions of ftp transmit in ASCII mode by default: they change every incidence of the ASCII line feed character (the C language constant \n) to the sequence \r\n (they prep end an ASCII carriage return character). This permits you to print the results on normal printers, but makes a terrible mess of binary files. Transmitting in binary form always works. As the message shows, the FreeBSD ftp server uses binary mode, but it doesn't harm to enter the bin command. The message Type set to I. is ftp's way of telling you that it has set binary transmission mode.
  • The line of **** is an indication of the progress of the transfer. It's specific to BSD; other ftp clients don't show you anything here.

Specifying file names as URIs

This transmission is fairly typical, and it's the traditional way to do it. FreeBSD has another method, though, which can be of use: instead of the interactive approach, you can specify the file name as a URI, and you can use ftp to download HTTP documents from a web server. For example, the last transfer can be simplified to:

$ ftp ftp://ftp.freebsd.org/pub/FreeBSD/ports/distfiles/xtset-l.O.tar.gz
Connected to ftp.beastie.tdk.net.
220 ftp.beastie.tdk.net FTP server (Version 6.00LS) ready.
331 Guest login ok, send your email address as password.
...
230 Guest login ok, access restrictions apply.
Remote system type is UNIX.
Using binary mode to transfer files.
200 Type set to I.
250 CWD command successful.
250 CWD command successful.
250 CWD command successful.
250 CWD command successful.
local: xtset-1.0.tar.gz remote: xtset-1.0.tar.gz
229 Entering Extended Passive Mode (|||59779|)
150 Opening BINARY mode data connection for 'xtset-1.0.tar.gz' (4239 bytes).
100%  |*************************************|  4239  5.82 KB/s  00:00
226 Transfer complete.
4239 bytes received in 00:00 (5.81 KB/s)
Goodbye.

Note that this method implies anonymous ftp: you don't have to log in. In the same way, you can download a web page like this:

$ ftp http://www.FreeBSD.org/index.litml
Requesting http://www.FreeBSD.org/index.html
100%  |*************************************| 26493  12.20 KB/s 00:02
26493 bytes retrieved in 00:02 (12.17 KB/s)

Note that in this case you can't just specify the URI as http://www.FreeBSD.org: you must specify the real file name.

Other ftp commands

ftp has about sixty commands, some of which can be of use. We'll look at the most useful commands in the following sections.

mget

ftp> ls
200 PORT command successful.
150 Opening ASCII mode data connection for /bin/ls.
-rw-rw-r--  1  117  1001    43367  Nov   1  02:37  gcc-2.5.0-2.5.2.diff.gz
-rw-rw-r--  1  117  1001     1010  Nov   1  02:37  gcc-2.5.1-2.5.2.diff.gz
-rw-rw-r--  1  117  1001    78731  Nov  11  13:53  gcc-2.5.2-2.5.3.diff.gz
-rw-rw-r--  1  117  1001    13931  Nov  17  09:27  gcc-2.5.3-2.5.4.diff.gz
-rw-rw-r--  1  117  1001    76271  Nov  27  16:48  gcc-2.5.4-2.5.5.diff.gz
-rw-rw-r--  1  117  1001     8047  Dec   3  09:22  gcc-2.5.5-2.5.6.diff.gz
-rw-rw-r--  1  117  1001  5994481  Nov  27  16:49  gcc-2.5.5.tar.gz
-rw-rw-r--  1  117  1001    10753  Dec  12  19:15  gcc-2.5.6-2.5.7.diff.gz
-rw-rw-r--  1  117  1001    14726  Jan  24  09:02  gcc-2.5.7-2.5.8.diff.gz
-rw-rw-r--  1  117  1001  5955006  Dec  22  14:16  gcc-2.5.7.tar.gz
-rw-rw-r--  1  117  1001  5997896  Jan  24  09:03  gcc-2.5.8.tar.gz
226 Transfer  complete.          
ftp>

Frequently you need to copy more than a single file. For example, if you currently have gcc-2.5.0 and want to get gcc-2.5.8, you will discover the following files on the file server:

In other words, you have the choice of transferring 6 MB of software in gcc-2.5.8.tar.gz or seven incremental patch files with a total of less than 250 kB. On the other hand, copying the diffs requires typing all these long, complicated file names, so you might decide it's easier just to duplicate the whole 6 MB.

There is an easier way: mget (multiple get) duplicates files matching a wild card. You could perform the complete transfer with:

ftp> mget gcc-2*diff.gz
mget gcc-2.5.0-2.5.2.diff.gz?y
200 PORT command successful.
150 Opening BINARY mode data connection for
    gcc-2.5.0-2.5.2.diff.gz (43667 bytes).
226 Transfer complete.
43667 bytes received in 19 seconds (2.298 Kbytes/s)
mget gcc-2.5.1-2.5.2.diff.gz?n        we don't need this one
mget gcc-2.5.2-2.5.3.diff.gz?y
200 PORT command successful.
150 Opening BINARY mode data connection for
  gcc-2.5.2-2.5.3.diff.gz (78731 bytes).
226 Transfer complete.
78731 bytes received in 33 seconds (2.835 Kbytes/s)
... etc

prompt

Using mget saves a lot of network bandwidth and copies the files faster, but it has one disadvantage: ftp prompts you for each file name, so you have to wait around to answer the prompts. If you don't, ftp disconnects after 15 minutes of inactivity. It would be simpler to perform all the mgets without any intervention. This is where the prompt command comes in.

The prompt command specifies whether to issue certain prompts or not—the mget command is one example. This command is a toggle—in other words, if prompting is on, prompt turns it off, and if prompting is off, prompt turns it on. If prompting is off, the mget command in the previous example would have gone through with no interruptions.

In the previous example, you don't really want to transfer the file gcc-2.5.1-2.5.2.diff.gz, because you don't need it to perform the patches: you can upgrade from 2.5.0 to 2.5.2 directly with the file gcc-2.5.0-2.5.2.diff.gz. On the other hand, not copying the file would mean sitting around for the duration of the transfer and answering the prompt for each file, and the file is only 1 kB long. In this case, it is reasonable to copy it as well-in other cases; you may need to consider alternatives.

Владимир Шишкин
Владимир Шишкин
Россия, Киров
Олег Страхов
Олег Страхов
Россия