domingo, 12 de junio de 2011

Get IP from Ruby

Ok here my code. It works with Ruby 1.9 on my Arch Linux, i hope it be useful

=begin
  GNU GPL v3
  (C) Gerardo Gonzalez Cruz gerardogc2378@gmail.com
  June 2011
=end

require 'ipaddr'
require 'net/http'

class Utils
  def check_ip_on_dydns
    con = Net::HTTP.new('checkip.dyndns.org', 80)
    resp,body = con.get("/")
    ip = body.match(/\d+\.\d+\.\d+\.\d+/)

    ip[0]
  end
 
  def get_ip
    return IPAddr.new(check_ip_on_dydns).to_s
  end
end

references:
  • http://checkip.dyndns.org/
  • http://www.ruby-forum.com/

Happy Hackin'

No hay comentarios:

Publicar un comentario