Linux Curl Command

Source: Internet
Author: User
Tags http authentication http post

Command: Curl

In Linux, Curl is a file transfer tool that works under the command line using URL rules, which can be said to be a powerful HTTP command-line tool. It supports file upload and download, is a comprehensive transfer tool, but by tradition, the custom called URL for download tool.

Syntax: # curl [option] [url]

Common parameters:

-a/--user-agent <string>Set up user agent to send to server-b/--cookie <name=string/file>Cookie string or file read location-c/--cookie-jar <file>Write the cookie to this file after the operation is complete-c/--Continue-at <offset> breakpoint Continuation-d/--dump-header <file> Write header information to the file-e/--referer Source URL- f/--failconnection fails with HTTP error-o/--output writes outputs to the file-o/--remote-name writes the output to the file, retains the file name of the remote files-r/--range <range > Retrievessilent mute mode from HTTP/1.1 or FTP server byte range-s/--. Do not output anything-t/--upload-file <file> upload files-u/--user <user[:p assword]> Set the server user and password-w/--write-out [format] What output is completed-x/--proxy  current delivery status is displayed using the HTTP proxy-#/--progress-bar progress bar on a given port  

Example:
1. Basic usage

# Curl http://www.linux.com 

Once executed, the www.linux.com HTML will be displayed on the screen.
Ps: Since the installation of Linux is often not installed on the desktop, also means that there is no browser, so this method is frequently used to test whether a server can reach a website

2. Save the visited Web page
2.1: Save using the redirection feature of Linux

# Curl http:///www.linux.com >> linux.html 

2.2: You can use Curl's built-in option:-o (lowercase) to save Web pages

$ Curl-o linux.html http://www.linux.com 

After execution, the following screen appears, displaying 100% indicating a successful save

% Total    % Received% xferd Average speed time time current                                dload  Upload  Total spent    left  Speed100 79684    0 79684    0    0  3437k      0--:--:--- -:--:----:--:--7781k

2.3: You can use Curl's built-in option:-O (uppercase) to save files in a Web page
Note that the URL behind this is specific to a file, or you can't catch it.

# Curl-o http://www.linux.com/hello.sh 

3. Test page return value

# Curl-o/dev/null-s-W%{http_code} www.linux.com

Ps: In the script, it is very common to test whether the website is in normal use

4. Specify proxy server and its port
Most of the time the Internet needs to use a proxy server (such as using a proxy server to surf the Internet or because of the use of curl other people's websites to block the IP address), fortunately, curl by using the built-in option:-x to support the setup agent

# curl-x 192.168.100.100:1080 http://www.linux.com 

5. Cookies
Some websites use cookies to record session information. Cookies can be easily processed by browsers such as Chrome, but it is easy to process cookies in curl by adding relevant parameters.
5.1: Save the cookie information inside the HTTP response. Built-in option:-c (lowercase)

# curl-c Cookiec.txt  http://www.linux.com 

Cookie information is stored in cookiec.txt after execution.

5.2: Keep the header information inside the HTTP response. Built-in option:-D

# curl-d Cookied.txt http://www.linux.com 

Cookie information is stored in cookied.txt after execution.

Note: The cookie generated by-C (lowercase) and the-D cookie are not the same.


5.3: Using Cookies
Many websites monitor your cookie information to see if you have access to their website as a rule, so we need to use stored cookie information. Built-in option:-B

# curl-b Cookiec.txt http://www.linux.com 

6, imitate the browser
Some websites need to use a specific browser to access them, and some need to use certain specific versions. Curl built-in option:-a allows us to specify a browser to access the site

# curl-a "mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0) "http://www.linux.com 

So the server side will be considered to be accessed using IE8.0.

7, forged Referer (hotlinking)
Many servers check HTTP access to Referer to control access. For example: You first visit the homepage, and then visit the home page of the mailbox, where access to the mailbox Referer address is to visit the page after the successful homepage address, if the server found on the mailbox page access to the Referer address is not the home address, it is concluded that it is a stolen
The built-in OPTION:-E in curl allows us to set Referer

# curl-e "www.linux.com" http://mail.linux.com 

This will make the server think that you have clicked a link from www.linux.com.

8. Download the file
8.1: Download the file using Curl.
#使用内置option:-O (lowercase)

# Curl-o Dodo1.jpg http:www.linux.com/dodo1.jpg

#使用内置option:-O (uppercase)

# Curl-o http://Www.linux.com/dodo1.JPG 

This will save the file on the server name to the local

8.2: Cyclic download
Sometimes download pictures can be the previous part of the name is the same, the last tailbone name is not the same

# Curl-o http://www.linux.com/dodo[1-5]. JPG 

This will save the DODO1,DODO2,DODO3,DODO4,DODO5 all.

8.3: Under load name

# Curl-o http://www.linux.com/{hello,bb}/dodo[1-5]. JPG 

Because the downloaded hello and the file name in the BB are dodo1,dodo2,dodo3,dodo4,dodo5. So the second download will overwrite the first download, so you need to rename the file.

# Curl-o #1_ #2.jpg http://www.linux.com/{hello,bb}/dodo[1-5]. JPG 

This is hello/dodo1. JPG files will become hello_dodo1 when downloaded. JPG, other files and so on, which effectively avoids overwriting the file

8.4: Sub-block download
Sometimes the download of things will be relatively large, this time we can download sections. Using the built-in Option:-r

# Curl-r 0-100-o Dodo1_part1. JPG http://www.linux.com/dodo1.JPG# curl-r 100-200-o dodo1_part2. JPG http://www.linux.com/dodo1.JPG# curl-r 200--o dodo1_part3. JPG http://www.linux.com/dodo1.JPG# cat dodo1_part* > Dodo1. JPG     

This will allow you to view the Dodo1. JPG's content.

8.5: Download files via FTP
Curl can download files via FTP, Curl provides two kinds of syntax to download from FTP

# Curl-o-u user name: password ftp://www.linux.com/dodo1. jpg# Curl-o ftp:// user name: Password @www.linux.com/dodo1. JPG   

8.6: Show Download progress bar

# Curl-#-O http://Www.linux.com/dodo1.JPG 

8.7: Download Progress information is not displayed

# curl-s-O http://Www.linux.com/dodo1.JPG 

9, the breakpoint continues to pass
In Windows, we can use software such as Thunderbolt to continue the breakpoint. Curl can also achieve the same effect with built-in option:-c
If you are downloading Dodo1. JPG in the process of a sudden drop off, you can use the following ways to continue to pass

# curl-c-O http://Www.linux.com/dodo1.JPG 

10. Uploading Files
Curl can not only download files, but also upload files. With built-in option:-t to achieve

# curl-t Dodo1. Jpg-u User name: password ftp://www.linux.com/img/ 

This uploads the file Dodo1 to the FTP server. Jpg

11. Display Crawl Error

# curl-f http://www.linux.com/error 

Other parameters (translated here for reprint):

-a/--Append when uploading a file, attach to the target file--Anyauth can use "any" authentication Method--Basic Authentication with HTTP-b/--use-ASCII text transmission-d/--data <data>HTTP Post mode transfer data--data-ascii <data>Post data in ASCII mode--data-binary <data>Post data in binary mode--Negotiate using HTTP authentication--Digest using digital authentication--disable-EPRT prohibit the use of eprt or lprt--disable-EPSV Forbidden to use Epsv--egd-file <file>Set the EGD socket path for random data (SSL)--tcp-Nodelay using the tcp_nodelay option-e/--cert <cert[:p asswd]>Client certificate file and password (SSL)--cert-type <type> certificate file type (der/pem/ENG) (SSL)--key <key>Private key Name (SSL)--key-type <type> private key file type (der/pem/ENG) (SSL)--pass <pass>The private key password (SSL)--engine <eng> encryption engine is used (SSL). "--engine List"ForList--cacert <file>CA certificate (SSL)--capath <directory>CA mesh (made using C_rehash) to verify peer against (SSL)--ciphers <list>SSL Password--Compressed requirements return is a compressed situation (using deflate or gzip)--connect-timeout <seconds>Set Maximum request time--create-Dirs Creating a directory hierarchy of local directories--CRLF upload is to convert LF into crlf--ftp-create-dirs If the remote directory does not exist, create a remote directory--ftp-method [multicwd/nocwd/SINGLECWD] Controlling the use of CWD--FTP-PASV using pasv/EPSV instead of Port--ftp-skip-pasv-When IP is using PASV, ignore the IP address--ftp-ssl try to use ssl/TLS for FTP data transfer--FTP-SSL-REQD requirements with ssl/TLS for FTP data transfer-f/--form <name=content>Analog HTTP form submission data-form-string <name=string>Analog HTTP form submission data-g/--Globoff disables URL sequences and ranges using {} and []-g/--Get sends data in a get way-h/--Help-h/--header <line>Custom header information is passed to the server--ignore-content-Length of the HTTP header information ignored-i/--Include protocol header information when included output-i/--Head only displays document information-j/--junk-session-Cookies Ignore session cookie--when reading filesInterface <Interface> using the specified network interface/Address--krb4 <level>krb4-k/--with the specified security levelInsecure allow the use of certificates to SSL sites-k/--Config profile read-l/--list-Only list the file names under the FTP directory--limit-rate <rate>Set the transfer speed--local-port<num>Force local port number-m/--max-time <seconds>Set maximum transfer time--max-redirs <num>Set the maximum number of read directories--max-filesize <bytes>Set the maximum amount of files to download-m/--Manual Display full Manual-n/--NETRC read the user name and password from the Netrc file--netrc-optional use. netrc or URL to overwrite-n--NTLM uses HTTP NTLM authentication-n/--no-Buffer Disable buffered output-p/--Proxytunnel using HTTP proxy--proxy-Anyauth Select either proxy authentication method--proxy-Basic authentication is used on the agent--proxy-Digest using digital authentication on the proxy--proxy-NTLM uses NTLM authentication on the proxy-p/--ftp-port <address>Use the port address instead of using Pasv-q/--quote <cmd>Before file transfer, send command to server--range-File read (SSL) for random files-r/--remote-Time to preserve remote file times when generating files locally--retry <num>Number of retries when there is a problem with transmission--retry-delay <seconds>Setting the retry interval when there is a problem with the transmission--retry-max-time <seconds>Set the maximum retry time when there is a problem with the transport-s/--show-Error--SOCKS4 Use the SOCKS4 proxy for a given host and port--socks5 Using SOCKS5 proxy for given host and Port-t/--telnet-option <OPT=val>Telnet option settings--trace <file>Debug--trace-ascii <file> like for a specified file--Trace but no hex output--trace-time tracking/When verbose output, add timestamp--url <URL> Spet URL to work With-u/--proxy-user <user[:p assword]>  set proxy user name and password-v/--version show version info-x/--request < Command> specify what command-y/--speed-time the time it takes to abandon the speed limit. The default is 30-y/--speed-limit stop transfer speed limit, speed time ' s-z/--time-cond transfer time settings -0/--http1.0 use HTTP 1.0-1 /--tlsv1 uses TLSV1 (SSL) -2/--sslv3 used SSLv3 (SSL)--3p-quote like-q for the source URL  3rd party transfer--3p-url use URLs for third parties to transfer--3p-user use user name and password for third-party transfer -4/--ipv4 use Ip4-6/--ipv6     

Transferred from: http://www.linuxdiyf.com/linux/2800.html

Linux Curl Command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.