开发者

How do I fetch a sites IP in Rails

开发者 https://www.devze.com 2023-02-03 07:38 出处:网络
I have a rails project. I scaffolded Site and Server, their models are below. When a new site is created I want to associate it with the right server by fetching the IP that the url points to. I will

I have a rails project. I scaffolded Site and Server, their models are below. When a new site is created I want to associate it with the right server by fetching the IP that the url points to. I will run a cronjob or EventMachine to check if this data is still up to date. This way this data will stay up to date and users don't have to maintain it开发者_StackOverflow.

Question is: how do I do that; how do I fetch a sites IP if I only have the url in Rails?

I'm running Rails 3 on Ruby 1.9.2.

Table name: sites  
url        :string(255)  
name       :string(255)  
server_id  :integer #this is a foreign key to a server

Table name: servers
ip         :string(255)
name       :string(255)


Take a look at Resolv, which is part of the ruby stdlib.

Resolv is a thread-aware DNS resolver library written in Ruby.

>> require "Resolv"
=> true
>> p Resolv.getaddress "google.com"
"209.85.149.147"
0

精彩评论

暂无评论...
验证码 换一张
取 消