0

I am using the Ruby Mechanize Gem in to fetch and parse websites and I need to detect redirects to a certain IP. Here is my basic setup:

agent = Mechanize.new
page = agent.get('http://www.example.com')

Now, its obvious how to detect the redirect as such:

is_redirect? = page.code[/30[12]/].present?

but I want to take it a step further and check what domain/IP it redirects to; so something along the lines of (pseudo-code):

if page.resolves_to(55.55.55.55)...

Any thoughts on how this can be achieved?

Severin
  • 8,508
  • 14
  • 68
  • 117
  • possible duplicate of [Finding the IP address of a domain](http://stackoverflow.com/questions/5742521/finding-the-ip-address-of-a-domain) – D-side Oct 25 '14 at 13:25
  • @D-side: This question is not about finding the IP but checking the final destination of a redirect via its IP. – Severin Oct 25 '14 at 14:04
  • I did manage to detect a redirection IP for `mail.google.com`, but it responds with code `200` and a `` redirect. I started writing up an answer but then I thought you might get redirects differently. Is there a live example of a redirect you are expecting? – D-side Oct 25 '14 at 18:07

1 Answers1

1

The redirected url is in Page#uri:

require 'socket'
IPSocket::getaddress(page.uri.host)
pguardiario
  • 53,827
  • 19
  • 119
  • 159