# Getting ServerIP Dynamically

**URL:** <https://www.ruby-forum.com/t/getting-serverip-dynamically/138344>\
**Category:** Rails\
**Created:** [May 22, 2008, 6:18am UTC](https://www.ruby-forum.com/t/getting-serverip-dynamically/138344 "2008-05-22T06:18:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrSBless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@MrSBless](https://www.ruby-forum.com/u/MrSBless)\
**Post date:** [May 22, 2008, 6:18am UTC](https://www.ruby-forum.com/t/getting-serverip-dynamically/138344/1 "2008-05-22T06:18:30Z")

</div>

How to get the server Ip address dynamically.

---

<div class="post-metadata">

**Author:** ![MrSBless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@MrSBless](https://www.ruby-forum.com/u/MrSBless)\
**Post date:** [May 22, 2008, 9:28am UTC](https://www.ruby-forum.com/t/getting-serverip-dynamically/138344/2 "2008-05-22T09:28:23Z")

</div>

Mr. Bless wrote:

> How to get the server Ip address dynamically.

what operating system do u use?..do u have a dhcp server in your  
network?

if you have a dhcp server and your server operating system is linux,  
then type

dhclient in the terminal…it will fetch you a ip address dynamically…

---

<div class="post-metadata">

**Author:** ![MrSBless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@MrSBless](https://www.ruby-forum.com/u/MrSBless)\
**Post date:** [May 22, 2008, 10:22am UTC](https://www.ruby-forum.com/t/getting-serverip-dynamically/138344/3 "2008-05-22T10:22:18Z")

</div>

Can you elaborate on what you are trying to accomplish?

---

<div class="post-metadata">

**Author:** ![MrSBless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@MrSBless](https://www.ruby-forum.com/u/MrSBless)\
**Post date:** [May 22, 2008, 10:29am UTC](https://www.ruby-forum.com/t/getting-serverip-dynamically/138344/4 "2008-05-22T10:29:21Z")

</div>

I am using window. My requirement is to send the email with the link  
including activation code when the user sign up for an account. Right  
now it sends [http://localhost:3000/activation\_code](http://localhost:3000/activation_code) now I want to  
include server ip address instead of localhost

On May 22, 12:28 pm, Mahalingam Mr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![MrSBless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@MrSBless](https://www.ruby-forum.com/u/MrSBless)\
**Post date:** [May 22, 2008, 1:55pm UTC](https://www.ruby-forum.com/t/getting-serverip-dynamically/138344/5 "2008-05-22T13:55:39Z")

</div>

Mahalingam Mr wrote:

> mrbless wrote:
> 
> > I am using window. My requirement is to send the email with the link  
> > including activation code when the user sign up for an account. Right  
> > now it sends [http://localhost:3000/activation\_code](http://localhost:3000/activation_code) now I want to  
> > include server ip address instead of localhost
> > 
> > On May 22, 12:28 pm, Mahalingam Mr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> if the server is in windows look for the httpd.conf file in it & edit  
> the servername from localhost to your servers ip address…

I believe the original poster is wanting to know how to do it  
dynamically. I use the following method to build URLs:

require ‘ipaddr’

def wgg\_build\_url(resource)  
begin  
ip = IPAddr.new(request.host)  
rescue  
ip = nil  
end

```
root_url =
  if ip && ip.ipv4?
    "#{request.protocol}#{request.host}#{request.port_string}"
  else
    sub_domain = request.subdomains.first ? 

```

“#{request.subdomains.first}.” : ‘’  
“#{request.protocol}#{sub\_domain}#{request.domain}#{request.port\_string}”  
end

```
resource[0] = '' if resource.at(0) == '/'
return "#{root_url}/#{resource}"

```

end

It’s something I wrote, so feel free to use it, rename it, change it,  
whatever.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![MrSBless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@MrSBless](https://www.ruby-forum.com/u/MrSBless)\
**Post date:** [May 22, 2008, 1:02pm UTC](https://www.ruby-forum.com/t/getting-serverip-dynamically/138344/6 "2008-05-22T13:02:19Z")

</div>

mrbless wrote:

> I am using window. My requirement is to send the email with the link  
> including activation code when the user sign up for an account. Right  
> now it sends [http://localhost:3000/activation\_code](http://localhost:3000/activation_code) now I want to  
> include server ip address instead of localhost
> 
> On May 22, 12:28 pm, Mahalingam Mr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

if the server is in windows look for the httpd.conf file in it & edit  
the servername from localhost to your servers ip address…

---

<div class="post-metadata">

**Author:** ![MrSBless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@MrSBless](https://www.ruby-forum.com/u/MrSBless)\
**Post date:** [May 23, 2008, 8:03am UTC](https://www.ruby-forum.com/t/getting-serverip-dynamically/138344/7 "2008-05-23T08:03:51Z")

</div>

Okay I hate to ask for more details but the server ip address can be  
several things. First of all the IP address that your RoR web server  
is binding to may not be the correct IP address depending on what you  
are trying to do. If you are going to access your web server from your  
internal network, then yes the IP address that your RoR web server is  
binding to will most likely be the correct one. However, if your web  
server is serving requests to the outside (for example, to others on  
the internet) you will most likely have a different “server” IP  
address that is provided to you by your ISP (this would be your public  
IP address). Then you must make sure that you have the appropriate  
port mapping rules (eg. firewall rules) that will route port 80 (or  
any port like 3000) on the public IP address to your computer’s IP  
address’s port 3000 that is running the RoR web server.

In either case, if you are using apache I would change the  
‘servername’ (also suggested by Mahalingam) to the either one of the  
two possible IP address I mentioned above, either the local computer’s  
internal IP address or the public IP address.

Thanks,  
Gregg

On May 22, 1:28 am, mrbless [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > what operating system do u use?..do u have a dhcp server in your  
> > network?
> 
> > if you have a dhcp server and your server operating system is linux,  
> > then type
> 
> > ## dhclient in the terminal…it will fetch you a ip address dynamically…
> > 
> > Posted viahttp://www.ruby-forum.com/.

On May 22, 4:02 am, Mahalingam Mr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![MrSBless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@MrSBless](https://www.ruby-forum.com/u/MrSBless)\
**Post date:** [July 5, 2008, 8:11am UTC](https://www.ruby-forum.com/t/getting-serverip-dynamically/138344/8 "2008-07-05T08:11:53Z")

</div>

ok

---

<div class="post-metadata">

**Author:** ![MrSBless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@MrSBless](https://www.ruby-forum.com/u/MrSBless)\
**Post date:** [May 23, 2008, 10:32am UTC](https://www.ruby-forum.com/t/getting-serverip-dynamically/138344/9 "2008-05-23T10:32:36Z")

</div>

Thanks alot Guys for your time and suggestion. Thank you…
