cURL 을 사용한 고급 연결 테스트
기본 연결 테스트를 이해했으므로, 자세한 문제 해결 및 테스트에 도움이 될 수 있는 cURL 의 더 고급 기능을 살펴보겠습니다.
자세한 디버깅을 위한 Verbose 모드 사용
Verbose 모드 (-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
Verbose 출력은 연결이 성공했는지 또는 문제가 있었는지 보여줍니다.