使用带有curl的真实性令牌登录到网页

使用带有curl的真实性令牌登录到网页,curl,login,authenticity-token,Curl,Login,Authenticity Token,我正在尝试创建一个自定义程序(或脚本)来登录到一个网页,然后下载一个文件。登录页面有一个表单,其中包含两个文本字段(用户名和密码)和一个隐藏字段“Authentity\u token”。我读过关于真实性令牌的文章 我尝试了使用curl的自定义bash脚本,但它不起作用。谁能看一下我的代码,告诉我有什么问题吗 #!/bin/bash #Pulls the login page and strips out the auth token authToken=`curl -L -c cookies.t

我正在尝试创建一个自定义程序(或脚本)来登录到一个网页,然后下载一个文件。登录页面有一个表单,其中包含两个文本字段(用户名和密码)和一个隐藏字段“Authentity\u token”。我读过关于真实性令牌的文章

我尝试了使用curl的自定义bash脚本,但它不起作用。谁能看一下我的代码,告诉我有什么问题吗

#!/bin/bash
#Pulls the login page and strips out the auth token
authToken=`curl -L -c cookies.txt --silent 'http://xxx.xxxxx.xx' | grep authenticity_token | awk -F'value="' '{print $2;}' | awk -F'="' '{print $1;}'`

echo $authToken

# Posts all the pre-URI-encoded stuff and appends the URI-encoded auth token
curl -L -c cookies.txt -b cookies.txt --data-urlencode authenticity_token=$authToken  --data-urlencode 'username=my-username' --data-urlencode 'password=my-pass'  'http://xxx.xxxxx.xxx/login' 

# access the page i want
curl -b cookies 'http://xxx.xxxxx.xxx/page_i_want_to_access'
当我使用浏览器登录到网页时,如果登录成功,服务器会将我重定向到下一页,但当我使用脚本时,服务器会使用标头为“200 OK”的数据包而不是“302重定向”的数据包进行回复(我使用wireshark捕获pakets)。因此,我认为身份验证不起作用


有什么建议吗?

Wireshark是一个不错的数据包嗅探工具,但我建议您使用,它对这类事情更干净。您是否检查了第二个请求的结果,看看它是否包含成功登录后应该包含的内容?Wireshark是一个不错的数据包嗅探工具,但我建议您使用,对于这类事情,它要干净得多。您是否检查过第二个请求的结果,看看它是否包含成功登录后应该包含的内容?