不求甚解,curl命令如何终止断开连接退到命令提示符下?

通常情况下,可以通过以下curl命令打出一个域名连接的详细信息(包含ssl证书):

% curl -Iv --stderr - https://moneyslow.com        
*   Trying 123.123.80.65...
* TCP_NODELAY set
* Connected to moneyslow.com (123.123.80.65) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/cert.pem
  CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use h2
* Server certificate:
*  subject: CN=moneyslow.com
*  start date: Dec 19 00:00:00 2022 GMT
*  expire date: Dec 18 23:59:59 2023 GMT
*  subjectAltName: host "moneyslow.com" matched cert's "moneyslow.com"
*  issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=Encryption Everywhere DV TLS CA - G1
*  SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x7fdc2000ba00)
> HEAD / HTTP/2
> Host: moneyslow.com
> User-Agent: curl/7.64.1
> Accept: */*
> 
* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!
< HTTP/2 200 
HTTP/2 200 
< server: nginx/1.21.6
server: nginx/1.21.6
< date: Fri, 10 Mar 2023 11:30:43 GMT
date: Fri, 10 Mar 2023 11:30:43 GMT
< content-type: text/html; charset=UTF-8
content-type: text/html; charset=UTF-8
< link: <https://moneyslow.com/wp-json/>; rel="https://api.w.org/"
link: <https://moneyslow.com/wp-json/>; rel="https://api.w.org/"
< strict-transport-security: max-age=31536000; includeSubDomains
strict-transport-security: max-age=31536000; includeSubDomains

< 
* Connection #0 to host moneyslow.com left intact
* Closing connection 0

输出完成,就退回到shell界面了,但有些情况下,会僵死在curl命令结果下:

不求甚解,curl命令如何终止断开连接退到命令提示符下?
curl命令
添加了 超时 和 断掉连接 的相关参数:
curl --connect-timeout 2 -Iv --stderr - https://moneyslow.com -H "Connection: close"

由于某些原因,上图中必须通过Ctrl+C才能退出,如何退出到shell提示符下呢?原来curl还有--max-time 这个参数,意思为:Maximum time allowed for the transfer,即允许传输的最大时间,单位是秒。再次执行:

不求甚解,curl命令如何终止断开连接退到命令提示符下?
curl命令

这次可以退出到命令提示符了。

这对于把curl写在shell脚本里有好处,不会一直等待。

Maximum  time  in  seconds that you allow the whole operation to
take.  This is useful for preventing your batch jobs from  hang‐
ing  for  hours due to slow networks or links going down.  Since
7.32.0, this option accepts decimal values, but the actual time‐
out will decrease in accuracy as the specified timeout increases
in decimal precision.  See also the --connect-timeout option.
If this option is used several times, the last one will be used

您允许整个操作完成的最长时间(以秒为单位)。 这对于防止批处理作业挂起很有用。
由于网络速度慢或链接断开而持续数小时。 自从7.32.0,此选项接受十进制值,但实际时间-
随着指定超时的增加,out 的准确性会降低小数精度。 另请参阅 --connect-timeout 选项。
如果多次使用此选项,将使用最后一个。

参考链接:https://unix.stackexchange.com/questions/94604/does-curl-have-a-timeout

滚动至顶部