curl –resolveでコマンドラインから名前解決する

HTTP サーバに名前ベースのバーチャルホスト設定がされていて、設定されているホスト名でアクセスしたい場合は Host ヘッダーを渡せばOK

IP アドレス 10.0.1.2 に http://www.example.com のホストでアクセスするときには以下のようにする。

curl

$ curl --header "Host: www.example.com" http://10.0.1.2/

wget

$ wget --header="Host: www.example.com" http://10.0.1.2/

telnet

$ telnet 10.0.1.2
GET / HTTP/1.1
Host: www.example.com
<Return>
<Return>

リクエストURLのホストをゴニョゴニョする処理が入っている場合、上の手順ではまずい。
そのため hosts ファイルを書き換えて、ローカルで名前解決させて本来のホスト名でアクセスすることが多々ある。

curl は、このような用途のために --resolve オプションが用意されている。

たとえば hosts ファイルに

10.0.1.2 www.example.com

と書いて、 $ curl http://www.example.com とアクセスするかわりに

$ curl --resolve www.example.com:80:10.0.1.2 http://www.example.com/

とすればよい。

–resolve <host:port:address>

Provide a custom address for a specific host and port pair. Using this, you can make the curl requests(s) use a specified address and prevent the otherwise normally resolved address to be used. Consider it a sort of /etc/hosts alternative provided on the command line. The port number should be the number used for the specific protocol the host will be used for. It means you need several entries if you want to provide address for the same host but different ports.

This option can be used many times to add many host names to resolve.

(Added in 7.21.3)

Added in 7.21.3 という文字が気になるけど、 7.21.3 がリリースされたのは 2010年12月なので、それなりにメンテされているシステムを触っている限りは 7.21.3 以降の curl がインストールされているハズ。

 

References

 

Leave a comment