卷曲。检查重定向

ipe*_*cks 9 linux ubuntu curl

假设我们有 3 个链接:link1、link2、link3。link1 重定向到 link2,link2 重定向到 link3。那么如何用 curl 看到呢?

小智 14

您可以使用-I. 如果重定向是元刷新,它应该以这种方式作为标题。

lamp@oort ~ $ curl -I http://google.com<br>
HTTP/1.1 301 Moved Permanently<br>
Location: http://www.google.com/<br>
Content-Type: text/html; charset=UTF-8<br>
Date: Thu, 21 Nov 2013 14:59:13 GMT<br>
Expires: Sat, 21 Dec 2013 14:59:13 GMT<br>
Cache-Control: public, max-age=2592000<br>
Server: gws<br>
Content-Length: 219<br>
X-XSS-Protection: 1; mode=block<br>
X-Frame-Options: SAMEORIGIN<br>
Alternate-Protocol: 80:quic
Run Code Online (Sandbox Code Playgroud)

如果重定向是通过 PHP 发生的,您可以通过比较浏览器的去向与实际去向的位置来检测这一点......有很多方法可以使用 Python、JS 等来做到这一点。一个项目可能很有趣对您来说是 phantomjs,一个可编写脚本的无头浏览器。


Flo*_*elf 8

man curl

   -w, --write-out <format>
          Defines what to display on stdout after a completed and
          successful operation.

          <...>

          redirect_url   When an HTTP request was made without -L to
                         follow redirects, this variable will show the 
                         actual URL a redirect would take you to.
                         (Added in 7.18.2)
Run Code Online (Sandbox Code Playgroud)

所以可能curl -w "%{redirect_url}" link1会给你第一个重定向网址。

也许这样的东西适合你:

URL="http://google.com"
while [ -n "${URL}" ]
do
    echo $URL
    URL=$(curl -sw "\n\n%{redirect_url}" "${URL}" | tail -n 1)
done
Run Code Online (Sandbox Code Playgroud)


Gil*_*not 6

尝试这个 :

for link in link1 link2 link3; do
    curl -Is "$link" | awk '/Location/{print $2}'
done
Run Code Online (Sandbox Code Playgroud)

或使用

for link in link1 link2 link3; do
    printf '%s\n%s\n\n%s\n' 'HEAD / HTTP/1.1' "Host: $link" 'Connexion:close' |
    netcat $link 80 | awk '/Location/{print $2}'
done
Run Code Online (Sandbox Code Playgroud)