Function application.getIPAddress()

Questions, tips and tricks and techniques for scripting in Servoy

Function application.getIPAddress()

Postby gerardo.gomez » Thu May 27, 2010 1:58 pm

Hi all!

I'm in troubles with application.getIPAddress() function... When executed in a Windows or Mac client [with Smart Client], it returns right IP for client. Cool!
However, when executed in Linux (currently I'm using Ubuntu 10.04), it returns 127.0.0.1...

Clues:

1. Mac, Windows and Ubuntu machines are in the same local net...
2. all of them have ip in range 192.168.1.XXX
3. Mac and Windows clients return the right 192.168.1.XXX value...

Somebody knows what is happening???

Thanks in advance!!!
Best regards/Un saludo,

Gerardo Gómez
--
Servoy 5.1.4 build 964
Win7 - Java 1.6.0_20
Ubuntu 10.04 - Java 1.6.0_20
gerardo.gomez
 
Posts: 79
Joined: Thu Aug 20, 2009 1:15 am

Re: Function application.getIPAddress()

Postby lvostinar » Thu May 27, 2010 3:51 pm

gerardo.gomez wrote:Hi all!

I'm in troubles with application.getIPAddress() function... When executed in a Windows or Mac client [with Smart Client], it returns right IP for client. Cool!
However, when executed in Linux (currently I'm using Ubuntu 10.04), it returns 127.0.0.1...

Clues:

1. Mac, Windows and Ubuntu machines are in the same local net...
2. all of them have ip in range 192.168.1.XXX
3. Mac and Windows clients return the right 192.168.1.XXX value...

Somebody knows what is happening???

Thanks in advance!!!


I think is best to open a case in our support system, so we can have an in depth look at the issue. (http://www.servoy.com/s)
Laurian Vostinar
Servoy
lvostinar
 
Posts: 1062
Joined: Tue Feb 19, 2008 10:53 am

Re: Function application.getIPAddress()

Postby jcompagner » Thu May 27, 2010 4:20 pm

this is something that linux does by the way

Linux always returns the local address because i guess linux says what else to return (for example if there are more the 1 network adapter..)

For the same reason if you would use plain rmi and use linux as a server, you have to set the rmi.hostname property else the remote objects will get 127.0.0.1 as there server address...
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8833
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet

Re: Function application.getIPAddress()

Postby gerardo.gomez » Fri May 28, 2010 8:46 am

Hi!

lvostinar wrote:I think is best to open a case in our support system, so we can have an in depth look at the issue. (http://www.servoy.com/s)


Laurian, thanks for your answer!

jcompagner wrote:Linux always returns the local address because i guess linux says what else to return (for example if there are more the 1 network adapter..)


Johan, I didn't understand what you mean... :? In Win or Mac you can have several network adapter too... It isn't?

Do you think I must to open a case as Laurian said?
Best regards/Un saludo,

Gerardo Gómez
--
Servoy 5.1.4 build 964
Win7 - Java 1.6.0_20
Ubuntu 10.04 - Java 1.6.0_20
gerardo.gomez
 
Posts: 79
Joined: Thu Aug 20, 2009 1:15 am

Re: Function application.getIPAddress()

Postby jcompagner » Fri May 28, 2010 10:18 am

yes but linux does it it like that ..
In java we have: java.net.InetAddress.getLocalHost()

and that returns under windows my primary? netwerk card address but that call will result in linux with 127.0.0.1

The only thing i see that we could maybe use is:

java.net.NetworkInterface.getNetworkInterfaces();

But that will return many many adapters! Which one to get then?
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8833
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet

Re: Function application.getIPAddress()

Postby gerardo.gomez » Fri May 28, 2010 12:35 pm

After another test I found out maintenance plugin has the same behaviour:

combination of Linux & Smart client returns 127.0.0.1

Code: Select all
function onAction_getIp_btn(event) {

   // In Linux & Smart Client, this returns 127.0.1.1
   application.output(application.getIPAddress())
   
   var clients = plugins.maintenance.getConnectedClients();
   application.output('There are ' + clients.length + ' connected clients.');
   for (var i = 0; i < clients.length; i++)
   {
      var client = clients[i];
      application.output('Client details:');

      application.output('\tID: ' + client.getClientID());
      application.output('\tApplication type: ' + client.getApplicationType());

      // In Linux & Smart Client, this returns 127.0.1.1      
      application.output('\tHost address: ' + client.getHostAddress());

      application.output('\tHost identifier: ' + client.getHostIdentifier());
      application.output('\tHost name: ' + client.getHostName());
      application.output('\tUser name: ' + client.getUserName());
      application.output('\tUsed UID: ' + client.getUserUID());
   }
}


And data in admin is like this:

clients_smart_and_web_diferences.png
clients_smart_and_web_diferences.png (109.13 KiB) Viewed 5084 times
Best regards/Un saludo,

Gerardo Gómez
--
Servoy 5.1.4 build 964
Win7 - Java 1.6.0_20
Ubuntu 10.04 - Java 1.6.0_20
gerardo.gomez
 
Posts: 79
Joined: Thu Aug 20, 2009 1:15 am

Re: Function application.getIPAddress()

Postby gerardo.gomez » Fri May 28, 2010 3:05 pm

I think I got the "right" behaviour with application.getIPAddress() in Linux...

On the one hand, Web Client seems to return IP value in /etc/network/interfaces file for active interface.

On the other hand, Java (Smart Client) and hostname -i command are returning value from /etc/hosts (filled by default with 172.0.1.1 for hostname and 172.0.0.1 for localhost). Due to this, if you have a static IP in /etc/network/interfaces, you have to copy that value in /etc/hosts file...
If you have a dinamic IP (DHCP), you have to change the value in /etc/hosts with value returned with DHCP...

To sum up, IP values in /etc/hosts and /etc/network/interfaces have to be same value!
Best regards/Un saludo,

Gerardo Gómez
--
Servoy 5.1.4 build 964
Win7 - Java 1.6.0_20
Ubuntu 10.04 - Java 1.6.0_20
gerardo.gomez
 
Posts: 79
Joined: Thu Aug 20, 2009 1:15 am

Re: Function application.getIPAddress()

Postby ryanparrish » Fri Mar 04, 2011 8:33 pm

gerardo.gomez wrote:To sum up, IP values in /etc/hosts and /etc/network/interfaces have to be same value!


No they don't, and you can cause some really fun to diagnose issues with sudo and ssh playing with the hosts file.

I just strung this together, so there is probably a cleaner way to do it, but this will get the ip address of the interface that has the default route attached to it. It works on GNU ifconfig, BSD ifconfig (like OS X) is a little different in layout but some tweaks would make it work there also.

Just pass this to application.executeProgram(), the result will be your IP address.

Code: Select all
netstat -rn| grep "^0.0.0.0" | awk '{print $8}' | xargs -n1 ifconfig |grep "inet addr"| sed 's/addr://'|awk '{print $2}'


To break down what it is doing...

netstat -rn : prints out your routing table without looking up dns names
grep "^0.0.0.0" : finds the line that has the default route
awk '{print $8}' : grabs the 8th column which is the interface name
xargs -n1 ifconfig : passes that interface name to ifconfig
grep "inet addr" : find the line that has the IPv4 address
sed 's/addr://' : strip out the addr: to make the following awk easy
awk '{print $2}' : grab the second column, the ip address.
ryanparrish
 
Posts: 162
Joined: Thu May 17, 2007 7:49 pm
Location: Miami, FL


Return to Methods

Who is online

Users browsing this forum: No registered users and 12 guests

cron