我如何使用Bash捕获请求标头

时间:2019-04-08 06:45:03

标签: bash

我需要制作一个脚本,该脚本可以在网站的请求标头中获取访问令牌,有人可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

您将可以通过 grep cut 命令传递卷曲输出来实现此目的。在这里,我捕获了 Content-Length 标头的值。

curl -s -I example.com | grep "Content-Length" | cut -d ':' -f 2

下面是一个示例脚本。

#!/bin/bash

DOMAIN="example.com"
HEADER="Content-Length"

HEADER_VALUE=$(curl -s -I $DOMAIN | grep $HEADER | cut -d ':' -f 2)
echo $HEADER_VALUE

答案 1 :(得分:1)

尝试将curl与选项-I一起使用:

示例:

$ curl -I stackoverflow.com
HTTP/1.1 301 Moved Permanently
Content-Length: 143
Content-Type: text/html; charset=utf-8
Location: https://stackoverflow.com/
X-Request-Guid: 2396f2a8-3398-4264-9b26-ad79f282cb71
Content-Security-Policy: upgrade-insecure-requests
Accept-Ranges: bytes
Date: Mon, 08 Apr 2019 06:49:12 GMT
Via: 1.1 varnish
Connection: keep-alive
X-Served-By: cache-hhn1522-HHN
X-Cache: MISS
X-Cache-Hits: 0
X-Timer: S1554706153.814312,VS0,VE79
Vary: Fastly-SSL
X-DNS-Prefetch-Control: off
Set-Cookie: prov=e68f14b2-d35a-6ca6-8e8d-0b3f936049b4; domain=.stackoverflow.com; 
expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
相关问题