4

I want to find out the default Gateway of client and DNS in jsp. Is there anyway to do that .I have find out IP address but i want to know the default gateway or DNS also. I have done following to find ip of client.

String ipAddress = request.getHeader("X-FORWARDED-FOR");  
String getWay = request.getRemoteAddr() ;   // Gateway
out.println("<br/>IP Address:"+ipAddress+"<br/>");
out.println("<br/>Gateway:"+getWay+"<br/>");
nagi
  • 381
  • 2
  • 8
  • 22
  • 1
    You used; request.getRemoteAddr() method? What it prints? – Shivam Oct 29 '15 at 08:42
  • Does this probably solve your problem: http://stackoverflow.com/questions/11930/how-can-i-determine-the-ip-of-my-router-gateway-in-java – Felix Gerber Oct 29 '15 at 08:42
  • @Shivam it gives me ip of client – nagi Oct 29 '15 at 08:43
  • @FelixGerber i want to find out the client gateway using ip or anything else. – nagi Oct 29 '15 at 08:47
  • That'll be difficult... You can only get this information on the *client*. The code you included runs on the *server*. You have to create some code which runs on the *client*. But if this is standard web traffic, the client runs only a web browser, and your web browser will not run .jsp code. – Laszlo Valko Oct 31 '15 at 01:12

2 Answers2

0

You can use this snippet and analyse the response:

p = Runtime.getRuntime().exec("ipconfig");
p.waitFor();

BufferedReader reader = 
     new BufferedReader(new InputStreamReader(p.getInputStream()));

String line = "";           
while ((line = reader.readLine())!= null) {
sb.append(line + "\n");
}
Georgi
  • 189
  • 2
  • 12
-1

Please go through the link below. Hope that answers your question: http://ireasoning.com/articles/find_local_ip_address.htm

The most reliable way is to use the route table. On windows, "route print" command gives us something like:

Runtime.getRuntime().exec("cmd.exe /c route print")

On linux, we can check the route table at /proc/net/route

BufferedReader reader = new BufferedReader(new FileReader("/proc/net/route"))

Aid325
  • 1
  • 2
  • this is the way to find rout of my network default gateway right? but i want to find any vsisting ip or client gateway – nagi Oct 30 '15 at 03:42
  • Which webserver are you using? – Aid325 Oct 30 '15 at 11:04
  • Since you are able to get the IPAddress, you could use the 'host -t ns ' command to get the DNS details, if you are on Linux/UNIX OS. – Aid325 Oct 30 '15 at 13:36