跳至主要内容

通过curl获取http状态码

通过curl获取网页当前的http状态码

操作实例如下:

  1. bbq@DESKTOP-SOR0NVK MINGW64 /d/gits/plan9 (main)
  2. $ curl -o /dev/null -s -w "%{http_code}" https://plan9.cn
  3. 200

说明:

  • -o 输出,定向到/dev/null
  • -s 静默模式,不显示过程
  • -w 格式化输出,相关说明可通过curl --help -w来查看
  1. bbq@DESKTOP-SOR0NVK MINGW64 /d/gits/plan9 (main)
  2. $ curl --help -w
  3. -w, --write-out <format>
  4. Make curl display information on stdout after a completed
  5. transfer. The format is a string that may contain plain text mixed
  6. with any number of variables. The format can be specified as a
  7. literal "string", or you can have curl read the format from a file
  8. with "@filename" and to tell curl to read the format from stdin
  9. you write "@-".
  10. The variables present in the output format are substituted by the
  11. value or text that curl thinks fit, as described below. All
  12. variables are specified as %{variable_name} and to output a normal
  13. % you just write them as %%. You can output a newline by using \n,
  14. a carriage return with \r and a tab space with \t.
  15. The output is by default written to standard output, but can be
  16. changed with %{stderr} and %output{}.
  17. Output HTTP header values from the transfer's most recent server
  18. response by using %header{name} where name is the case insensitive
  19. name of the header (without the trailing colon). The header
  20. contents are exactly as delivered over the network but with
  21. leading and trailing whitespace and newlines stripped off (added
  22. in 7.84.0).
  23. Select a specific target destination file to write the output to,
  24. by using %output{name} (added in curl 8.3.0) where name is the
  25. full filename. The output following that instruction is then
  26. written to that file. More than one %output{} instruction can be
  27. specified in the same write-out argument. If the filename cannot
  28. be created, curl leaves the output destination to the one used
  29. prior to the %output{} instruction. Use %output{>>name} to append
  30. data to an existing file.
  31. This output is done independently of if the file transfer was
  32. successful or not.
  33. If the specified action or output specified with this option fails
  34. in any way, it does not make curl return a (different) error.
  35. NOTE: On Windows, the %-symbol is a special symbol used to expand
  36. environment variables. In batch files, all occurrences of % must
  37. be doubled when using this option to properly escape. If this
  38. option is used at the command prompt then the % cannot be escaped
  39. and unintended expansion is possible.
  40. The variables available are:
  41. certs
  42. Output the certificate chain with details. Supported only by
  43. the OpenSSL, GnuTLS, Schannel, Rustls, and Secure Transport
  44. backends. (Added in 7.88.0)
  45. conn_id
  46. The connection identifier last used by the transfer. The
  47. connection id is unique number among all connections using the
  48. same connection cache. (Added in 8.2.0)
  49. content_type
  50. The Content-Type of the requested document, if there was any.
  51. errormsg
  52. The error message. (Added in 7.75.0)
  53. exitcode
  54. The numerical exit code of the transfer. (Added in 7.75.0)
  55. filename_effective
  56. The ultimate filename that curl writes out to. This is only
  57. meaningful if curl is told to write to a file with the
  58. --remote-name or --output option. It is most useful in
  59. combination with the --remote-header-name option.
  60. ftp_entry_path
  61. The initial path curl ended up in when logging on to the
  62. remote FTP server.
  63. header{name}
  64. The value of header "name" from the transfer's most recent
  65. server response. Unlike other variables, the variable name
  66. "header" is not in braces. For example "%header{date}". Refer
  67. to --write-out remarks. (Added in 7.84.0)
  68. header_json
  69. A JSON object with all HTTP response headers from the recent
  70. transfer. Values are provided as arrays, since in the case of
  71. multiple headers there can be multiple values. (Added in
  72. 7.83.0)
  73. The header names provided in lowercase, listed in order of
  74. appearance over the wire. Except for duplicated headers. They
  75. are grouped on the first occurrence of that header, each value
  76. is presented in the JSON array.
  77. http_code
  78. The numerical response code that was found in the last
  79. retrieved HTTP(S) or FTP(s) transfer.
  80. http_connect
  81. The numerical code that was found in the last response (from a
  82. proxy) to a curl CONNECT request.
  83. http_version
  84. The http version that was effectively used.
  85. json
  86. A JSON object with all available keys. (Added in 7.70.0)
  87. local_ip
  88. The IP address of the local end of the most recently done
  89. connection - can be either IPv4 or IPv6.
  90. local_port
  91. The local port number of the most recently done connection.
  92. method
  93. The http method used in the most recent HTTP request. (Added
  94. in 7.72.0)
  95. num_certs
  96. Number of server certificates received in the TLS handshake.
  97. Supported only by the OpenSSL, GnuTLS, Schannel, Rustls and
  98. Secure Transport backends. (Added in 7.88.0)
  99. num_connects
  100. Number of new connects made in the recent transfer.
  101. num_headers
  102. The number of response headers in the most recent request
  103. (restarted at each redirect). Note that the status line IS NOT
  104. a header. (Added in 7.73.0)
  105. num_redirects
  106. Number of redirects that were followed in the request.
  107. num_retries
  108. Number of retries actually performed when "--retry" has been
  109. used. (Added in 8.9.0)
  110. onerror
  111. The rest of the output is only shown if the transfer returned
  112. a non-zero error. (Added in 7.75.0)
  113. output{filename}
  114. From this point on, the --write-out output is written to the
  115. filename specified in braces. The filename can be prefixed
  116. with ">>" to append to the file. Unlike other variables, the
  117. variable name "output" is not in braces. For example
  118. "%output{>>stats.txt}". Refer to --write-out remarks. (Added
  119. in 8.3.0)
  120. proxy_ssl_verify_result
  121. The result of the HTTPS proxy's SSL peer certificate
  122. verification that was requested. 0 means the verification was
  123. successful.
  124. proxy_used
  125. Returns 1 if the previous transfer used a proxy, otherwise 0.
  126. Useful to for example determine if a "NOPROXY" pattern matched
  127. the hostname or not. (Added in 8.7.0)
  128. redirect_url
  129. When an HTTP request was made without --location to follow
  130. redirects (or when --max-redirs is met), this variable shows
  131. the actual URL a redirect would have gone to.
  132. referer
  133. The Referer: header, if there was any. (Added in 7.76.0)
  134. remote_ip
  135. The remote IP address of the most recently done connection -
  136. can be either IPv4 or IPv6.
  137. remote_port
  138. The remote port number of the most recently done connection.
  139. response_code
  140. The numerical response code that was found in the last
  141. transfer (formerly known as "http_code").
  142. scheme
  143. The URL scheme (sometimes called protocol) that was
  144. effectively used.
  145. size_download
  146. The total amount of bytes that were downloaded. This is the
  147. size of the body/data that was transferred, excluding headers.
  148. size_header
  149. The total amount of bytes of the downloaded headers.
  150. size_request
  151. The total amount of bytes that were sent in the HTTP request.
  152. size_upload
  153. The total amount of bytes that were uploaded. This is the size
  154. of the body/data that was transferred, excluding headers.
  155. speed_download
  156. The average download speed that curl measured for the complete
  157. download. Bytes per second.
  158. speed_upload
  159. The average upload speed that curl measured for the complete
  160. upload. Bytes per second.
  161. ssl_verify_result
  162. The result of the SSL peer certificate verification that was
  163. requested. 0 means the verification was successful.
  164. stderr
  165. From this point on, the --write-out output is written to
  166. standard error. (Added in 7.63.0)
  167. stdout
  168. From this point on, the --write-out output is written to
  169. standard output. This is the default, but can be used to
  170. switch back after switching to stderr. (Added in 7.63.0)
  171. time_appconnect
  172. The time, in seconds, it took from the start until the
  173. SSL/SSH/etc connect/handshake to the remote host was
  174. completed.
  175. time_connect
  176. The time, in seconds, it took from the start until the TCP
  177. connect to the remote host (or proxy) was completed.
  178. time_namelookup
  179. The time, in seconds, it took from the start until the name
  180. resolving was completed.
  181. time_posttransfer
  182. The time it took from the start until the last byte is sent by
  183. libcurl. In microseconds. (Added in 8.10.0)
  184. time_pretransfer
  185. The time, in seconds, it took from the start until the file
  186. transfer was just about to begin. This includes all
  187. pre-transfer commands and negotiations that are specific to
  188. the particular protocol(s) involved.
  189. time_queue
  190. The time, in seconds, the transfer was queued during its run.
  191. This adds the queue time for each redirect step that may have
  192. happened. Transfers may be queued for significant amounts of
  193. time when connection or parallel limits are in place. (Added
  194. in 8.12.0)
  195. time_redirect
  196. The time, in seconds, it took for all redirection steps
  197. including name lookup, connect, pretransfer and transfer
  198. before the final transaction was started. "time_redirect"
  199. shows the complete execution time for multiple redirections.
  200. time_starttransfer
  201. The time, in seconds, it took from the start until the first
  202. byte was received. This includes time_pretransfer and also the
  203. time the server needed to calculate the result.
  204. time_total
  205. The total time, in seconds, that the full operation lasted.
  206. tls_earlydata
  207. The amount of bytes that were sent as TLSv1.3 early data. This
  208. is 0 if this TLS feature was not used and negative if the data
  209. sent had been rejected by the server. The use of early data is
  210. enabled via the command line option "--tls-earlydata". (Added
  211. in 8.12.0)
  212. url
  213. The URL that was fetched. (Added in 7.75.0)
  214. url.scheme
  215. The scheme part of the URL that was fetched. (Added in 8.1.0)
  216. url.user
  217. The user part of the URL that was fetched. (Added in 8.1.0)
  218. url.password
  219. The password part of the URL that was fetched. (Added in
  220. 8.1.0)
  221. url.options
  222. The options part of the URL that was fetched. (Added in 8.1.0)
  223. url.host
  224. The host part of the URL that was fetched. (Added in 8.1.0)
  225. url.port
  226. The port number of the URL that was fetched. If no port number
  227. was specified and the URL scheme is known, that scheme's
  228. default port number is shown. (Added in 8.1.0)
  229. url.path
  230. The path part of the URL that was fetched. (Added in 8.1.0)
  231. url.query
  232. The query part of the URL that was fetched. (Added in 8.1.0)
  233. url.fragment
  234. The fragment part of the URL that was fetched. (Added in
  235. 8.1.0)
  236. url.zoneid
  237. The zone id part of the URL that was fetched. (Added in 8.1.0)
  238. urle.scheme
  239. The scheme part of the effective (last) URL that was fetched.
  240. (Added in 8.1.0)
  241. urle.user
  242. The user part of the effective (last) URL that was fetched.
  243. (Added in 8.1.0)
  244. urle.password
  245. The password part of the effective (last) URL that was
  246. fetched. (Added in 8.1.0)
  247. urle.options
  248. The options part of the effective (last) URL that was fetched.
  249. (Added in 8.1.0)
  250. urle.host
  251. The host part of the effective (last) URL that was fetched.
  252. (Added in 8.1.0)
  253. urle.port
  254. The port number of the effective (last) URL that was fetched.
  255. If no port number was specified, but the URL scheme is known,
  256. that scheme's default port number is shown. (Added in 8.1.0)
  257. urle.path
  258. The path part of the effective (last) URL that was fetched.
  259. (Added in 8.1.0)
  260. urle.query
  261. The query part of the effective (last) URL that was fetched.
  262. (Added in 8.1.0)
  263. urle.fragment
  264. The fragment part of the effective (last) URL that was
  265. fetched. (Added in 8.1.0)
  266. urle.zoneid
  267. The zone id part of the effective (last) URL that was fetched.
  268. (Added in 8.1.0)
  269. urlnum
  270. The URL index number of this transfer, 0-indexed. Unglobbed
  271. URLs share the same index number as the origin globbed URL.
  272. (Added in 7.75.0)
  273. url_effective
  274. The URL that was fetched last. This is most meaningful if you
  275. have told curl to follow location: headers.
  276. xfer_id
  277. The numerical identifier of the last transfer done. -1 if no
  278. transfer has been started yet for the handle. The transfer id
  279. is unique among all transfers performed using the same
  280. connection cache. (Added in 8.2.0)
  281. If --write-out is provided several times, the last set value is
  282. used.
  283. Example:
  284. curl -w '%{response_code}\n' https://example.com
  285. See also --verbose and --head.

评论

此博客中的热门博文

Shell 文件包含

  和其他语言一样,Shell 也可以包含外部脚本。这样可以很方便的封装一些公用的代码作为一个独立的文件。 Shell 文件包含的语法格式如下: . filename # 注意点号(.)和文件名中间有一空格 或 source filename 实例 创建两个 shell 脚本文件。 test1.sh 代码如下: #!/bin/bash # author:菜鸟教程 # url:www.runoob.com url = "http://www.runoob.com" test2.sh 代码如下: #!/bin/bash # author:菜鸟教程 # url:www.runoob.com #使用 . 号来引用test1.sh 文件 . ./ test1 . sh # 或者使用以下包含文件代码 # source ./test1.sh echo "菜鸟教程官网地址:$url" 接下来,我们为 test2.sh 添加可执行权限并执行: $ chmod + x test2 . sh $ ./ test2 . sh 菜鸟教程官网地址: http : //www.runoob.com 注: 被包含的文件 test1.sh 不需要可执行权限。

Shell 输入/输出重定向

  大多数 UNIX 系统命令从你的终端接受输入并将所产生的输出发送回​​到您的终端。一个命令通常从一个叫标准输入的地方读取输入,默认情况下,这恰好是你的终端。同样,一个命令通常将其输出写入到标准输出,默认情况下,这也是你的终端。 重定向命令列表如下: 命令 说明 command > file 将输出重定向到 file。 command < file 将输入重定向到 file。 command >> file 将输出以追加的方式重定向到 file。 n > file 将文件描述符为 n 的文件重定向到 file。 n >> file 将文件描述符为 n 的文件以追加的方式重定向到 file。 n >& m 将输出文件 m 和 n 合并。 n <& m 将输入文件 m 和 n 合并。 << tag 将开始标记 tag 和结束标记 tag 之间的内容作为输入。 需要注意的是文件描述符 0 通常是标准输入(STDIN),1 是标准输出(STDOUT),2 是标准错误输出(STDERR)。 输出重定向 重定向一般通过在命令间插入特定的符号来实现。特别的,这些符号的语法如下所示: command1 > file1 上面这个命令执行command1然后将输出的内容存入file1。 注意任何file1内的已经存在的内容将被新内容替代。如果要将新内容添加在文件末尾,请使用>>操作符。 实例 执行下面的 who 命令,它将命令的完整的输出重定向在用户文件中(users): $ who > users 执行后,并没有在终端输出信息,这是因为输出已被从默认的标准输出设备(终端)重定向到指定的文件。 你可以使用 cat 命令查看文件内容: $ cat users _mbsetupuser console Oct 31 17 : 35 tianqixin console Oct 31 17 : 35 tianqixin ttys000 Dec 1 11 : 33 输出重定向会覆盖文件内容,请看下面的例子: $ echo "菜鸟教程:www.runoob.com" > users $ cat users 菜鸟教程:...

Shell 函数

  linux shell 可以用户定义函数,然后在shell脚本中可以随便调用。 shell中函数的定义格式如下: [   function   ]  funname  [ ( ) ] {     action;      [ return  int; ] } 说明: 1、可以带  function fun()  定义,也可以直接  fun()  定义,不带任何参数。 2、参数返回,可以显示加: return  返回,如果不加,将以最后一条命令运行结果,作为返回值。  return  后跟数值  n(0-255) . 下面的例子定义了一个函数并进行调用: 实例 #!/bin/bash # author:菜鸟教程 # url:www.runoob.com demoFun ( ) {      echo   "这是我的第一个 shell 函数!" } echo   "-----函数开始执行-----" demoFun echo   "-----函数执行完毕-----" 输出结果: -----函数开始执行----- 这是我的第一个 shell 函数! -----函数执行完毕----- 下面定义一个带有  return  语句的函数: 实例 #!/bin/bash # author:菜鸟教程 # url:www.runoob.com funWithReturn ( ) {      echo   "这个函数会对输入的两个数字进行相加运算..."      echo   "输入第一个数字: "      read  aNum      echo   "输入第二个数字: "      read  anotherNum      echo   "两个数字分别为  ...