curl头中的Bash变量作为用户代理

时间:2018-02-21 18:45:07

标签: bash curl user-agent variable-expansion

我试图从bash变量设置用户代理动态。但似乎并非那么直截了当。

如果标题值包含空格,则标题需要用单引号括起来,我们无法避免用户代理标题的单引号,但如果我们这样做,则不会评估变量,如果我们用双引号替换它, ua字符串中的空格会造成很多混乱。

推荐的方法是在curl命令的外侧创建所有头,数据,cookie变量,并在单次使用中使用它。但这是否意味着我们永远不会在curl命令中单独设置用户代理变量?

我现在尝试使用的命令如下所示,

curl --proxy localhost:8000 -m 1 --retry 0  -i -s -S -b cookie.jar -c cookie.jar -X POST "https://myhost:51512/id?val=${id}" -H 'user-agent: Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405'

linux_ua="Mozilla/5.0 (Unknown; Linux) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/v1.0.0 Safari/537.1"
my_user_agent=$linux_ua
curl --proxy localhost:8000 -m 1 --retry 0  -i -s -S -b cookie.jar -c cookie.jar -X POST "https://myhost:51512/id?val=${id}" -H '${user_agent}'

2 个答案:

答案 0 :(得分:2)

单引号只是shell语法,以防止字符串内的任何类型的扩展;它们不是标题本身的一部分。在这种情况下,使用双引号也可以。

curl ... -H "user-agent: Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405"

将用户代理存储在变量中是微不足道的:

ua="user-agent: Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405"
curl ... -H "$ua"

在这里,您必须使用双引号来扩展$ua的值;使用curl ... -H '$ua'会将文字3个字符的字符串$ua作为标题发送。

答案 1 :(得分:0)

您在Bash中的word splitting遇到了一个特殊的问题,BashFAQ/050在这个问题上有很好的例子。

您可以运行以下脚本来调查行为。

#!/usr/bin/env bash
#
# a script for the demonstration of word splitting

set -ex

url='localhost:8080/get' # docker run --rm -p 8080:80 kennethreitz/httpbin
user_agent='Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1'

curl_opts_with_quote="--user-agent '${user_agent}'"
curl_opts_without_quote="--user-agent ${user_agent}"

curl -H 'Accept: application/json' $curl_opts_with_quote $url
curl -H 'Accept: application/json' $curl_opts_without_quote $url

curl_opts_array=(--user-agent "${user_agent}")

curl -H 'Accept: application/json' "${curl_opts_array[@]}" $url

输出

请注意,输出行从httpbin返回+User-Agent

+ url=localhost:8080/get
+ user_agent='Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1'
+ curl_opts_with_quote='--user-agent '\''Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1'\'''
+ curl_opts_without_quote='--user-agent Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1'
+ curl -H 'Accept: application/json' --user-agent ''\''Mozilla/5.0' '(Windows' NT '5.1;' 'rv:2.0.1)' Gecko/20100101 'Firefox/4.0.1'\''' localhost:8080/get
curl: (6) Could not resolve host: (Windows
curl: (6) Could not resolve host: NT
curl: (6) Could not resolve host: 5.1;
curl: (3) Port number ended with '.'
curl: (6) Could not resolve host: Gecko
curl: (6) Could not resolve host: Firefox
{
  "args": {},
  "headers": {
    "Accept": "application/json",
    "Host": "localhost:8080",
    "User-Agent": "'Mozilla/5.0"
  },
  "origin": "172.30.5.9",
  "url": "http://localhost:8080/get"
}
+ curl -H 'Accept: application/json' --user-agent Mozilla/5.0 '(Windows' NT '5.1;' 'rv:2.0.1)' Gecko/20100101 Firefox/4.0.1 localhost:8080/get
curl: (6) Could not resolve host: (Windows
curl: (6) Could not resolve host: NT
curl: (6) Could not resolve host: 5.1;
curl: (3) Port number ended with '.'
curl: (6) Could not resolve host: Gecko
curl: (6) Could not resolve host: Firefox
{
  "args": {},
  "headers": {
    "Accept": "application/json",
    "Host": "localhost:8080",
    "User-Agent": "Mozilla/5.0"
  },
  "origin": "172.30.5.9",
  "url": "http://localhost:8080/get"
}
+ curl_opts_array=(--user-agent "${user_agent}")
+ curl -H 'Accept: application/json' --user-agent 'Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1' localhost:8080/get
{
  "args": {},
  "headers": {
    "Accept": "application/json",
    "Host": "localhost:8080",
    "User-Agent": "Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"
  },
  "origin": "172.30.5.9",
  "url": "http://localhost:8080/get"
}