In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use the curl command in Linux, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
Linux common command curl command is a file transfer tool that uses URL rules to work under the command line. It supports uploading and downloading of files, so it is an integrated transfer tool, but traditionally, curl is called a download tool.
A file transfer tool that curl uses URL rules to work on the command line
As a powerful tool, curl supports HTTP, HTTPS, ftp and many other protocols, as well as POST, cookies, authentication, downloading some files from the specified offset, user agent string, speed limit, file size, progress bar and other features. Curl can lend a helping hand to automate web page processing and data retrieval.
Syntax curl (option) (parameter) option-when a/-append uploads a file, attach to the target file-A/-user-agent sets the user agent to send to the server
Undefined
-anyauth can use "any" authentication method undefined-b/-cookiecookie string or file read location undefined-basic use HTTP basic authentication undefined-B/-use-ascii use ASCII / text transfer undefined-c/-cookie-jar operation write cookie to this file after the end of undefined-C/-continue-at breakpoint continuation undefined-d/-dataHTTP POST transfer data undefined-data-ascii post data undefined in ascii mode -data-binary binary post data undefined-negotiate uses HTTP authentication undefined-digest uses digital authentication undefined-disable-eprt forbids the use of EPRT or LPRTundefined-disable-epsv prohibits the use of EPSVundefined-D/-dump-header to write header information to this file undefined-egd-file sets the EGD socket path for random data (SSL) undefined-tcp-nodelay uses the TCP_NODELAY option undefined-e/-referer source URL undefined-E/- Cert cert: [passwd] client certificate file and password (SSL) undefined-cert-type certificate file type (DER/PEM/ENG) (SSL) undefined-key private key file name (SSL) undefined-key-type private key file type (DER/PEM/ENG) (SSL) undefined-pass private key password (SSL) undefined-engine encryption engine uses (SSL). "- engine list" for listundefined-cacertCA certificate (SSL) undefined-capathCA directory (made using c_rehash) to verify peer against (SSL) undefined-ciphersSSL password undefined-compressed request to return is compressed situation (using deflate or gzip) undefined-connect-timeout set maximum request time undefined-create-dirs establish directory hierarchy of local directory undefined-crlf upload is to convert LF to CRLFundefined-f/-fail connection failure does not show http error undefined -ftp-create-dirs if the remote directory does not exist Create a remote directory undefined-ftp-method [multicwd/nocwd/singlecwd] to control the use of undefined-ftp-pasv when using undefined-ftp-pasv instead of port undefined-ftp-skip-pasv-ip to use PASV Ignore the IP address undefined-ftp-ssl attempts to use SSL/TLS for ftp data transmission undefined-ftp-ssl-reqd requires SSL/TLS for ftp data transmission undefined-F/-form simulation http form submission data undefined-form-string simulation http form submission data undefined-g/-globoff disable URL sequence and range to use {} and [] undefined-G/-get to send data undefined-H/-header from Define the length of header information passed to the server undefined-ignore-content-length ignored HTTP header information undefined-i/-include output includes header information undefined-I/-head only displays request header information undefined-j/-junk-session-cookies read file forward ignore session cookieundefined-interface use specified network interface / address undefined-krb4 use specified security level of krb4undefined-k/-insecure to allow no certificate to SSL site undefined- K/-config specified profile read undefined-l/-list-only lists file names under the ftp directory undefined-limit-rate sets transfer speed undefined-local-port forces the use of local port numbers undefined-m/-max-time sets maximum transfer time undefined-max-redirs sets maximum number of directories read undefined-max-filesize sets maximum total number of files downloaded undefined-M/-manual displays full manual undefined-n/- Netrc reads username and password from netrc file undefined-netrc-optional overrides it with .netrc or URL-nundefined-ntlm uses HTTP NTLM authentication undefined-N/-no-buffer disables buffering output undefined-o/-output writes output to this file undefined-O/-remote-name writes output to this file Keep the file name of the remote file undefined-p/-proxytunnel use HTTP agent undefined-proxy-anyauth choose any agent authentication method undefined-proxy-basic on the agent use basic authentication undefined-proxy-digest on the agent use digital authentication undefined-proxy-ntlm on the agent use ntlm authentication undefined-P/-ftp-port enable port address instead of using PASVundefined-q as the first parameter Before closing the .curlrcundefined-Q/-quote file transfer, send a command to the server undefined-r/-range to retrieve random files from HTTP/1.1 or FTP server byte range undefined-range-file read (SSL) undefined-R/-remote-time when generating files locally, when there is a problem with the remote file transfer time undefined-retry transfer, the number of retries when there is a problem with undefined-retry-delay transfer Set retry interval when there is a problem with undefined-retry-max-time transfer, set the maximum retry time undefined-s/-silent silent mode. Do not output anything undefined-S/-show-error display error undefined-socks4 host [: port] use socks4 proxy given host and port undefined-socks5 host [: port] use socks5 proxy given host and port undefined-stderrundefined-t/-telnet-optionTelnet option set undefined-trace to debugundefined-trace-asciiLike the specified file debugundefined-trace-asciiLike-trace but no hex output undefined-trace-time trace / detailed output Add timestamp undefined-T/-upload-file upload file undefined-urlSpet URL to work withundefined-u/-user user [: password] set the user and password of the server undefined-U/-proxy-user user [: password] set the proxy user name and password undefined-w/-write-out [format] what is the output after undefined-x/-proxy host [: port] use HTTP proxy undefined-X/-request on a given port to specify what life The time it takes for undefined-y/-speed-time to give up the speed limit Default is 30undefined-Y/-speed-limit stop transfer speed limit, speed time undefined instance file download
The curl command can be used to perform downloads, send various HTTP requests, specify HTTP headers, and so on. If the system does not have curl, you can install it using yum install curl, or you can download and install it. Curl outputs the download file to stdout, the progress information to stderr, and does not display progress information using the-- silent option.
The command curl URL-- silent outputs the download file to the terminal, and all downloaded data is written to stdout.
Use the option-O to write the downloaded data to a file, and you must use the absolute address of the file:
Curl http://example.com/text.iso-- silent-O option-o writes the download data to a file with the specified name and uses-- progress to display the progress bar:
Curl http://example.com/test.iso-o filename.iso-- progress # # 100.0% does not output error and progress information
The-s parameter will not output error and progress information.
The above command curl-s https://www.example.com # does not display an error message once an error occurs. If no error occurs, the running result will be displayed normally. If you want curl to produce no output, you can use the following command.
Curl-s-o / dev/null https://google.com breakpoint continuation
Curl can continue to download from a specific file offset, and it can download some files by specifying a cheap amount:
Curl URL/File-C offset # offset is an integer in bytes. If you let curl automatically infer the correct continuation position, use-C -: curl-C-URL to set the reference page string using curl
The reference page is a string located in the HTTP header to indicate from which page the user arrived at the current page. If the user clicks on a connection in page A, the user will jump to page B, and the reference page string in the header of page B contains the URL of page A.
Use the-- referer option to specify the reference page string:
Curl-- referer http://www.google.com http://wangchujiang.com sets the user agent string with curl
Some Web sites are prompted to be accessed only using IE browsers, because these sites have a check user agent, which can be set to IE using curl, so that they can be accessed. Use the-- user-agent or-An option:
Curl URL-user-agent "Mozilla/5.0" curl URL-A "Mozilla/5.0" other HTTP header information can also be sent using curl, using-H "header message" to pass multiple header information, for example:
Bandwidth control and download quota for curl-H "Host:wangchujiang.com"-H "accept-language:zh-cn" URLcurl
Use-- limit-rate to limit the download speed of curl:
Curl URL-the download speed limit is specified with k (kilobytes) and m (megabytes) in the limit-rate 50k command.
Use-- max-filesize to specify the maximum file size that can be downloaded:
Curl URL-- max-filesize bytes if the file size exceeds the limit, the command returns a non-zero exit code, or 0 if the command is normal.
Curl-- limit-rate 200k https://example.com # the above command limits bandwidth to 200K bytes per second. Authentication with curl
Use the curl option-u to complete the authentication of HTTP or FTP. You can specify a password, or enter a password in subsequent operations without specifying a password:
Curl-u user:pwd http://wangchujiang.com curl-u user http://wangchujiang.com prints only response header information
With-I or-head, you can print only the HTTP header information:
[root@localhost text] # curl-I http://wangchujiang.com HTTP/1.1 200 OK Server: nginx/1.2.5 date: Mon, 10 Dec 2012 09:24:34 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive Vary: Accept-Encoding X-Pingback: http://wangchujiang.com/xmlrpc.phpget request
Curl "http://www.wangchujiang.com" # if the URL here points to a file or a picture, you can download it directly to the local curl-I" http://www.wangchujiang.com" # display all the information curl-l "http://www.wangchujiang.com" # display only the header information curl-v" http://www.wangchujiang.com" # display the whole process of get request parsing post request
The parameter of $curl-d "param1=value1¶m2=value2"http://www.wangchujiang.com/login" curl-dined loginencoded emmaxed password123'- X POST https://wangchujiang.com/login # or $curl-d'login=emma'- d' password=123'-X POST https://wangchujiang.com/login--data-urlencode is equivalent to-d, the data body that sends the POST request, the difference is that the data sent is automatically URL encoded.
Curl-- data-urlencode 'comment=hello world' https://wangchujiang.com/login # in the above code, there is a space between the sent data hello world, which needs to be URL encoded. Read the data from the local text file and send it to the server
Curl-d'@ data.txt' https://wangchujiang.com/upload # reads the contents of the data.txt file and sends it to the server as a data body. Post request in json format
Curl-l-H "Content-type: application/json"-X POST-d'{"phone": "13521389587", "password": "test"} 'http://wangchujiang.com/apis/users.json sends Cookie to the server
Use the-- cookie "COKKIES" option to specify cookie, and multiple cookie are separated by semicolons:
Curl http://wangchujiang.com-- cookie "user=root;pass=123456" saves cookie as a file, using the-- cookie-jar option:
The curl URL-- cookie-jar cookie_file-b parameter is used to send Cookie to the server.
The above command curl-b 'foo=bar' https://taobao.com # generates a header Cookie: foo=bar and sends a Cookie named foo with a value of bar to the server. Curl-b 'foo1=bar'-b' foo2=baz' https://taobao.com # the above command sends two Cookie. ````shell curl-b cookies.txt https://www.taobao.com # the above command reads the local file cookies.txt, which contains the Cookie set by the server (see-c parameter), and sends it to the server. Cookie writes to a file
Curl-c cookies.txt https://www.taobao.com # the above command writes the Cookie set by the server's HTTP response to the text file cookies.txt. The source of the request
The-e parameter is used to set the header Referer of the HTTP, indicating the source of the request.
Curl-e 'https://taobao.com?q=example' https://www.example.com # the above command sets the Referer header to https://taobao.com?q=example. The-H parameter can achieve the same effect by directly adding the header Referer.
Curl-H 'Referer: https://taobao.com?q=example' https://www.example.com uploads binary files
The-F parameter is used to upload binaries to the server.
Curl-F 'file=@photo.png' https://taobao.com/profile # the above command adds the header Content-Type: multipart/form-data to the HTTP request, and then uploads the file photo.png as a file field. The-F parameter can specify the MIME type.
Curl-F 'file=@photo.png;type=image/png' https://taobao.com/profile # the above command specifies that the MIME type is image/png, otherwise curl will set the MIME type to application/octet-stream. The-F parameter can also specify a file name.
In the above command of curl-F 'file=@photo.png;filename=me.png' https://taobao.com/profile #, the original file name is photo.png, but the file name received by the server is me.png. Set request header
The-H parameter adds the header of the HTTP request.
Curl-H 'Accept-Language: en-US' https://google.com # the above command adds the HTTP header Accept-Language: en-US. Curl-H 'Accept-Language: en-US'-H' Secret-Message: xyzzy' https://google.com # the above command adds two HTTP headers. Curl-d'{"login": "emma", "pass": "123"}'- H 'Content-Type: application/json' https://google.com/login # the above command adds the HTTP request with the header Content-Type: application/json, and then sends the JSON data with the-d parameter. Skip SSL detection
The above command curl-k https://www.example.com # does not check whether the server's SSL certificate is correct. Request to follow the redirection of the server
The-L parameter causes the HTTP request to follow the redirection of the server. Curl does not follow redirects by default.
Curl-L-d 'tweet=hi' https://api.example.com/tweet debug parameters
The-v parameter outputs the whole process of communication for debugging.
The curl-v https://www.example.com #-- trace parameter can also be used for debugging and outputs raw binary data. $curl-- trace-https://www.example.com to obtain the local public network ip
Curl ipecho.net/plain uses curl to test the website loading speed
The command has a little-known option,-w, which prints the statistics of this request to standard output after the request ends.
First, we define a format string that controls the printing behavior. Create a new text file fmt.txt and fill in the following:
\ n Response Time for:% {url_effective}\ n\ n DNS Lookup Time:\ t% {time_namelookup} s\ n Redirection Time:\ t\ t% {time_redirect} s\ n Connection Time:\ t\ t% {time_connect} s\ n App Connection Time:\ t\ t% {time_appconnect} s\ n Pre-transfer Time:\ t\ t% {time_pretransfer} s\ n Start-transfer Time:\ t\ t% {time_starttransfer} s\ n\ N Total Time:\ t\ t\ t% {time_total} s\ ncurl provides many permutation variables Can be used in the format string in the form of% {var}. The complete list of variables can be viewed in curl's manpage. A brief introduction to the variables we use:
Url_effective: the final URL after the address redirection
Time_namelookup: the time taken from the start of the request to the completion of the name resolution (in seconds), same as below
Time_redirect: time spent performing all redirects
Time_connect: time taken from the start of the request to the establishment of a TCP connection
Time_appconnect: the time taken from the start of the request to the completion of the SSL/SSH handshake
Time_pretransfer: the time taken from the request to the server to prepare the file for transfer, including the delivery negotiation time
Time_starttransfer: the time it takes from the request to the server to prepare to transfer the first byte
Time_total: complete and time-consuming.
Then execute the request, specifying the file with the format string saved through @ filename:
$curl-L-s-w @ fmt.txt-o / dev/null http://www.google.com output:
Response Time for: http://www.google.co.jp/?gfe_rd=cr&dcr=0&ei=cjIaWpTkHeiQ8QfnxYzoBA DNS Lookup Time: 0.000038s Redirection Time: 0.207271s Connection Time: 0.000039s App Connection Time: 0.000039s Pre-transfer Time: 0.000067s Start-transfer Time: 0.260115s Total Time: 0.467691s required to return the compressed status ▶ curl-- compressed-o-- L https: / / yarnpkg.com/install.sh | bash% Total% Received% Xferd Average Speed Time Current Dload Upload Total Spent Left Speed 54 100 54 00 42 00: 00:01 0:00:01 -::-42 100 2341 2341 00 1202 00: 00:01 0:00:01: --:-9289 Installing Yarn! > Downloading tarball... [1x2]: https://yarnpkg.com/latest.tar.gz-> / var/folders/j7/3xly5sk567s65ny5dnr__3b80000gn/T/yarn.tar.gz.XXXXXXXXXX.9hJsBsrA% Total% Received% Xferd Average Speed Time Current Dload Upload Total Spent Left Speed 10057 100 57 00 720 -: -:--: -:-- :-72 100 93 100 93 00 63 00: 00:01 0:00:01 -::-63 100 643 100 643 100 643 00 248 00: 00:02 0:00:02 -:-707 100 100 1215k 100 1215k 100 1215k 00 153k 00: 00:07 -:: -305k [2amp 2]: https://yarnpkg.com/latest.tar.gz.asc-> / var/folders/j7/3xly5sk567s65ny5dnr__3b80000gn/T/yarn.tar.gz.XXXXXXXXXX.9hJsBsrA.asc 100 61 100 61 00 356 0--:-356 100 97 100 97 00 325 0 -:-- -325 100 647 100 647 00 1283 0 -: 1283 100 832 100 832 00 1107 0 -:-812k Thank you for reading this article carefully I hope the article "how to use curl commands in Linux" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.