Блог Олега Сердюкова

Выгрузка файла на FTP-сервер с помощью curl

curl - мощная утилита для обмена файлами. Я постоянно пользуюсь ею для списывания файлов через консоль, но только вчера обнаружил, что она может использоваться и для выгрузки файлов.

С помощью такой команды можно передать файл testfile.zip на FTP-сервер ftp.domain.com под пользователем user с паролем password:

$ curl --upload-file testfile.zip ftp://user:password@ftp.domain.com/

Можно выяснить среднюю скорость выгрузки:

curl --upload-file testfile.zip ftp://user:password@ftp.domain.com/ --silent \
	--write-out "Time: %{time_total}\nSize: %{size_upload}\nUpload speed: %{speed_upload}\n"

В секции –write-out возможны такие переменные:

  • url_effective. The URL that was fetched last. This is most meaningful if you’ve told curl to follow location: headers.
  • http_code. The numerical response code that was found in the last retrieved HTTP(S) or FTP(s) transfer. In 7.18.2 the alias response_code was added to show the same info.
  • http_connect. The numerical code that was found in the last response (from a proxy) to a curl CONNECT request. (Added in 7.12.4)
  • time_total. The total time, in seconds, that the full operation lasted. The time will be displayed with millisecond resolution.
  • time_namelookup. The time, in seconds, it took from the start until the name resolving was completed.
  • time_connect. The time, in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed.
  • time_appconnect. The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed. (Added in 7.19.0)
  • time_pretransfer. The time, in seconds, it took from the start until the file transfer was just about to begin. This includes all pre-transfer commands and negotiations that are specific to the particular protocol(s) involved.
  • time_redirect. The time, in seconds, it took for all redirection steps include name lookup, connect, pretransfer and transfer before the final transaction was started. time_redirect shows the complete execution time for multiple redirections. (Added in 7.12.3)
  • time_starttransfer. The time, in seconds, it took from the start until the first byte was just about to be transferred. This includes time_pretransfer and also the time the server needed to calculate the result.
  • size_download. The total amount of bytes that were downloaded.
  • size_upload. The total amount of bytes that were uploaded.
  • size_header. The total amount of bytes of the downloaded headers.
  • size_request. The total amount of bytes that were sent in the HTTP request.
  • speed_download. The average download speed that curl measured for the complete download.
  • speed_upload. The average upload speed that curl measured for the complete upload.
  • content_type. The Content-Type of the requested document, if there was any.
  • num_connects. Number of new connects made in the recent transfer. (Added in 7.12.3)
  • num_redirects. Number of redirects that were followed in the request. (Added in 7.12.3)
  • redirect_url. When a HTTP request was made without -L to follow redirects, this variable will show the actual URL a redirect would take you to. (Added in 7.18.2)
  • ftp_entry_path. The initial path libcurl ended up in when logging on to the remote FTP server. (Added in 7.15.4)
  • ssl_verify_result. The result of the SSL peer certificate verification that was requested. 0 means the verification was successful. (Added in 7.19.0)

Штатный curl, идущий в поставке Mac OS X 10.6, поддерживает такие протоколы:

$ curl -V
curl 7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3
Protocols: tftp ftp telnet dict ldap http file https ftps
Features: GSS-Negotiate IPv6 Largefile NTLM SSL libz

Рекомендую почитать man curl - это может пригодиться. К curl также есть API к разнообразным языкам программирования - PHP, Perl, Python, и т.д. Так что использовать утилиту можно - была бы необходимость.

Comments