bash 如何使用 curl 将收到的 cookie 信息打印到标准输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21463776/
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-10 00:39:00  来源:igfitidea点击:

How do you print received cookie info to stdout with curl?

bashunixcookiescurl

提问by Brian Yeh

How do you print received cookie info to stdout with curl?

如何使用 curl 将收到的 cookie 信息打印到标准输出?

According to the man pages if you use '-' as the file name for the -c --cookie-jar option it should print the cookie to stdout. The problem is I get an error:

根据手册页,如果您使用“-”作为 -c --cookie-jar 选项的文件名,它应该将 cookie 打印到标准输出。问题是我收到一个错误:

curl: option -: is unknown

an example of the command I am running:

我正在运行的命令示例:

curl -c --cookie-jar - 'http://google.com'

采纳答案by Radu R?deanu

You get that error because you use in the wrong way that option. When you see in a man page an option like:

您会收到该错误,因为您以错误的方式使用该选项。当您在手册页中看到如下选项时:

-c, --cookie-jar <file name>

this mean that if you want to use that option, you must to use -cOR--cookie-jar, never both! These two are equivalent and, in fact, -cis the abbreviated form for --cookie-jar. There are many, many options in man pages which are designed in the same way.

这意味着如果你想使用那个选项,你必须使用-cOR--cookie-jar,永远不要同时使用!这两个是等价的,实际上-c是 的缩写形式--cookie-jar。手册页中有许多以相同方式设计的选项。

In your case:

在你的情况下:

curl -c - 'http://google.com'

--cookie-jaris given as argument for -coption, so, it's interpreted as a file name, not like an option (as you may think), and -remains alone which leads to error because curl, indeed, doesn't have such an option.

--cookie-jar作为-c选项的参数给出,因此,它被解释为一个文件名,而不是一个选项(如您所想),并且-仍然单独存在,这会导致错误,因为curl确实没有这样的选项。

回答by gmoon

You need to use two options to get only the cookie text on stdout:

您需要使用两个选项来仅获取 stdout 上的 cookie 文本:

--cookie-jar <file name>from the man page "If you set the file name to a single dash, '-', the cookies will be written to stdout"

--cookie-jar <file name>从手册页“如果您将文件名设置为单个破折号,'-',cookies 将被写入标准输出”

--output <file>from the man page "Write output to instead of stdout". Set it to /dev/null to throw it away.

--output <file>来自手册页“将输出写入而不是标准输出”。将其设置为 /dev/null 以将其丢弃。

Also helpful is --silent

也有帮助的是 --silent

Putting it all together:

把它们放在一起:

curl --silent --output /dev/null --cookie-jar - 'http://www.google.com/'

curl --silent --output /dev/null --cookie-jar - 'http://www.google.com/'

Output:

输出:

# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

#HttpOnly_.google.com   TRUE    /   FALSE   1512524163  NID 105=DownH33BKZnCsWJeGvsIC5cKRi7CPT3K3QjfUB-4js5xGw6P_6svMqU1yKlKOEu4XwL_TdddZlcMITefFGOtCCyzJNhO_7E9UMNpbQHja40IAerYP5Bwj-FhY1m35mZdvkVSmrg1pZPvH96IkVVVVVVVV

My use case: Test that your website uses the HttpOnly cookie setting, per the OWASP recommendation:

我的用例:根据 OWASP 建议,测试您的网站是否使用 HttpOnly cookie 设置:

curl --silent --output /dev/null --cookie-jar - 'http://www.google.com/' | grep HttpOnly

回答by Matías Insaurralde

Remove the "-c"

删除“-c”

curl --cookie-jar - 'http://google.com'

Also you try verbose mode and see the cookie headers:

您还可以尝试详细模式并查看 cookie 标头:

curl -v 'http://google.com'

回答by Ronaldo Afonso

You can save the cookies received and send them back to the server using the following commands:

您可以使用以下命令保存收到的 cookie 并将它们发送回服务器:

1) To get/save the cookies to file "/tmp/cookies.txt":

1) 获取/保存 cookie 到文件“/tmp/cookies.txt”:

curl -c /tmp/cookies.txt http://the.site.with.cookies/

2) To send the cookies back to the server (again using file "/tmp/cookies.txt"):

2) 将 cookie 发送回服务器(再次使用文件“/tmp/cookies.txt”):

curl -b /tmp/cookies.txt http://the.site.with.cookies/

I hope it was useful.

我希望它很有用。

[]s Ronaldo

[] 罗纳尔多