CURL का आउटपुट कैसे प्राप्त करें?


70

मुझे SSL प्रमाणपत्र की समाप्ति तिथि को पुनः प्राप्त करना होगा। curlआवेदन यह जानकारी प्रदान करता है:

$ curl -v https://google.com/
* Hostname was NOT found in DNS cache
*   Trying 212.179.180.121...
* Connected to google.com (212.179.180.121) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server key exchange (12):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using ECDHE-ECDSA-AES128-GCM-SHA256
* Server certificate:
*        subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=*.google.com
*        start date: 2014-10-22 13:04:07 GMT
*        expire date: 2015-01-20 00:00:00 GMT
*        subjectAltName: google.com matched
*        issuer: C=US; O=Google Inc; CN=Google Internet Authority G2
*        SSL certificate verify ok.
> GET / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: google.com
> Accept: */*
> 
< HTTP/1.1 302 Found
< Cache-Control: private
< Content-Type: text/html; charset=UTF-8
< Location: https://www.google.co.il/?gfe_rd=cr&ei=HkxbVMzCM-WkiAbU6YCoCg
< Content-Length: 262
< Date: Thu, 06 Nov 2014 10:23:26 GMT
* Server GFE/2.0 is not blacklisted
< Server: GFE/2.0
< 
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="https://www.google.co.il/?gfe_rd=cr&amp;ei=HkxbVMzCM-WkiAbU6YCoCg">here</A>.
</BODY></HTML>
* Connection #0 to host google.com left intact

हालांकि, जब grepपरिणाम के माध्यम से आउटपुट को पाइप करना स्क्रीन पर कम जानकारी नहीं है , बल्कि बहुत अधिक है :

$ curl -v https://google.com/ | grep expire
* Hostname was NOT found in DNS cache
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 212.179.180.84...
* Connected to google.com (212.179.180.84) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):
} [data not shown]
* SSLv3, TLS handshake, Server hello (2):
{ [data not shown]
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* SSLv3, TLS handshake, CERT (11):
{ [data not shown]
* SSLv3, TLS handshake, Server key exchange (12):
{ [data not shown]
* SSLv3, TLS handshake, Server finished (14):
{ [data not shown]
* SSLv3, TLS handshake, Client key exchange (16):
} [data not shown]
* SSLv3, TLS change cipher, Client hello (1):
} [data not shown]
* SSLv3, TLS handshake, Finished (20):
} [data not shown]
* SSLv3, TLS change cipher, Client hello (1):
{ [data not shown]
* SSLv3, TLS handshake, Finished (20):
{ [data not shown]
* SSL connection using ECDHE-ECDSA-AES128-GCM-SHA256
* Server certificate:
*        subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=*.google.com
*        start date: 2014-10-22 13:04:07 GMT
*        expire date: 2015-01-20 00:00:00 GMT
*        subjectAltName: google.com matched
*        issuer: C=US; O=Google Inc; CN=Google Internet Authority G2
*        SSL certificate verify ok.
> GET / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: google.com
> Accept: */*
> 
< HTTP/1.1 302 Found
< Cache-Control: private
< Content-Type: text/html; charset=UTF-8
< Location: https://www.google.co.il/?gfe_rd=cr&ei=IkxbVMy4K4OBbKuDgKgF
< Content-Length: 260
< Date: Thu, 06 Nov 2014 10:23:30 GMT
* Server GFE/2.0 is not blacklisted
< Server: GFE/2.0
< 
{ [data not shown]
100   260  100   260    0     0    714      0 --:--:-- --:--:-- --:--:--   714
* Connection #0 to host google.com left intact

मैं संदेह है कि curlपता लगाता है कि यह एक टर्मिनल पर प्रिंट नहीं है और इस प्रकार विभिन्न उत्पादन, जिनमें से सभी नहीं द्वारा मान्यता प्राप्त है देता है grepके रूप में किया जा रहा है stdoutऔर इस तरह टर्मिनल के लिए के माध्यम से पारित कर दिया है। हालाँकि, इस के लिए निकटतम चीज जो मुझे मिल सकती है man curl (उसके लिए कभी भी Google!) यह नहीं है:

PROGRESS METER
   curl  normally  displays  a  progress meter during operations, indicating the amount of transferred data, transfer speeds and estimated time
   left, etc.

   curl displays this data to the terminal by default, so if you invoke curl to do an operation and it is about to write data to the  terminal,
   it disables the progress meter as otherwise it would mess up the output mixing progress meter and response data.

   If you want a progress meter for HTTP POST or PUT requests, you need to redirect the response output to a file, using shell redirect (>), -o
   [file] or similar.

   It is not the same case for FTP upload as that operation does not spit out any response data to the terminal.

   If you prefer a progress "bar" instead of the regular meter, -# is your friend.

मैं आउटपुट expiryसे सिर्फ लाइन कैसे प्राप्त कर सकता हूं curl? इसके अलावा, स्थिति को बेहतर समझने के लिए मुझे क्या पढ़ना चाहिए?

ऐसा लगता है कि एक "stdmeta" फ़ाइल डिस्क्रिप्टर के लिए एक अच्छा उपयोग मामला होगा ।



@EladKarako: धन्यवाद। यह एक अच्छा जवाब है, लेकिन यह इस स्थिति पर लागू नहीं है। शायद विंडोज शेल में अलग आउटपुट फ़ाइल हैंडलर की कोई अवधारणा नहीं है, लेकिन लिनक्स के गोले में यह एक अलग मामला है।
dotancohen

1
मैं हवा और गूगल मैन कर्ल के लिए सावधानी फेंकने के लिए मजबूर महसूस करता हूं। मुझे यकीन है कि कुछ सार्थक परिणाम वापस करने के लिए बाध्य है।
जेरेमीकैनफील्ड

जवाबों:


105

कर्ल स्टडर को आउटपुट लिखता है, इसलिए उसे रीडायरेक्ट करता है और प्रगति को भी दबाता है:

curl -v --silent https://google.com/ 2>&1 | grep expire

curlStderr को जानकारी लिखने का कारण यह है कि आप ऐसा कर सकते हैं:
curl <url> | someprgramउस जानकारी के बिना इनपुट के बारे में जानकारी देनाsomeprogram



2
-vआप के लिए कोई ज़रूरत नहीं है एक बहुत पाठ पाइप होगा .. विशेष रूप से कई पुनर्निर्देशन और एसएसएल हैंडशेक और प्रमाणपत्र विनिमय पर !! आपको या तो --includeबस हेडर को प्रतिक्रिया के शरीर में जोड़ने के लिए उपयोग करना चाहिए , या बेहतर अभी तक उपयोग करना चाहिए --head, साथ ही, आप \rअंत में बेहतर तरीके से निकालते हैं यदि आप एक चर में मूल्य को स्टोर करने की योजना बना रहे हैं या इसे बाद में आउटपुट पर भेज सकते हैं (मेरा देखें Content-Lengthउदाहरण के साथ ऊपर टिप्पणी )

19

--stderr -पैरामीटर के रूप में उपयोग करना संभव है , आउटपुट को stderr (डिफ़ॉल्ट) से stdout में पुनर्निर्देशित करना। इस विकल्प के साथ आपको --silentप्रगति पट्टी को दबाने के लिए भी उपयोग करना चाहिए ।

$ curl -v --silent https://google.com/ --stderr - | grep expire
*    expire date: 2015-09-01 00:00:00 GMT
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.