某种“新手问题”,以部分确认/纠正我的理解:我想实现一些自动网站探测包括。登录,我不想真的提取任何数据。从之前的一项工作中,我记得curl和仅仅检查URL可用性( earlier响应代码,例如200或不),这是很好的工作。现在的任务也是登录-但我不能让它工作,并有一些疑问,这是否在任何情况下,如预期的工作。经过一段时间的实验和在WWW上搜索,我找不到一个明确的答案-所以我的希望在这里:)
最初,我的想法是使用一个脚本并在其中放入curl命令,以防万一使用cookie选项,例如:
$ curl -s -w "%{http_code}" -o /tmp/tge-HTML.out -b /tmp/tge-cookies -c /tmp/tge-cookies -F "username=bla&passwd=blub&Submit=Login" http://some.url(我使用HTTPfox来确定POST参数,并且大多数情况下肯定是正确的)
然后,我搜索文件/tmp/tge-HTML.out是否包含"Logout“,以证明我已经登录(即使没有登录,我也总是在所有带有我尝试过的URL的测试中获得HTTP 200 )。
在其中一种情况下(后面有一个Tomcat ),我看到在登录火狐之后,后续的URL包含...&jsessionid=.当然,我一开始不知道--所以我尝试了几种方法,例如调用curl (命令!)在shell脚本中两次从第一个获取sessionID并将其附加到第二个URL中,如下所示:
$ curl -s -w "%{http_code}" -o /tmp/tge-HTML.out -b /tmp/tge-cookies -c /tmp/tge-cookies -F "username=bla&passwd=blub&Submit=Login" http://some.url
... extract jsessionid from /tmp/tge-HTML.out ...
$ curl -s -w "%{http_code}" -o /tmp/tge-HTML.out -b /tmp/tge-cookies -c /tmp/tge-cookies
http://some.url/...?jsessionid=...但不管我做了什么,我什么都没得到:-现在是问题:
。
- looks to me as if true, but then a simple POST should do and it should contain the "Logout" ...
- however, since I never get this to work, what might be wrong?
如果我必须执行多个请求,那么使用curl命令这样的概念是否有效?当卷曲退出时会发生什么?套接字关闭,web服务器可能关闭会话(我的jsessionid无效)?
- If this approach does not work, a libcurl-based concept might work better? E.g. phpcurl (just read about it but never tried yet) where I keep the session within my php script, hence connection/session remains open etc ...
- Possibly depends on whether the site uses cookies or not?
很可能有很多“依赖”:
拉格兹山
发布于 2012-04-09 20:21:34
好吧,最后我取得了一些进展-也许这对其他人有帮助.首先,基于脚本的方法确实有效,至少在服务器端有一个Joomla的测试用例中是这样的(稍后我将尝试其他方法)。解决方案的关键是:CURL login by script to a Joomla website
类似于所描述的方式,我从第一个GET的响应中提取令牌,并将其放入第二个GET的表单参数中:
rm -f /tmp/tge-cookies
HTTP_CODE=$(./curl -s -w "%{http_code}\\n" -o /tmp/tge-HTML-out.1 -b /tmp/tge-cookies -c /tmp/tge-cookies -L ${URL})
# Eval HTTP_CODE ...
Token=`awk '{ if(match($1, "<input") && match($3, "name=\"[a-z0-9]+\""))
{
gsub("name=\"", "", $3);
gsub("\"", "", $3);
if(length($3) == 33)
{
print $3;
exit 0;
}
} }' /tmp/tge-HTML-out.1`
HTTP_CODE=$(./curl -L -s -w "%{http_code}\\n" -o /tmp/tge-HTML-out.2 -b /tmp/tge-cookies -c /tmp/tge-cookies -d "username=${User}&passwd=${Pass}&option=login&task=login&${Token}=1&remember=yes&Submit=Login" ${URL})
# Eval HTTP_CODE ...
# Eval /tmp/tge-HTML-out.2 whether containing "Logout" ...第二个区别是使用"-F“不起作用,但"-d”起作用。非常有趣的是,POST参数与我在HTTPfox中看到的不同。
关照
https://stackoverflow.com/questions/10008129
复制相似问题