Discussion:
How to get the IP address of the machine where the code is running?
Fred Janon
2008-11-20 13:07:36 UTC
Permalink
I would like to find out the IP address (the real one not 127.0.0.1 or
localhost) of the Windows machine my Grails application (in Tomcat) is
running on. I tried different methods in the Request but I get either
127.0.0.1 or localhost.

Is there an easy way to get that IP address either from the request or
context or in Groovy with a cool trick?

Thanks

Fred
Robert Fischer
2008-11-20 13:13:55 UTC
Permalink
There's no particularly great way to do this, because the actual world-visible IP can be set far,
far away from your particular server.

My suggestion would be to scrape the page linked to here: http://whatismyip.com/automation.asp

My stronger suggestion is to say you shouldn't do this at all: I'm yet to encounter a place where
it's a particularly good idea. What's the purpose?

~~ Robert.
Post by Fred Janon
I would like to find out the IP address (the real one not 127.0.0.1
<http://127.0.0.1> or localhost) of the Windows machine my Grails
application (in Tomcat) is running on. I tried different methods in the
Request but I get either 127.0.0.1 <http://127.0.0.1> or localhost.
Is there an easy way to get that IP address either from the request or
context or in Groovy with a cool trick?
Thanks
Fred
--
~~ Robert Fischer.
Smokejumper Consulting http://smokejumperit.com
Enfranchised Mind Blog http://enfranchisedmind.com/blog
LinkedIn Profile http://www.linkedin.com/in/robertfischer
Twitter Feed http://twitter.com/robertfischer

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Michael Kimsal
2008-11-20 13:55:15 UTC
Permalink
I'd concur with Robert - use the domain name of the application you're
running and hit an external service to determine what the outside
world *thinks* your IP is. The domain name should be coming in some
sort of server/header map. in PHP, I'd run $_SERVER['HTTP_HOST'],
which would give me the host the user used to get to my app. If it
was an domain name, I'd get that. If it was an IP address
(69.93.219.178, for example), I'd get that as well.

Aha - I knew I had this somewhere:
http://michaelkimsal.com/blog/grails-for-php-developers/grails-for-php-developers-part-1/

Well, that doesn't show it entirely, but
request.getHeaders("Host")
will give you a string back that has the hostname (domain or IP) used
to access your app.
Post by Robert Fischer
There's no particularly great way to do this, because the actual
world-visible IP can be set far, far away from your particular server.
My suggestion would be to scrape the page linked to here: http://whatismyip.com/automation.asp
My stronger suggestion is to say you shouldn't do this at all: I'm
yet to encounter a place where it's a particularly good idea.
What's the purpose?
~~ Robert.
Post by Fred Janon
I would like to find out the IP address (the real one not 127.0.0.1
<http://127.0.0.1> or localhost) of the Windows machine my Grails
application (in Tomcat) is running on. I tried different methods in
the Request but I get either 127.0.0.1 <http://127.0.0.1> or
localhost.
Is there an easy way to get that IP address either from the request
or context or in Groovy with a cool trick?
Thanks
Fred
--
~~ Robert Fischer.
Smokejumper Consulting http://smokejumperit.com
Enfranchised Mind Blog http://enfranchisedmind.com/blog
LinkedIn Profile http://www.linkedin.com/in/robertfischer
Twitter Feed http://twitter.com/robertfischer
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
==========================
Michael Kimsal
http://www.groovymag.com
for groovy and grails developers
919-455-8488


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
houbie
2008-11-20 13:49:43 UTC
Permalink
Post by Fred Janon
I would like to find out the IP address (the real one not 127.0.0.1 or
localhost) ...
The simple answer: InetAddress.getLocalHost().getHostAddress().

The reality: your host can have multiple network interfaces (wired, wireless
...) and hence multiple IP addresses. So getLocalHost() will select (almost)
ramdom one of them if you don't specify the interface explicitly.

Grtz,

Ivo
--
View this message in context: http://www.nabble.com/How-to-get-the-IP-address-of-the-machine-where-the-code-is-running--tp20600752p20601442.html
Sent from the grails - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Fred Janon
2008-11-21 01:23:31 UTC
Permalink
Thanks to all for the interesting answers and references that I will
definitely study and keep for future reference. I ended up using Ivo's
simple (I love the KISS principle) and it works in my environment:
java.net.InetAddress.getLocalHost().getHostAddress()

I added that line in the layout file main.gsp:

<div class="appaddress">Tip: To access the application from another
computer on the same local network, use this address in a web browser:

${request.getScheme()}://${java.net.InetAddress.getLocalHost().getHostAddress()
+ ((request.getLocalPort() != 80) ? ':' + request.getLocalPort() : '') +
request.getContextPath()}</div>

I wrote that application for a small business (just a few people in an
office), the Tomcat server with the Grails application are hosted on one of
the staff's computer, not networked at the moment but will be in the near
future. As they use another company to maintain their computers and to
install the network, I wanted to provide them with a very simple help to
find the application on the network even if the server changes IP address.
So the message above shows the application URL to use, even from the same
computer where the app lives. The users just have to copy that address in
the browser of the other computers when the LAN is in place.

Thanks again all for your help.

Fred
Post by houbie
Post by Fred Janon
I would like to find out the IP address (the real one not 127.0.0.1 or
localhost) ...
The simple answer: InetAddress.getLocalHost().getHostAddress().
The reality: your host can have multiple network interfaces (wired, wireless
...) and hence multiple IP addresses. So getLocalHost() will select (almost)
ramdom one of them if you don't specify the interface explicitly.
Grtz,
Ivo
--
http://www.nabble.com/How-to-get-the-IP-address-of-the-machine-where-the-code-is-running--tp20600752p20601442.html
Sent from the grails - user mailing list archive at Nabble.com.
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
Brad Whitaker
2008-11-21 05:08:13 UTC
Permalink
This java based code will find all network interfaces and associated IP
addresses:

NetworkInterface nface
Enumeration ne = NetworkInterface.getNetworkInterfaces();
while (ne.hasMoreElements()) {
NetworkInterface netFace = (NetworkInterface)
ne.nextElement();
log.debug("FOUND NetworkInterface:
${netFace.getDisplayName()} ${netFace.getName()}")
Enumeration ae = netFace.getInetAddresses();
while (ae.hasMoreElements()) {
InetAddress address = (InetAddress) ae.nextElement();
log.debug("FOUND IP address:
${address.getHostAddress()}");
}
}
Post by houbie
Post by Fred Janon
I would like to find out the IP address (the real one not 127.0.0.1 or
localhost) ...
The simple answer: InetAddress.getLocalHost().getHostAddress().
The reality: your host can have multiple network interfaces (wired,
wireless ...) and hence multiple IP addresses. So getLocalHost() will
select (almost) ramdom one of them if you don't specify the interface
explicitly.
Grtz,
Ivo
--
View this message in context: http://www.nabble.com/How-to-get-the-IP-address-of-the-machine-where-the-code-is-running--tp20600752p20615349.html
Sent from the grails - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Robert Fischer
2008-11-21 05:16:37 UTC
Permalink
For future warning, please note that the IP addressed returned by these methods may be *very
different* than the IP address the internet thinks you have.

~~ Robert.
Post by Brad Whitaker
This java based code will find all network interfaces and associated IP
NetworkInterface nface
Enumeration ne = NetworkInterface.getNetworkInterfaces();
while (ne.hasMoreElements()) {
NetworkInterface netFace = (NetworkInterface)
ne.nextElement();
${netFace.getDisplayName()} ${netFace.getName()}")
Enumeration ae = netFace.getInetAddresses();
while (ae.hasMoreElements()) {
InetAddress address = (InetAddress) ae.nextElement();
${address.getHostAddress()}");
}
}
Post by houbie
Post by Fred Janon
I would like to find out the IP address (the real one not 127.0.0.1 or
localhost) ...
The simple answer: InetAddress.getLocalHost().getHostAddress().
The reality: your host can have multiple network interfaces (wired,
wireless ...) and hence multiple IP addresses. So getLocalHost() will
select (almost) ramdom one of them if you don't specify the interface
explicitly.
Grtz,
Ivo
--
~~ Robert Fischer.
Smokejumper Consulting http://smokejumperit.com
Enfranchised Mind Blog http://enfranchisedmind.com/blog
LinkedIn Profile http://www.linkedin.com/in/robertfischer
Twitter Feed http://twitter.com/robertfischer

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Mingfai
2008-11-21 06:37:26 UTC
Permalink
but it's usually good enough. I suppose for most use case, people want to
get the server IP to identify the server or for any network operations,
rather than showing the server IP to the user. So they don't need to care
the IP that the Internet think they are. If they do, it's better to use
domain name for that purpose.



On Fri, Nov 21, 2008 at 1:16 PM, Robert Fischer <
Post by Robert Fischer
For future warning, please note that the IP addressed returned by these
methods may be *very different* than the IP address the internet thinks you
have.
~~ Robert.
Post by Brad Whitaker
This java based code will find all network interfaces and associated IP
NetworkInterface nface
Enumeration ne = NetworkInterface.getNetworkInterfaces();
while (ne.hasMoreElements()) {
NetworkInterface netFace = (NetworkInterface)
ne.nextElement();
${netFace.getDisplayName()} ${netFace.getName()}")
Enumeration ae = netFace.getInetAddresses();
while (ae.hasMoreElements()) {
InetAddress address = (InetAddress) ae.nextElement();
${address.getHostAddress()}");
}
}
Post by houbie
Post by Fred Janon
I would like to find out the IP address (the real one not 127.0.0.1 or
localhost) ...
The simple answer: InetAddress.getLocalHost().getHostAddress().
The reality: your host can have multiple network interfaces (wired,
wireless ...) and hence multiple IP addresses. So getLocalHost() will
select (almost) ramdom one of them if you don't specify the interface
explicitly.
Grtz,
Ivo
--
~~ Robert Fischer.
Smokejumper Consulting http://smokejumperit.com
Enfranchised Mind Blog http://enfranchisedmind.com/blog
LinkedIn Profile http://www.linkedin.com/in/robertfischer
Twitter Feed http://twitter.com/robertfischer
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
Fred Janon
2008-11-21 08:32:54 UTC
Permalink
Fair warning Robert, I understand. In my case the few computers are on on
the same LAN using one router, not visible from the Internet, so like
Mingfai out it, it's good enough in my case and not catastrophic if it
doesn't work properly: I'll get a phone call and will have to spend less
than an hour at my client's to fix them up.

Fred
Post by Mingfai
but it's usually good enough. I suppose for most use case, people want to
get the server IP to identify the server or for any network operations,
rather than showing the server IP to the user. So they don't need to care
the IP that the Internet think they are. If they do, it's better to use
domain name for that purpose.
On Fri, Nov 21, 2008 at 1:16 PM, Robert Fischer <
Post by Robert Fischer
For future warning, please note that the IP addressed returned by these
methods may be *very different* than the IP address the internet thinks you
have.
~~ Robert.
Post by Brad Whitaker
This java based code will find all network interfaces and associated IP
NetworkInterface nface
Enumeration ne = NetworkInterface.getNetworkInterfaces();
while (ne.hasMoreElements()) {
NetworkInterface netFace = (NetworkInterface)
ne.nextElement();
${netFace.getDisplayName()} ${netFace.getName()}")
Enumeration ae = netFace.getInetAddresses();
while (ae.hasMoreElements()) {
InetAddress address = (InetAddress) ae.nextElement();
${address.getHostAddress()}");
}
}
Post by houbie
Post by Fred Janon
I would like to find out the IP address (the real one not 127.0.0.1 or
localhost) ...
The simple answer: InetAddress.getLocalHost().getHostAddress().
The reality: your host can have multiple network interfaces (wired,
wireless ...) and hence multiple IP addresses. So getLocalHost() will
select (almost) ramdom one of them if you don't specify the interface
explicitly.
Grtz,
Ivo
--
~~ Robert Fischer.
Smokejumper Consulting http://smokejumperit.com
Enfranchised Mind Blog http://enfranchisedmind.com/blog
LinkedIn Profile http://www.linkedin.com/in/robertfischer
Twitter Feed http://twitter.com/robertfischer
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
Fred Janon
2008-11-21 08:33:37 UTC
Permalink
Thanks Bard, I'll definitely keep your code snippet handy.

Fred
Post by Brad Whitaker
This java based code will find all network interfaces and associated IP
NetworkInterface nface
Enumeration ne = NetworkInterface.getNetworkInterfaces();
while (ne.hasMoreElements()) {
NetworkInterface netFace = (NetworkInterface)
ne.nextElement();
${netFace.getDisplayName()} ${netFace.getName()}")
Enumeration ae = netFace.getInetAddresses();
while (ae.hasMoreElements()) {
InetAddress address = (InetAddress) ae.nextElement();
${address.getHostAddress()}");
}
}
Post by houbie
Post by Fred Janon
I would like to find out the IP address (the real one not 127.0.0.1 or
localhost) ...
The simple answer: InetAddress.getLocalHost().getHostAddress().
The reality: your host can have multiple network interfaces (wired,
wireless ...) and hence multiple IP addresses. So getLocalHost() will
select (almost) ramdom one of them if you don't specify the interface
explicitly.
Grtz,
Ivo
--
http://www.nabble.com/How-to-get-the-IP-address-of-the-machine-where-the-code-is-running--tp20600752p20615349.html
Sent from the grails - user mailing list archive at Nabble.com.
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
Continue reading on narkive:
Loading...