Byeon-gun's WIPI

IT 인프라와 소프트웨어 개발에 관한 실용적인 기술 가이드와 튜토리얼을 제공하는 기술 블로그입니다.

Byeon-gun's WIPI

IT 인프라와 소프트웨어 개발에 관한 실용적인 기술 가이드와 튜토리얼을 제공하는 기술 블로그입니다.

curl 명령어

curl은 CLI 환경에서 HTTP/HTTPS 요청을 전송하고 응답을 확인할 수 있는 가장 기본적이면서도 강력한 도구입니다. API 테스트, 파일 다운로드, 프록시/네트워크 디버깅, 웹 성능 측정 등 다양한 상황에서 활용할 수 있습니다.

1. 기본 구문

curl [options...] <URL>

주요 옵션

옵션설명
-XHTTP 메서드 지정 (GET, POST 등)
-dPOST 데이터 전송
-HHTTP 헤더 설정
-O원본 파일명으로 다운로드
-sSilent 모드 (출력 최소화)
-S에러 발생 시 메시지 출력
-fHTTP 에러 코드(4xx/5xx) 시 실패 처리
-w요청 완료 후 추가 정보 출력
–noproxy프록시를 사용하지 않을 호스트 목록 지정
-x, –proxy지정한 프록시 서버 사용
–resolveDNS 조회를 무시하고 특정 IP로 강제 연결

2. 기본 사용 예시

GET 요청 보내기

curl http://blog.scbyun.com

POST 요청 보내기

curl -X POST -d "param1=value1&param2=value2" http://blog.scbyun.com

파일 다운로드

curl -O http://blog.scbyun.com/file.zip

HTTP 헤더 설정

curl -H "Content-Type: application/json" http://blog.scbyun.com/api

인증서를 사용한 HTTPS 요청

curl --cert /tmp/cert.pem --key /tmp/key.pem https://blog.scbyun.com

3. 프록시 사용

프록시를 사용하지 않도록 설정(–noproxy)

curl --noproxy example.com http://blog.scbyun.com
curl --noproxy localhost,127.0.0.1 http://blog.scbyun.com
  • 특정 호스트만 프록시를 우회해야 할 때 유용합니다.

프록시 서버 사용(–proxy, -x)

curl --proxy http://proxy-server:8080 http://blog.scbyun.com
curl -x proxy.example.com:8080 -U username:password http://blog.scbyun.com

4. DNS 강제 지정(–resolve)

DNS를 변경하지 않고 특정 IP로 요청을 보내야 할 때 사용합니다.

curl --resolve example.com:80:203.0.113.1 http://blog.scbyun.com
  • 테스트 서버 점검, 로드밸런서 우회 테스트 시 매우 유용합니다.

5. 스크립트 다운로드 활용

스크립트만 다운로드

curl -Ssf https://blog.scbyun.com/scripte/webconfsync.sh -o /tmp/webconfsync.sh

스크립트 다운로드 후 즉시 실행

curl -Ssf https://blog.scbyun.com/scripte/webconfsync.sh | bash -x
  • -Ssf 옵션 조합은 에러 발생 시 즉시 실패하도록 만들어 자동화에 적합합니다.

wget 을 사용한 동일한 방식

wget -qO - https://blog.scbyun.com/scripte/webconfsync.sh | bash -x

6. HTTP 상태 코드 확인

curl -s -o /dev/null -w '%{http_code}\n' https://blog.scbyun.com
200

7. 웹사이트 응답 속도 측정

간단한 응답 시간 측정

curl -s -w '\nTesting Website Response Time for: %{url_effective}\n\nLookup Time:\t\t%{time_namelookup}\nConnect Time:\t\t%{time_connect}\nPre-transfer Time:\t%{time_pretransfer}\nStart-transfer Time:\t%{time_starttransfer}\n\nTotal Time:\t\t%{time_total}\n' -o /dev/null https://blog.scbyun.com
Testing Website Response Time for: https://blog.scbyun.com/

Lookup Time:		0.004
Connect Time:		0.007
Pre-transfer Time:	0.208
Start-transfer Time:	0.526

Total Time:		0.529
포맷 파일을 이용한 상세 측정

curl-format.txt 생성

cat <<EOF > curl-format.txt
time_namelookup: %{time_namelookup}
time_connect: %{time_connect}
time_appconnect: %{time_appconnect}
time_pretransfer: %{time_pretransfer}
time_redirect: %{time_redirect}
time_starttransfer: %{time_starttransfer}
----------
time_total: %{time_total}
EOF

포맷 파일 적용

curl -w "@curl-format.txt" -o /dev/null -s https://blog.scbyun.com
time_namelookup: 0.004
time_connect: 0.007
time_appconnect: 0.210
time_pretransfer: 0.210
time_redirect: 0.000
time_starttransfer: 0.488
----------
time_total: 0.488

특정 IP로 HTTPS 성능 테스트

curl -w "@curl-format.txt" -o /dev/null -s \
--resolve blog.scbyun.com:443:111.111.111.111 \
https://blog.scbyun.com

9. 반복 측정으로 평균 체감 확인

for i in {1..3}; do
curl -s -w "%{time_total}\n" -o /dev/null https://blog.scbyun.com
done
0.689
0.566
0.737

참고URL

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

Scroll to top