簡體   English   中英

如何從 cURL 輸出日志條目中獲取原始 URL?

[英]How to get original URL from the cURL output log entry?

我一次向 cURL 提供多個 URL,並且很難解析輸出日志以獲取原始地址。 即,如果 URL 解析,則輸出如下:

$ curl --head --verbose https://www.google.com/
*   Trying 64.233.165.106...
* TCP_NODELAY set
* Connected to www.google.com (64.233.165.106) port 443 (#0)
<...>
> HEAD / HTTP/2
> Host: www.google.com
<...>

最終可以解析回https://www.google.com/

但是,對於無效的 URL,它不會執行以下操作:

$ curl --head --verbose --connect-timeout 3 https://imap.gmail.com/
*   Trying 74.125.131.109...
* TCP_NODELAY set
* After 1491ms connect time, move on!
* connect to 74.125.131.109 port 443 failed: Operation timed out
<...>
* Failed to connect to imap.gmail.com port 443: Operation timed out

在這種情況下,錯誤消息包含 URL,但在其他情況下不包含。 我不能依賴它。

因此,我需要在輸出中禁用 URL 到 IP 解析,例如

*   Trying https://imap.gmail.com/...

或者以某種方式將列表中的每個 URL 附加到相應的輸出,例如:

$ curl --head --verbose --connect-timeout 3 https://imap.gmail.com/ https://www.google.com/

https://imap.gmail.com/
*   Trying 64.233.162.108...
* TCP_NODELAY set
* After 1495ms connect time, move on!
* connect to 64.233.162.108 port 443 failed: Operation timed out
<...>

https://www.google.com/
*   Trying 74.125.131.17...
* TCP_NODELAY set
* Connected to www.gmail.com (74.125.131.17) port 443 (#0)
<...>

Wget 或 HTTPie 不是一個選項。 如何使用 cURL 實現這一目標?

也許這是解決方案:

while read LINE ; do
    print "REQUESTED URL: $LINE" >> output.txt;
    curl $LINE >> output.txt 2>&1;
done < url-list.txt

從 curl v.7.75.0 開始,可以使用 --write --write-out '%{url}'選項使 curl 顯示獲取的 URL。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM