curl は色々とオプションがあるので、使い込むと楽しい
-w オプションで、必要な物を取り出す。
レスポンスコード
curl -I -s -o /dev/null http://store.apple.com -w '%{http_code}\n' 301
ダウンロード・サイズ
curl -L -s -o /dev/null http://store.apple.com -w '%{size_download}\n' 59770
これらを必要な出力(JSON)にまとめる
出力はJSONにまとめることが出来る
takuya@atom:~$ curl -L -s -o /dev/null http://store.apple.com -w '{ "dl_size" : %{size_download}, "status_code" : %{http_code} }' | jq . { "dl_size": 59851, "status_code": 200 }
うーん便利。
よく使いそうなものをまとめて、JSON化
curl -s -o /dev/null https://store.apple.com/ -w '{ "http_code" : "%{http_code}", "time_total" : "%{time_total}", "time_namelookup" : "%{time_namelookup}", "time_connect" : "%{time_connect}", "time_redirect" : "%{time_redirect}", "size_download" : "%{size_download}", "size_request" : "%{size_request}", "speed_download" : "%{speed_download}", "content_type" : "%{content_type}", "num_connects" : "%{num_connects}", "num_redirects" : "%{num_redirects}", "redirect_url" : "%{redirect_url}" }"' | jq .
実行結果
{ "http_code": "301", "time_total": "0.163", "time_namelookup": "0.002", "time_connect": "0.008", "time_redirect": "0.000", "size_download": "0", "size_request": "79", "speed_download": "0.000", "content_type": "", "num_connects": "1", "num_redirects": "0", "redirect_url": "https://store.apple.com/us" }
-w で使えるオプション
結構色々と使えます。速度測定に便利ですね。php や ruby で書くとどうも何処がオーバーヘッドになってるかわからないし、curl ならシェルにコピペでいいいわけで。
curl の動かない環境はないに等しいのでパパっと使えて便利ですね。
- filename_effective
- http_code
- http_connect
- time_total
- time_namelookup
- time_connect
- time_appconnect
- time_pretransfer
- time_redirect
- time_starttransfer
- size_download
- size_upload
- size_header
- size_request
- speed_download
- speed_upload
- content_type
- num_connects
- num_redirects
- redirect_url
- ftp_entry_path
- ssl_verify_result
curlのサンプルで使ったよくつかうオプション
基本 -s オプション -o オプション
-o オプションは、出力ファイルを指定する。/dev/null とか指定すると便利です。
curl -o /dev/null http://store.apple.com/jp % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 55332 0 55332 0 0 1747k 0 --:--:-- --:--:-- --:--:-- 2843k
リダイレクトより、 -o /dev/null のほうが curl のヘッダオプションを使うときは扱いやすいので、こちらを使いました。
-s オプションは、プログレスバーを出さない。 silent モード
curl -o /dev/null -s http://store.apple.com/jp ## 何も出ない
基本その2 -I オプションと -L オプション
-I をつけるとヘッダ情報だけ出せる。
-L オプションをつけると、リダイレクトを追跡します。
curl -I -L http://store.apple.com HTTP/1.1 301 Moved Permanently x-request-id: 9e9179c7-7cac-4a83-bab9-9d6f53ba21e3 Pragma: no-cache Location: /us Content-Length: 0 x-frame-options: sameorigin Cache-Control: private, must-revalidate, proxy-revalidate, max-age=24, post-check=0, pre-check=0 Expires: Tue, 23 Sep 2014 13:34:33 GMT Date: Tue, 23 Sep 2014 13:34:09 GMT Connection: keep-alive HTTP/1.1 200 OK Cache-Control: no-store, private, must-revalidate, proxy-revalidate, max-age=0, pre-check=0, post-check=0, no-cache ETag: "6a68935248b237f3bb92bf6654ca3bb3" Expires: Mon, 22 Sep 2014 13:34:09 GMT Last-Modified: Tue, 23 Sep 2014 13:33:13 GMT Pragma: no-cache X-XSS-Protection: 1; mode=block Content-Type: text/html;charset=UTF-8 x-request-id: e9da1b39-44f5-4bf4-8be6-fa2dc2692022 x-frame-options: sameorigin Date: Tue, 23 Sep 2014 13:34:09 GMT Connection: keep-alive Set-Cookie: dssid2=eef56de0-36ae-4dfa-ba8c-48fb30b943f6; Domain=.apple.com; Expires=Wed, 23-Sep-2015 13:34:09 GMT; Path=/ Set-Cookie: dssf=1; Domain=.apple.com; Expires=Wed, 23-Sep-2015 13:34:09 GMT; Path=/ Set-Cookie: dc=nc; Domain=.store.apple.com; Expires=Tue, 23-Sep-2014 14:04:09 GMT; Path=/