10 실시간 예제와 함께 cURL 명령 사용

cURL은 URL 구문을 사용하여 데이터를 가져오거나 보내는 명령줄 도구입니다.

개발자 또는 지원 기능으로 작업하는 경우 웹 응용 프로그램 문제를 해결하기 위해 cURL 명령 사용법을 알고 있어야 합니다. cURL은 Windows, MAC 및 UNIX에서 사용할 수 있는 크로스 플랫폼 유틸리티입니다.

다음은 도움이 되는 예제와 함께 가장 많이 사용되는 구문입니다.

URL에 연결할 수 있는지 확인

UNIX 시스템에서 작업 중이고 외부 URL에 연결을 시도하는 경우 가장 먼저 하고 싶은 것은 curl을 통해 URL에 액세스할 수 있는지 확인하는 것입니다.

컬 yoururl.com

어떤 출력도 던지지 않을 것입니다. 그러나 서버에 연결할 수 없으면 호스트를 확인할 수 없다는 오류가 발생합니다.

[[email protected] tmp]# curl helloitdoesntexist.com
curl: (6) Could not resolve host: helloitdoesntexist.com; Unknown error
[[email protected] tmp]#

URL/URI 출력을 파일에 저장

URL 또는 URI 내용을 특정 파일에 저장해야 하는 경우 다음 구문을 사용할 수 있습니다.

curl https://yoururl.com > yoururl.html

전:

[[email protected] tmp]# curl https://gf.dev > /tmp/gfhtml
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 18557    0 18557    0     0  72565      0 --:--:-- --:--:-- --:--:-- 72772
[[email protected] tmp]#

위의 예는 gf.dev의 모든 내용을 /tmp/gf.html에 저장합니다.

요청 및 응답 헤더 표시

문제가 있고 유효성을 검사하려는 경우 예상되는 요청 및 응답 헤더가 표시됩니다.

curl -v yoururl.com

전:

[[email protected] tmp]# curl -v https://koreantech.org.com
* About to connect() to koreantech.org.com port 443 (#0)
*   Trying 104.25.134.107...
* Connected to koreantech.org.com (104.25.134.107) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
* Server certificate:
* 	subject: CN=ssl371609.cloudflaressl.com,OU=PositiveSSL Multi-Domain,OU=Domain Control Validated
* 	start date: Nov 07 00:00:00 2019 GMT
* 	expire date: May 15 23:59:59 2020 GMT
* 	common name: ssl371609.cloudflaressl.com
* 	issuer: CN=COMODO ECC Domain Validation Secure Server CA 2,O=COMODO CA Limited,L=Salford,ST=Greater Manchester,C=GB
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: koreantech.org.com
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Sat, 09 Nov 2019 19:41:37 GMT
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Set-Cookie: __cfduid=d2ce6cd359ebc0b6eb5ff3a454ed042021573328497; expires=Sun, 08-Nov-20 19:41:37 GMT; path=/; domain=.koreantech.org.com; HttpOnly; Secure
< Vary: Accept-Encoding
< Link: <https://koreantech.org.com/wp-json/>; rel="https://api.w.org/"
< Link: <https://koreantech.org.com/>; rel=shortlink
< X-SRCache-Fetch-Status: HIT
< X-SRCache-Store-Status: BYPASS
< X-Frame-Options: SAMEORIGIN
< X-Powered-By: EasyEngine v4.0.12
< Via: 1.1 google
< CF-Cache-Status: DYNAMIC
< Strict-Transport-Security: max-age=15552000; preload
< X-Content-Type-Options: nosniff
< Alt-Svc: h3-23=":443"; ma=86400
< Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
< Server: cloudflare
< CF-RAY: 533243e4bcd4bbf4-LHR
<

제한된 속도로 다운로드

최적화 작업을 하고 있고 특정 속도로 다운로드하는 데 시간이 얼마나 걸리는지 확인하려면 다음을 수행할 수 있습니다.

curl –-limit-rate 2000B

전:

curl –-limit-rate 2000B https://gf.dev

프록시를 사용하여 연결

프록시를 사용하여 외부 세계에 연결해야 하는 DMZ 서버에서 작업하는 경우 매우 편리합니다.

curl --proxy yourproxy:port https://yoururl.com

데이터와 함께 헤더를 삽입하여 curl을 사용하여 특정 문제를 테스트하거나 해결할 수 있습니다. Content-Type으로 요청하는 다음 예를 살펴보겠습니다.

curl --header 'Content-Type: application/json' http://yoururl.com

위와 같이 하면 요청 헤더에 Content-Type을 application/json으로 전달하도록 curl에 요청하게 됩니다.

  SnapChat에서 지도를 보는 방법

문제 해결을 수행하고 응답 헤더를 빠르게 확인하려는 경우 다음 구문을 사용할 수 있습니다.

curl --head http://yoururl.com

전:

[[email protected] tmp]# curl --head https://chandan.io
HTTP/1.1 200 OK
Date: Sat, 09 Nov 2019 19:51:23 GMT
Content-Type: text/html
Connection: keep-alive
Set-Cookie: __cfduid=d3cb2c7b8e566ad99c870b0af12b0f1eb1573329083; expires=Sun, 08-Nov-20 19:51:23 GMT; path=/; domain=.chandan.io; HttpOnly
X-GUploader-UploadID: AEnB2Uo96JhvJmR2zYUL-Ndh2ta3UD_ykQAB5C7O8cjZQhCf-GxHQ0MsodSzRnl3guSN3ywAYNjtWcPXfwDXjLg3bQ-P5vQMOA
Expires: Sat, 09 Nov 2019 20:51:23 GMT
Cache-Control: public, max-age=3600
Last-Modified: Mon, 06 Aug 2018 10:45:47 GMT
x-goog-generation: 1533552347482034
x-goog-metageneration: 1
x-goog-stored-content-encoding: identity
x-goog-stored-content-length: 24620
x-goog-hash: crc32c=DpDPAQ==
x-goog-hash: md5=cIP/3rusdUx12Zla1kf1yA==
x-goog-storage-class: MULTI_REGIONAL
Accept-Ranges: bytes
CF-Cache-Status: DYNAMIC
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
CF-RAY: 53325234dc2fbb9a-LHR

[[email protected] tmp]#

HTTPS/SSL URL을 연결하고 SSL 인증서 오류를 무시합니다.

SSL/TLS 인증서 보안 URL에 액세스하려고 할 때 잘못된 인증서가 있거나 CN이 일치하지 않으면 다음 오류가 발생합니다.

curl: (51) Unable to communicate securely with peer: requested domain name does not match the server's certificate.

좋은 소식입니다. -insecure 플래그를 사용하여 인증서 오류를 무시하도록 cURL에 지시할 수 있습니다.

curl --insecure https://yoururl.com

특정 프로토콜(SSL/TLS)을 사용하여 연결

특정 URL이 특정 SSL/TLS 프로토콜을 통해 핸드셰이크할 수 있는지 테스트하는 데 매우 편리합니다.

SSL v3를 사용하여 연결하려면

curl --sslv3 https://yoururl.com

다른 TLS 버전의 경우

curl --tlsv1 https://example.com
curl --tlsv1.0 https://example.com
curl --tlsv1.1 https://example.com
curl --tlsv1.2 https://example.com
curl --tlsv1.3 https://example.com

FTP 서버에서 파일 다운로드

사용자 이름과 비밀번호를 지정하여 curl을 사용하여 파일을 다운로드할 수도 있습니다.

curl -u user:password -O ftp://ftpurl/style.css

모든 구문과 함께 “-v”를 사용하여 자세한 정보 표시 모드로 인쇄할 수 있습니다.

호스트 헤더는 호스트 헤더가 일치할 때만 요청된 콘텐츠를 사용할 수 있는 경우 IP를 통한 대상 URL을 테스트하는 데 유용합니다. 또는 로드 밸런서 IP/URL을 사용하여 애플리케이션을 테스트하려는 경우.

curl --header 'Host: targetapplication.com' https://192.0.0.1:8080/

온라인에서 cURL을 사용하는 것은 어떻습니까?

예, 다음 도구를 사용하면 가능합니다. 원격으로 cURL을 실행할 수 있습니다.

온라인 CURL – URL을 온라인으로 가져오고 다음 옵션을 추가할 수 있는 가벼운 도구입니다.

--connect-timeout
--cookie
--data
--header
--head
--location
--max-time
--proxy
--request
--user
--url
--user-agent
 

cURL 명령줄 빌더 – 이것은 다릅니다. 멋진 UI로 정보를 입력할 수 있는 curl 명령어를 빌드할 수 있도록 도와주고, 하단에 cURL 명령어가 나옵니다.

  iPhone 홈 화면에서 위젯 스택을 만드는 방법

cURL은 실시간 연결 문제를 해결하는 데 유용한 유틸리티이며 위의 내용이 도움이 되었기를 바랍니다. 더 자세히 알고 싶다면 추천합니다 Linux 명령줄 기본 사항 온라인 코스.