bash 从 curl 请求中提取令牌并在另一个 shell 命令中使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45242119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:19:03  来源:igfitidea点击:

Extract token from a curl request and use in another shell command

jsonbashcurl

提问by raze

I've started picking up bash scripting and I'm stuck at something I'm trying to wrap my head around.

我已经开始学习 bash 脚本编写,但我被困在我试图解决的问题上。

I have a curl command that outputs a token and I need to use it in the following command:

我有一个输出令牌的 curl 命令,我需要在以下命令中使用它:

curl -k 'https://server:port/session' -X POST -H 'Content-Type: application/json' -d '{"username":"admin","password":"password"}'

It then outputs a token here:

然后它在这里输出一个令牌:

{"token":"ac07098ad59ca6f3fccea0e2a2f6cb080df55c9a52fc9d65"}

I then need to use it in the follow up command

然后我需要在后续命令中使用它

curl https://server:port/ -k -X POST -H 'Content-Type: application/json' -H 'X-Cookie:token=token' -d '

I was thinking I could output the token to a file, then have a sed command write the token to a file, then the new command use a variable where token=$token

我想我可以将令牌输出到文件,然后让 sed 命令将令牌写入文件,然后新命令使用变量 where token=$token

Thanks!

谢谢!

回答by glenn Hymanman

This is where a JSON parsing tool comes in handy (such as jq):

这是 JSON 解析工具派上用场的地方(例如jq):

$ echo '{"token":"ac07098ad59ca6f3fccea0e2a2f6cb080df55c9a52fc9d65"}' | jq -r .token
ac07098ad59ca6f3fccea0e2a2f6cb080df55c9a52fc9d65

So

所以

json=$( curl -k 'https://server:port/session' -X POST -H 'Content-Type: application/json' -d '{"username":"admin","password":"password"}' )
token=$( jq -r ".token" <<<"$json" )

curl https://server:port/ -k -X POST -H "X-Cookie:token=$token" ...

回答by teikitel

With no further tool than a bash (tested Centos/Rhel7/GitBash) :

除了 bash(已测试 Centos/Rhel7/GitBash)之外没有其他工具:

json=$(curl -k 'https://server:port/session' \
            -X POST -H 'Content-Type: application/json' \
            -d '{"username":"admin","password":"password"}') \
&& token=$(echo $json | sed "s/{.*\"token\":\"\([^\"]*\).*}//g") \
&& echo "token = $token"

then use your authentication needing commands like that :

然后使用您的身份验证需要这样的命令:

curl https://server:port/ -k -X POST \
                          -H 'Content-Type: application/json' \
                          -H 'X-Cookie:token=$token' -d ...'

回答by Chad D

If Python is installed, and hopefully it is on modern systems, you can do something like:

如果安装了 Python,并且希望它在现代系统上,您可以执行以下操作:

OUTPUT="$(curl -k 'https://server:port/session' -X POST -H 'Content-Type: application/json' -d '{"username":"admin","password":"password"}' | python -c "import sys, json; print json.load(sys.stdin)['token']")"

This will give you:

这会给你:

echo $OUTPUT
ec2e99a1d294fd4bc0a04da852ecbdeed3b55671c08cc09f

回答by Benjamin

Use the `` syntax:

使用 `` 语法:

cmd1result=$(command1 | cut -d ':' -f 2 | grep -Po "[a-z0-9-]+")
command2 $cmd1result