LoginSignup
23
9

More than 3 years have passed since last update.

curl でDBやキャッシュの疎通確認もできる

Posted at

これは何

DBへのネットワーク的な疎通確認といえば、MySQLだったら mysql-client、Redisならredis-cliなど専用のコマンドが一般的ですが、単にネットワーク的に疎通を確認したいということであれば curl だけでもできます。

コマンドイメージ

$ curl -v telnet://hogehoge.com:3306

疎通確認をする一般的なコマンドといえば

  • ping コマンド
    • メリット: だいたいどんなサーバにも入っている(気がする)
    • デメリット:
      • icmp を通していないサーバの確認には使えないし、pingだけの確認だと実際には到達できていてもサーバに到達していないと勘違いを起こしやすい
      • トランスポート層のポートまで確認できない
  • telnet コマンド
    • メリット: ホスト名とポートを指定できる柔軟なコマンド
    • デメリット: 接続を解除する方法がわかりにくかったりしてやや使いづらい
  • curl コマンド
    • 一般的に http や https での接続確認
    • 使いやすい

よくある curl の使い方(httpやhttpsに対する疎通確認)

$ curl -v https://hogehoge.com

のように http や https プロトコルを使って接続。

プロトコルをつけなかった時にはどうなるのか?
ubuntuで$ man curlをすると以下のような記述がありました。

If you specify URL without protocol:// prefix, curl will attempt to guess what protocol you  might
       want. It will then default to HTTP but try other protocols based on often-used host name prefixes.
       For example, for host names starting with "ftp." curl will assume you want to speak FTP.

プロトコルをつけないと、よく使われるスキーマが適当に試されるようです。

例えばローカルに立っているサーバに対してプロトコルを指定せずにlocalhostに対して叩くと、httpだと解釈されて実行されます。

$ curl -I localhost
HTTP/1.1 404 Not Found
Server: nginx
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.2.0RC6
Cache-Control: no-cache, private
date: Tue, 30 Jun 2020 09:27:28 GMT

DBやキャッシュへの接続確認

とあるサーバ(ubuntu)からあるDBサーバに接続確認したいケースは以下のようになります。
telnetプロトコルを使用してポートを指定します。telnetはコマンド名だと思っていましたが、実際にはtelnetプロトコルというのがあるようでした。
参考: https://ja.wikipedia.org/wiki/Telnet

$ curl -v telnet://hogehoge.com:3306
* Rebuilt URL to: telnet://hogehoge.com:3306/
*   Trying xx.xx.xx.xx...
* TCP_NODELAY set
* Connected to hogehoge.com (xx.xx.xx.xx) port 3306 (#0)
Warning: Binary output can mess up your terminal. Use "--output -" to tell
Warning: curl to output it to your terminal anyway, or consider "--output
Warning: <FILE>" to save to a file.
* Failed writing body (0 != 29)
* Closing connection 0

上記出力はちょっとわかりづらいですが、接続自体は成功(Connected to hogehoge.comのところ)していて、出力を書き込むのに失敗しているような形。
ちょっと見ずらかったので使わなかったですが、出力の通りoutputオプションを使って--output /dev/null など指定するのもいいかもしれません。

23
9
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
23
9