Get the system Ip

I am not able to get system Ip, System I always getting changed which has dynamic port
url.openStream(); throws exception as unknown source

023-08-11 19:57:21.220 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------
2023-08-11 19:57:21.227 INFO c.k.katalon.core.main.TestCaseExecutor - START Test Cases/ClientUI/User Permissions/IP
2023-08-11 19:57:22.161 DEBUG testcase.IP - 1: comment(getSystemIP())
2023-08-11 19:57:22.162 DEBUG testcase.IP - 1: url = new java.net.URL(https://checkip.amazonaws.com/)
2023-08-11 19:57:22.172 DEBUG testcase.IP - 2: conn = url.openConnection()
2023-08-11 19:57:22.792 DEBUG testcase.IP - 3: conn.setRequestProperty(“accept-language”, “en-US,en;q=0.9”)
2023-08-11 19:57:22.838 DEBUG testcase.IP - 4: conn.setRequestProperty(“user-agent”, “MyJavaApp”)
2023-08-11 19:57:22.840 DEBUG testcase.IP - 5: conn.setConnectTimeout(10)
2023-08-11 19:57:22.841 DEBUG testcase.IP - 6: ins = url.openStream()
2023-08-11 19:57:23.060 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/ClientUI/User Permissions/IP FAILED.
Reason:
java.net.UnknownHostException: checkip.amazonaws.com
at java_net_URL$openStream$0.call(Unknown Source)
at IP.getSystemIP(IP:28)
at IP.run(IP:44)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:448)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:439)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:418)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:410)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:285)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:144)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:135)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1691764036373.run(TempTestCase1691764036373.groovy:25)

2023-08-11 19:57:23.072 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/ClientUI/User Permis

My Code looks like

String getSystemIP() {
URL url = new URL(‘https://checkip.amazonaws.com/’)
URLConnection conn = url.openConnection();

conn.setRequestProperty("accept-language", "en-US,en;q=0.9");
conn.setRequestProperty("user-agent", "MyJavaApp");
conn.setConnectTimeout(10);
InputStream ins = url.openStream();
InputStreamReader isr = new InputStreamReader(ins);
BufferedReader websiteText = new BufferedReader(isr);
System.out.println(websiteText.readLine());

return websiteText.readLine().trim()

}

WebUI.comment(getSystemIP())

1 Like

Hi,

Any one experienced can help?

1 Like

URLConnection.setConnectTimeout(int timeout)

timeout value is in milliseconds.

Your code has

conn.setConnectTimeout(10);

Do you mean 10 miliseconds?
Maybe not.
If you want 10 seconds, then you should write

conn.setConnectTimeout(10000);

I just removed the line “conn.setConnectTimeout(10)” and tried; it ran successful. So you don’t need to specify the connection timeout explicitly.

The following code will return the IP Address for the Given URL

import java.net.*
String url=“www.javatpoint.com
InetAddress ia=InetAddress.getByName(url)
String ip=ia.getHostAddress()
println(ip)

or Use

import java.net.;
import java.io.
;
try
{
URL url = new URL(“https://checkip.amazonaws.com”);
URLConnection conn = url.openConnection();

conn.setRequestProperty(“accept-language”, “en-US,en;q=0.9”);
conn.setRequestProperty(“user-agent”, “MyJavaApp”);
conn.setConnectTimeout(10);
InputStream ins = url.openStream();
InputStreamReader isr = new InputStreamReader(ins);
BufferedReader websiteText = new BufferedReader(isr);
System.out.println(websiteText.readLine());

}catch(Exception e)
{

}
2 Likes

The above code will print the IP address of the remote host “www.javapoint.com”, which is not the one the original poster wants.

On the other hand https://checkip.amazonaws.com is a special URL which responds a string that contains the IP address of the requester, which is most probably your local PC.

This code will give you your local ip then ?

1 Like

I don’t see what you mean “your local ip”


As for “https://checkip.amazonaws.com”, see

The service checkip.amazonaws.com provides the public IP address of the client making the request.


I tried this code and it failed. The connection failed to esstablish due to the line

conn.setConnectTiout(10);

How to fix? You want to remove this line, or rewrite it to

conn.setConnectTiout(10000);

What do you mean by ‘local IP’?
This has to be clarified first.
E.g in my linux machine i can run something like:

$ ip a
---
4: enp7s0f3u1u1: <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 enp7s0f3u1u1
       valid_lft 78609sec preferred_lft 78609sec
    inet6 fe80::c416:ace4:bcdb:4ca8/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
---

This will provide me the IP addresses assigned to my networking cards and I filtered the output for the one currently in use.
I will name this ‘local IP’. Note that it may not be a single value.
It can change based on my local router settings (and ofcourse, based on the networking card in use, wifi, lan1, lan2 etc)

If this is the case, you may like to take a look at the discussions here:

Hower, on internet I appear a bit different, due to the other various gateways in place up to the internet provider.
So doing something like:

$ host myip.opendns.com resolver1.opendns.com
Using domain server:
Name: resolver1.opendns.com
Address: XXX.XXX.XXX.XXX#53
Aliases:
myip.opendns.com has address YYY.YYY.YYY.YY

Gives me some other values, the IP of the DNS resolver in use (xxx) and the public IP, as internet ‘see me’ (the YYY, whiuch should be same regardless of the query service I may use)
This can change based on the Internet provider settings and networking details, e.g if I am connected to VPN or not.

So, which one do you actually neeed?
Once we have this answer, we can look for a propper java solution, the proposed solutions up to now are returning just the public IP by using the AWS resolver, which is a public service.

I’m not getting any error and I added this solution because he tried to get IP from the same code he shared.

1 Like

The value in the ‘output’ is a public IP. Period.
Aside of that, since it starting with 34. looks like is an AWS public IP. I doubt it is the actual value you need … but what do I know.
Compare that with what you may get by typing on Google ‘my ip’ and look for something like:
IP

Another period.

If you are happy with it, that’s fine.

How long the timeout value of URLConnection.setConnectionTimeout(int timeout) will be in usal cases? See the following post, for example,

They write

urlConn.setConnectTimeout(15000);

timeout of 15 seconds looks usual to me. But, may be, too long?.

To me,

conn.setConnectTiout(10);

does not make a good sense. This requires super high-speed network connection to the public Internet.

In my case, the code failed because my home network gives me a very slow connection; The timeout of 10 milli sec is too strict on my home network.

Let me show you what I tried:

I could get my IP address via https://checkip.amazonaws.com. This worked OK.

That seems to be a Class B ip from the public range, see:

I think is OK, I had doubts only on the value obtained by the OP (34.x.x.x are usually AWS public IP’s, it may make sense only if the test was executed from an AWS VM).

To double check, compare with the value you get by opening the URL in browser (but I think you already did that) or use the Google way or whatever other public web service.

For the local / private IP’s, something like ifconfig | grep "inet " should work to display them on Mac.
To retrieve them by java code, there are some hints in the topic I mentioned previously.

I used https://whatismyipaddress.com/ to search 34.171.7.133 's ownership.

The following result revealed the IP Address is from the host ending with .bc.googleusercontent.com

It seems that @bharathi.a ran the code on a VM on Google Compute Engine service, not AWS EC2.

1 Like

Well, then the result they get I think is valid also, for a public IP.
I guess, class A may be preffered by cloud services providers, for a wider broadcast per network compared with class B which apparently is used by most of internet providers.
(this may not be allways true, the IP assigned by my provider is also from class A)

I was tricked by the 34 network which usually I saw it in use by AWS.

E.g if I ping a certain service I maintain (which I know for sure it is hosted in AWS) i get:

$ ping my-r53-something.net
PING my-aws-ballancer.com (34.xxx.xxx.xxx) 56(84) bytes of data.

Although, the checkip service himself uses two different networks, also from class A:

$ ping checkip.amazonaws.com
PING checkip.eu-west-1.prod.check-ip.aws.a2z.com (63.32.206.44) 56(84) bytes of data.

$ ping checkip.amazonaws.com
PING checkip.eu-west-1.prod.check-ip.aws.a2z.com (52.31.145.120) 56(84) bytes of data.

This is interresting …

For the rest, we can only wait to see what @deepaveerappa.kadrol actually needs, the public one or the local one.

1 Like

Even after using above url,
its displaying the same exception:
url is not supported in this Katalon tool

For trouble shooting, please visit: https://docs.katalon.com/katalon-studio/docs/troubleshooting.html

09-17-2023 09:22:53 PM Test Cases/ClientUI/Login/Login_Startscreen Device_17_18/Start screen device_setup

Elapsed time: 1.895s

Test Cases/ClientUI/Login/Login_Startscreen Device_17_18/Start screen device_setup FAILED.
Reason:
java.net.UnknownHostException: checkip.amazonaws.com
at java_net_URL$openStream$0.call(Unknown Source)
at Start screen device_setup.run(Start screen device_setup:31)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:448)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:439)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:418)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:410)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:285)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:144)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:135)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1694965967704.run(TempTestCase1694965967704.groovy:25)

1 Like

did you read the history of this topic carefully an understood what may be the difference between a local ip and a public ip?

why are you trying to login into a service which should only provide you a numeric value?

Hi Bionel,

I am not logging in, I am creating device in my application which needs system IP.
Dynamically i need to pass that Ip to create the device

2 Likes