Getting IP address from local machine using katalon studio

I want to get local machine IP and pass it into in one of the test case.
Please let me know , how to get IP address of the local machine?
Is there any command in the Katalon studio?

Thanks in adavance

Getting IP address from local machine

You see to have an misunderstanding: the IP Address is nowhere written in your local machine.

But you can ask some web site to tell the IP Address of your machine exposed public to the Internet. The following code asks AWS.

You can copy & paste the following as a Test Case, run it.

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

String askAWSforMyIP() {
	URL url = new URL("http://checkip.amazonaws.com/")
	BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))
	return br.readLine().trim()
}

WebUI.comment(askAWSforMyIP())

I got:

2022-11-14 17:54:42.598 INFO  c.k.k.c.keyword.builtin.CommentKeyword   - 163.131.xx.xxx

This approach would not work if you want to know your IP Address in the local private network.

Please check if the below solution suit’s you need.

java.net.InetAddress i = java.net.InetAddress.getLocalHost();
System.out.println(i.getHostAddress());

This printed this:

2022-11-14 21:06:37.892 DEBUG testcase.checkip                         - 2: i = InetAddress.getLocalHost()
2022-11-14 21:06:37.912 DEBUG testcase.checkip                         - 3: out.println(i.getHostAddress())
127.0.0.1

I guess, 127.0.0.1 is not what the original poster wanted to know.

@kazurayam , In my case it is different, as below.

Are you sure?

$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever

3: enp0s20f0u4u1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 3c:e1:a1:4a:c8:5b brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.112/24 brd 192.168.1.255 scope global dynamic noprefixroute enp0s20f0u4u1
       valid_lft 63998sec preferred_lft 63998sec
    inet6 fe80::c060:df8b:2c37:940a/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

4: wlp61s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 50:76:af:30:43:69 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.120/24 brd 192.168.1.255 scope global dynamic noprefixroute wlp61s0
       valid_lft 67127sec preferred_lft 67127sec
    inet6 fe80::d6ae:9df3:65b2:86ef/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

This shows that, in addition to the 127.0.0.1 ip address (standard for the loopback interface known as localhost but can be changed) I have also two other local IP’s, one for the wireless interface and one for the wired interface.

Of-course, are not the public IP’s as seen on internet, but are there and are needed for basic connectivity.

Your machine is connecting directly to Internet? I doubt … or you have a very expensive plan from your internet provider.

And of-course, the public IP is different:

$ dig +short myip.opendns.com @resolver1.opendns.com
XXX.XXX.XXX.XXX

Side note, for a single interface I can add more than one IP, if those are not assigned through DHCP but are static.
Just saying …

Ah! I was wrong. I was confused by looking at 127.0.0.1.

Windows OS resolves IP Address usually using DHCP on startup and caches the value as long as it is up and running. So my IP Address is cached locally, and should be availalbe to Java programs as well.

According to this document, 127.0.0.1 is the localhost address which will not be used to communicate with others. If you just mean to test the test cases locally and do not want to do any routers / calls externally, return this IP Address will be safe. If not, the IP Address that @discover.selenium has figured out, the real one, can be used.

@basavaraj.araballi

By the way, why do you want to do this?

I have no idea why.


The reason why you need “IP Address” will determine which IP Address you want, and how you should resolve it. Unless you explain the reason, then it is difficult for others to give you any appropriate advise.

Well … I never had a real use case for this, I only know is possible, examples:

On windows you can set also static IP addresses (actually on any OS you can do that)
To inspect the interfaces, I think the equivalent command of ifconfig or the more modern ip from linux is ipconfig, play with that in the command prompt.

Any device connected to a certain ethernet network should have a local IP address, either static or assigned by DHCP. This is required for Layer3 connectivity.
When you reach out another device on the local network you will be routed through the gateway on the local network to the targeted device.

When you reach out to internet, then you will be routed from the local gateway to the provider gateway and out through the provider public IP, so your local IP is NATed to the provider public IP.

Now, back on the topic:

To access / list network interfaces, java.net.* libraries are very handy, e.g:

So, having the list of all network interfaces, you can filter out the loopback (lo)interface and check the IP’s of the others.
Anyway, note that, as shown in my previous example, you can have more than one interface active at the same time (and any interface can have more than one IP, but that is a very rare case)
Another thing to consider is, if you have static IP’s configured, your interface will still own it, even if is disconnected (status Down) so you may like to filter, in addition, for only active interfaces.

Things are getting more interesting if you are using also VPN and / or virtualization.
In addition to previous shown, on my local machine I have two more interfaces:

5: gpd0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 500
    link/none 
    inet6 fe80::8760:4a86:f658:bef1/64 scope link stable-privacy 
       valid_lft forever preferred_lft forever
6: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:93:3f:4e brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever

gpd0 is the virtual adapter created by my VPN client. I am currently not connected to VPN so have status UNKNOWN and no IP (but that will change if I will connect it)

The vibr0 interface is the virtual bridge created by virt-manager (I am using qemu to virtualize various stuff I need)

Note that, despite the status Down (I don’t have any VM started now), it have also an IP assigned, 192.168.122.1 which acts as a gateway for my virtual machines.
So, on Windows, if you are using VirtualBox of Hyper-V to fire up some virtual machines, you should be aware also by such cases.

Have fun!