curl로 웹서버에 있는 파일 다운받는 방법

curl은 서버로부터 데이터를 받거나, 서버에 데이터를 전달하는 명령줄 도구입니다. 윈도우와 리눅스에서 모두 지원합니다. 비슷한 기능을 수행하는 툴로 wget이 있지만 curl은 더 다양한 프로토콜을 지원한다는 면에서 이점이 있습니다.

 

이 글에서는 curl을 통해 1. 서버와 통신이 되는지 확인하고 2. 서버에 있는 파일을 다운로드 받는 것 두 가지만 다루어보겠습니다. 모두 활용도가 높은 작업입니다.

curl

curl [options] [URL...]
  • curl 지원 프로토콜 : DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP,
    IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS,
    TELNET, TFTP

 

1. 통신 여부 확인 (방화벽 예외 확인)

curl로 특정 서버에 특정 포트에 대해 통신이 가능한지 확인할 수 있습니다. telnet과 비슷한 역할입니다.

curl -v URL
  • v : 더 자세한 정보를 출력합니다. 디버깅에 활용할 수 있습니다. >로 시작하는 라인은 curl에 의해 보내진 헤더 데이터라는 의미입니다. <는 curl이 받은 데이터라는 의미입니다. *는 참고할 수 있는 정보들을 표시합니다.

 

[예시]

구글 서버로 curl 명령어를 실행해보겠습니다. 내용이 좀 길지만 < 부분에 http 상태코드만 봐도 통신이 성공했는지 정도는 금새 확인할 수 있습니다. 

[root@localhost curl_test]# curl -v https://www.google.com:443
* About to connect() to www.google.com port 443 (#0)
*   Trying 172.217.25.164...
* Connected to www.google.com (172.217.25.164) 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=www.google.com
*       start date: Nov 28 08:19:01 2022 GMT
*       expire date: Feb 20 08:19:00 2023 GMT
*       common name: www.google.com
*       issuer: CN=GTS CA 1C3,O=Google Trust Services LLC,C=US
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.google.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Sun, 01 Jan 2023 04:29:58 GMT
< Expires: -1
< Cache-Control: private, max-age=0
< Content-Type: text/html; charset=ISO-8859-1
< Cross-Origin-Opener-Policy-Report-Only: same-origin-allow-popups; report-to="gws"
< Report-To: {"group":"gws","max_age":2592000,"endpoints":[{"url":"https://csp.withgoogle.com/csp/report-to/gws/other"}]}
< P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
< Server: gws
< X-XSS-Protection: 0
< X-Frame-Options: SAMEORIGIN
< Set-Cookie: 1P_JAR=2023-01-01-04; expires=Tue, 31-Jan-2023 04:29:58 GMT; path=/; domain=.google.com; Secure
< Set-Cookie: AEC=AakniGOg_iBv5nVsxF-cKJtVcse6exH6MBmPYM4aaEqY81A-CxT6WorgS4E; expires=Fri, 30-Jun-2023 04:29:58 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax
< Set-Cookie: NID=511=nzYvzKM8yGuy3BVw6s1D-hyNrw_kww2iiU7bhgoVXw6kUmwecWN9CfYE3Cf5aFN8BGLJxtxioz9VdPrOEG3Lss7ZiXoI4lkQGDH0kly2Xj6kfJxk1Q1f0JZjBBxz9587tN-y5ZEOyai3KfCBxU5BimjxraxOfnAczIMiqzpDfG0; expires=Mon, 03-Jul-2023 04:29:58 GMT; path=/; domain=.google.com; HttpOnly
< Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
< Accept-Ranges: none
< Vary: Accept-Encoding
< Transfer-Encoding: chunked
<

...(생략)

도메인에 연결된 주소는 172.217.25.164입니다. www.google.com 호스트에 http get 요청을 보내고 http 요청에 성공(200)한 것이 확인됩니다.

 

좀 더 간단한 요청과 응답을 받는 예시도 있습니다. www.openss7.org에 요청을 보냈습니다.

[root@localhost curl_test]# curl -v http://www.openss7.org
* About to connect() to www.openss7.org port 80 (#0)
*   Trying 142.59.210.7...
* Connected to www.openss7.org (142.59.210.7) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.openss7.org
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Sun, 01 Jan 2023 04:00:41 GMT
< Server: Apache/1.3.22 (Unix)  (Red-Hat/Linux) mod_python/2.7.8 Python/1.5.2 mod_ssl/2.8.5 OpenSSL/0.9.6b DAV/1.0.2 PHP/4.1.2 mod_perl/1.26 mod_throttle/3.1.2
< Last-Modified: Sat, 18 Oct 2014 12:50:11 GMT
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: text/html

...(생략)

 

2. 웹서버에 있는 파일 다운로드

curl로 웹서버에 있는 파일을 다운로드 받을 수 있습니다. O 또는 o 옵션을 이용합니다.

curl -O 프로토콜:도메인
curl -o [저장할이름] 프로토콜:도메인

 

[예시]

O 옵션은 원격 서버에 있는 파일명 그대로 현재 위치에 파일을 저장합니다.

[root@localhost webapps]# curl -O http://www.openss7.org/repos/tarballs/strx25-0.9.2.1.tar.bz2
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   339    0   339    0     0    288      0 --:--:--  0:00:01 --:--:--   288
[root@localhost curl_test]# ls
strx25-0.9.2.1.tar.bz2

 

o 옵션은 저장할 파일명을 지정할 수 있습니다.

[root@localhost curl_test]# curl -o curl_file.tar.bz2 http://www.openss7.org/repos/tarballs/strx25-0.9.2.1.tar.bz2
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   339    0   339    0     0    180      0 --:--:--  0:00:01 --:--:--   180
[root@localhost curl_test]# ls
curl_file.tar.bz2  strx25-0.9.2.1.tar.bz2

 

반응형

댓글

Designed by JB FACTORY