cURL による高度な接続性テスト
基本的な接続性テストを理解したところで、詳細なトラブルシューティングとテストに役立つ cURL のより高度な機能を調べてみましょう。
詳細なデバッグのための詳細モードの使用
詳細モード(-vオプション)は、リクエストとレスポンスのプロセス全体を表示するため、接続性の問題をトラブルシューティングする上で非常に役立ちます。
curl -v https://example.com
出力は包括的で、DNS 解決、TLS ハンドシェイク、リクエストヘッダー、レスポンスヘッダーなどを表示します。
* Trying 93.184.216.34:443...
* Connected to example.com (93.184.216.34) port 443 (#0)
* ALPN: offers h2
* ALPN: offers http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN: server accepted h2
* Server certificate:
* subject: C=US; ST=California; L=Los Angeles; O=Internet Corporation for Assigned Names and Numbers; CN=www.example.org
* start date: Nov 24 00:00:00 2022 GMT
* expire date: Nov 24 23:59:59 2023 GMT
* subjectAltName: host "example.com" matched cert's "example.com"
* issuer: C=US; O=DigiCert Inc; CN=DigiCert TLS RSA SHA256 2020 CA1
* SSL certificate verify ok.
* using HTTP/2
* h2 [:method: GET]
* h2 [:path: /]
* h2 [:scheme: https]
* h2 [:authority: example.com]
* h2 [user-agent: curl/7.81.0]
* h2 [accept: */*]
* Using Stream ID: 1
> GET / HTTP/2
> Host: example.com
> user-agent: curl/7.81.0
> accept: */*
>
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* Connection state changed (MAX_CONCURRENT_STREAMS == 100)!
< HTTP/2 200
< age: 587269
< cache-control: max-age=604800
< content-type: text/html; charset=UTF-8
< date: Tue, 28 Mar 2023 12:40:01 GMT
< etag: "3147526947+ident"
< expires: Tue, 04 Apr 2023 12:40:01 GMT
< last-modified: Thu, 17 Oct 2019 07:18:26 GMT
< server: ECS (nyb/1D2B)
< vary: Accept-Encoding
< x-cache: HIT
< content-length: 1256
<
<!doctype html>
<html>
<head>
<title>Example Domain</title>
<!-- More HTML content -->
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents...</p>
<!-- More content -->
</div>
</body>
</html>
* Connection #0 to host example.com left intact
この詳細な出力は、接続が正確にどこで失敗しているかを特定するのに役立ちます。
さまざまな HTTP メソッドのテスト
テスト API エンドポイントへの POST リクエストをテストしてみましょう。
curl -X POST -d "name=test&email=test@example.com" https://httpbin.org/post
このコマンドは次のとおりです。
-X POST: POST リクエストを指定します
-d "name=test&email=test@example.com": リクエストでフォームデータを送信します
送信したデータを示す JSON レスポンスを受け取るはずです。
{
"args": {},
"data": "",
"files": {},
"form": {
"email": "test@example.com",
"name": "test"
},
"headers": {
"Accept": "*/*",
"Content-Length": "32",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "curl/7.81.0",
"X-Amzn-Trace-Id": "Root=1-642295b1-0d2340ef34f2e8ea6270241a"
},
"json": null,
"origin": "198.51.100.42",
"url": "https://httpbin.org/post"
}
カスタムヘッダーを使用したテスト
多くの API では、認証またはコンテンツタイプを指定するために特定のヘッダーが必要です。これをテストしてみましょう。
curl -H "User-Agent: MyCustomAgent" -H "Authorization: Bearer test-token" https://httpbin.org/headers
このコマンドは次のとおりです。
-H "User-Agent: MyCustomAgent": カスタム User-Agent ヘッダーを設定します
-H "Authorization: Bearer test-token": Authorization ヘッダーを設定します
レスポンスには、リクエストで送信されたヘッダーが表示されます。
{
"headers": {
"Accept": "*/*",
"Authorization": "Bearer test-token",
"Host": "httpbin.org",
"User-Agent": "MyCustomAgent",
"X-Amzn-Trace-Id": "Root=1-642295c3-73cac0a73b34b1c93a8ce520"
}
}
さまざまなエンドポイントの応答時間のテスト
さまざまなサーバーの応答時間を比較するスクリプトを作成しましょう。
nano response_time.sh
次の内容を追加します。
#!/bin/bash
echo "Testing response times..."
for site in google.com bing.com baidu.com duckduckgo.com yahoo.com; do
echo -n "$site: "
curl -s -o /dev/null -w "%{time_total}s" "https://$site"
echo ""
done
echo "Testing complete!"
ファイルを保存し、実行可能にします。
chmod +x response_time.sh
スクリプトを実行します。
./response_time.sh
出力には、各サイトの応答時間が表示されます。
Testing response times...
google.com: 0.187s
bing.com: 0.232s
baidu.com: 0.412s
duckduckgo.com: 0.298s
yahoo.com: 0.342s
Testing complete!
これは、さまざまなサーバーのパフォーマンスを比較したり、時間の経過に伴うサーバーのパフォーマンスを監視したりするのに役立ちます。
特定のポートへの TCP 接続性のテスト
特定のポートがサーバーで開いているかどうかをテストする必要がある場合があります。cURL もこれに使用できます。
curl -v telnet://example.com:80
ポートが開いている場合は、接続が成功したというメッセージが表示されます。
* Trying 93.184.216.34:80...
* Connected to example.com (93.184.216.34) port 80 (#0)
Ctrl+Cを押して接続を終了します。
同様に、安全な接続をテストできます。
curl -v https://example.com:443
詳細出力は、接続が成功したかどうか、または問題があったかどうかを示します。