Forum: Ruby To get IP addres of the machine

661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2009-01-06 06:49
Hi
Could you please tell the code to get the IPaddress of my machine

Sijo
Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2009-01-06 07:23
Sijo Kg wrote:
> Hi
> Could you please tell the code to get the IPaddress of my machine

This might work:

class Socket
  class << self
    def get_ip hostName
      begin
       ipInt = gethostbyname(hostName)[3]
        return "%d.%d.%d.%d" % [ipInt[0].ord, ipInt[1].ord,
ipInt[2].ord, ipInt[3].ord]
      rescue SocketError # bizarre, but happens
        return ''
      end
    end

    def get_host_ip
      begin
        ip = Socket.getaddrinfo(Socket.gethostname, nil,
Socket::AF_UNSPEC, Socket::SOCK_STREAM, nil,
Socket::AI_CANONNAME).select{|type| type[0] == 'AF_INET'}[0][3] # ltodo
hmm we don't want it to do the reverse lookups!
        raise if ip.blank?
        return ip
      rescue => e
        get_ip(Socket.gethostname) # less accurate or something, I guess
      end
    end

  end
end
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2009-01-06 08:17
Hi
Thanks for the reply But please give an example to instantiate an object
of this class also

Sijo
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-06 10:06
Sijo Kg wrote:
> Could you please tell the code to get the IPaddress of my machine

Your machine may have multiple IP addresses, but assuming its hostname
has been set up properly, the following may do the trick:

require 'socket'
ip = IPSocket.getaddress(Socket.gethostname)
8870709061256c1b455dca8f169e855e?d=identicon&s=25 ab5tract (Guest)
on 2009-01-06 21:35
(Received via mailing list)
> Sijo Kg wrote:
> > Could you please tell the code to get the IPaddress of my machine

Another way to do this:

require 'ipaddr'
require 'net/http'

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

  ip[0]
end

my_ip = IPAddr.new(get_ip)

I had to do this as a work around before, though if I was working
around an issue with Socket.gethostname or my own ignorance I can no
longer tell ;)
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 Gary Wright (Guest)
on 2009-01-06 23:13
(Received via mailing list)
On Jan 6, 2009, at 3:15 PM, ab5tract wrote:
>  ip[0]
> end
>
> my_ip = IPAddr.new(get_ip)
>
> I had to do this as a work around before, though if I was working
> around an issue with Socket.gethostname or my own ignorance I can no
> longer tell ;

It is important to realize that this method won't take into account
any sort of NAT device that may be between you and an external website.
Whether that is important or not depends on the reason for needing an
IP address in the first place.
Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2009-01-07 07:03
Roger Pack wrote:
> Sijo Kg wrote:
>> Hi
>> Could you please tell the code to get the IPaddress of my machine

heh
it was for 1.9 anyway :)

let's see
require 'socket'
class Fixnum
 def ord
  self
 end
end
# the code above
Socket.get_host_ip
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.