LoginSignup
0
0

More than 5 years have passed since last update.

Ansible でFTPサーバーにファイルを追加したかった...

Last updated at Posted at 2018-08-22

Ansibleで非SFTPなFTPサーバーにファイルをPUTしようとして試行錯誤したが、結局 curl 叩くのがベストかなと思った次第です。

curl--ftp-create-dirs オプション凄く便利だなって思った。

環境

$ docker inspect --format="{{.Config.Image}}" pure-ftpd
stilliard/pure-ftpd:hardened
$ curl --version
curl 7.54.0 (x86_64-apple-darwin17.0) libcurl/7.54.0 LibreSSL/2.0.20 zlib/1.2.11 nghttp2/1.24.0
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz HTTP2 UnixSockets HTTPS-proxy

curl で色々

$ curl -I ftp://foo:bar@localhost/1/; echo $?
0

$ curl -I ftp://foo:bar@localhost/404; echo $?
curl: (19) Given file does not exist
19

$ curl --silent ftp://foo:bar@localhost/newdir/newfile --ftp-create-dirs --upload-file /path/to/test.txt; echo $?
0

$ curl -I ftp://foo:bar@localhost/newdir/newfile; echo $?
Last-Modified: Wed, 22 Aug 2018 05:51:55 GMT
Content-Length: 143
Accept-ranges: bytes
0

期待した通りにディレクトリを作成してくれないケース

curl の仕様なのか Pure-FTPd の仕様なのかはたまたRFCで定義されているのか、そこまで未調査ではありますが作成するディレクトリの名称に半角スペース (0x20) を含む場合、半角スペースが _ に置換された名称のディレクトリが作成されてしまうことを確認済みです。

そもそもそこまで考慮するなら FTPサーバーとの通信に curl なんて使わない方が良いと個人的には思うのでこれ以上は追求していません :sweat_smile:

$ curl --silent --ftp-create-dirs "ftp://foo:bar@127.0.0.1/x/ z/xz.log" --upload-file "x/ z/xz.log" -vvvvvvvvvvv
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 21 (#0)
< 220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
< 220-You are user number 1 of 5 allowed.
< 220-Local time is now 09:34. Server port: 21.
< 220-This is a private system - No anonymous login
< 220 You will be disconnected after 15 minutes of inactivity.
> USER foo
< 331 User foo OK. Password required
> PASS bar
< 230 OK. Current directory is /
> PWD
< 257 "/" is your current location
* Entry path is '/'
> CWD x
* ftp_perform ends with SECONDARY: 0
< 250 OK. Current directory is /x
> CWD  z
< 550 Can't change directory to  z: No such file or directory
> MKD  z
< 257 "_z" : The directory was successfully created
> CWD  z
< 550 Can't change directory to  z: No such file or directory
* Server denied you to change to the given directory
* Uploaded unaligned file size (0 out of 18 bytes)
* Connection #0 to host 127.0.0.1 left intact

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0