Solved

getting Computer name in a network environment

Posted on 2010-11-10
38
1,600 Views
Last Modified: 2012-05-10
I support a business application that uses an Applet to retrieve end-user's computer name at login time using Java Applet. Since Applet takes time to load, we need to find another way of retrieving coputer name. Our application consists of JSPs. I used
InetAddress.getLocalHost().getHostName()
to get the computer name, it worked in my local environemt where the client and host were the same machine, but when I deployed it on the server, I get back the server name. I need to find a way how can I retrieve cleint's comuter name without using APPLET.

Thank you
0
Comment
Question by:javaCaravan0
  • 14
  • 12
  • 7
  • +2
38 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 200 total points
ID: 34108423
you can use one of the follwoing

request.getRemoteAddr();
request.getRemoteHost();

but theres no guarantee that the browser will include that information in the request header
0
 
LVL 10

Accepted Solution

by:
Pramod Kumar earned 300 total points
ID: 34109283
Use below javacript code to get the details.

To get the data on server you may:
1) send it to server using ajax
2) fill the data in hidden input box of a form and once the user submit the form, the data will go on server.
<html>
<head>
<script>
function test()
{
try
{
var ax = new ActiveXObject("WScript.Network");
document.write('User: ' + ax.UserName + '<br />');
document.write('Computer: ' + ax.ComputerName + '<br />');
}
catch (e)
{
document.write('Permission to access computer name is denied' + '<br />');
}
}
</script>
</head>
<body onload=test()>
</body>
</html>

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34110143
>> I need to find a way how can I retrieve cleint's comuter name without using APPLET.

There isn't a way without using client-side code such as an applet
0
 
LVL 10

Expert Comment

by:Hegemon
ID: 34111518
If you need to know the name of a _Windows_ PC in your _local_ network, you can get the client's IP and query the name by executing command line tools like nbtstat -A <IP>. This can be executed programmatically and the output captured.
0
 

Author Comment

by:javaCaravan0
ID: 34112436
As per the comments from all participant, it seems that CEHJ is write, there is no way to get the computer name without the client-side code. The solution provided by pramodkrjsr is a cient side code which can retrieve the computer name without running applet. Thanks pramodkrjsr.
I have tested it in my local. My only concern is that whenever I run the code, I get the following in the IE:
"An ActiveX control on this page might be unsafe to interact with the other parts of the page. Do you want to allow this interaction?"

How can I get rid of this message so that all our end-users accessing the application will not see this message.

Thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 34115367
>  it seems that CEHJ is write,

no he is wrong. You can get it from the request as I explained in my first comment
problem is that there is no guarantee of it being included in the quest (as also explained in my first comment)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34115515
You won't get it from the server side. The best you're going to get it is the IP address
0
 

Author Comment

by:javaCaravan0
ID: 34115684
I have tested the solution provided by pramodkrjsr  in my local. My only concern is that whenever I run the code, I get the following in the IE:
"An ActiveX control on this page might be unsafe to interact with the other parts of the page. Do you want to allow this interaction?"

How can I get rid of this message so that all our end-users accessing the application will not see this message.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34115720
Normally you can't get rid of such messages: they're by intent and are there as a security measure
0
 
LVL 10

Expert Comment

by:Hegemon
ID: 34115777
So the solution is to derive names from IPs using WINS or DNS as in my earlier comment.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34115846
If the applet is served from your *own network* then a server side lookup on the ip address should work
0
 

Author Comment

by:javaCaravan0
ID: 34115850
The solution is to use client side AJAX and retrieve the name of the machine running under Win OS.
Hrhremon: My app. is web based JSPs deriven, how can I using thew command line tools in my web app to pull the computer name? (I don't want to use APPLET)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34115881
*IS* the applet served from your own network?
0
 
LVL 92

Expert Comment

by:objects
ID: 34116037
once you have the ip address from the request you can use the InetAddress class

http://helpdesk.objects.com.au/java/lookup-hostname-by-ip-address
0
 
LVL 10

Expert Comment

by:Hegemon
ID: 34119151
Once the server hosting your application receives a request, your JSPs or Servlets should be able to get client's IP. Knowing this IP, a command line tool can be executed or DNS queried directly  - on the SERVER ! In other words, the server queries the network about what is known about this particular IP.
0
 

Author Comment

by:javaCaravan0
ID: 34125885
All,

1. I tried pramodkrjsr's solution, it works but I get the message as explained above, in order to use this solution, I need to find a way of not letting the message prompted, if anybody knows how to get rid of the message without changing the security setting, please let me know.

2. I tried the objects's solution but it didn't retrieve my computer name, instead it got server's IP address and server name!

I really need a solution. Please suggest some other solution if you all think that pramodkrjsr's or objects's solution can't be scuritinized to make that work.

Thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 34125941
> 2. I tried the objects's solution but it didn't retrieve my computer name, instead it got server's IP address and server name!

sounds like you're checking the wrong request. you should be looking up the ip returned from the client. you will likely need to send a request using js that include the client ip
0
 

Author Comment

by:javaCaravan0
ID: 34126002
objects:

This is my code that I used to test on the server, it doesn't return my copmuter name (so it will not return our client's computer name too, because all will be accessing from the same server):

<%@ page language="java" import="java.net.InetAddress" %>
<%
String IpAddress =  request.getRemoteAddr();
InetAddress address = InetAddress.getByName(IpAddress);
String hostname = address.getHostName();
out.println("request.getRemoteAddr()="+IpAddress);
out.println("InetAddress.getByName(IpAddress)="+address);
out.println("address.getHostName()="+hostname);
%>

Please correct me if I have done any mistake in coding.
0
 
LVL 92

Expert Comment

by:objects
ID: 34126024
Is the remote address included in the request? you'll need to send a request using js to make sure its included.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 86

Expert Comment

by:CEHJ
ID: 34126879
Try
String IpAddress =  request.getRemoteHost();

Open in new window

0
 

Author Comment

by:javaCaravan0
ID: 34130582
objects:
           Passing the IP address in JS and then using that IP address in the above code may work but how can I get the IP address using JavaScript? I think the problem will be solved if we could find a way of retrieving IP address using JS.

CEHJ: I used it in the above code but it returns server's IP address. I need a way of getting IP address using JS.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34130610
>>CEHJ: I used it in the above code but it returns server's IP address.

It will give you the host address of the client. Unfortunately, because of the way networks work, it won't necessarily be the actual client box's ip - it could be a gateway address
0
 
LVL 92

Expert Comment

by:objects
ID: 34132630
>  but how can I get the IP address using JavaScript?

you don't. you need to get the browser to include as I mentioned earlier. Using a XMLHttpRequest has a better chance of doing that. Still no guarantees as I explained in my first comment

> CEHJ: I used it in the above code but it returns server's IP address. I need a way of getting IP address using JS.

And its already been discussed.
0
 

Author Comment

by:javaCaravan0
ID: 34163388
Object:

> you need to get the browser to include as I mentioned earlier. Using a XMLHttpRequest has a better chance of doing that. Still no guarantees as I explained in my first comment

  sorry but I don't understand what you mean by "you need to get the browser to include as I mentioned earlier".

> Using a XMLHttpRequest has a better chance of doing that.

How can I use XMLHttpRequest  to get the IP address. Do you mean using this: var ax = new ActiveXObject("WScript.Network");
In that case, as I have mentioned, I always get the message that I don't want.
If there is a way to capture the IP address of the machine, I can use that IP address in JSP to get the machine name. This is where I'm stuck.

What I have tested so far, does't work 100%.

1. using ActiveXObject("WScript.Network") to get the computer name always shows a warning message as explained earlier.

2.  if I pass IP address in "InetAddress address = InetAddress.getByName(IpAddress); " , I'm able to capture the coputer name, but to make this work, I need a way of gettting the IP address using Javascript.

This is very critical, If anyone knows how to do this, please let me know.




0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34163413
>>but to make this work, I need a way of gettting the IP address using Javascript.

Why do you need to use javascript?
0
 

Author Comment

by:javaCaravan0
ID: 34163961
is ther anyother way? How can I get the IP address.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34164027
To avoid going around in circles:

a. it's not possible to get the computer name client-side without running privileged code and that will be noticeable to the client. That means an applet or JavaScript
b. it can be possible to get the remote ip address on the server side using request.getRemoteHost
c. if the request is coming from the same network, the address obtained in b. should be resolvable to a name

Now, you said that the address in b. was that of the server. That is rather strange - unless of course, the request you inspected *did* come from the server ...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34164054
>>unless of course, the request you inspected *did* come from the server ...

(Which would naturally happen if testing on the server itself rather than a separate client on your network)
0
 

Author Comment

by:javaCaravan0
ID: 34164729
I deployed the application on the server and tested it from my comuter.

Do u think this is an issue with JBOSS AP. Please refer to this site: (it is an issue with Oracle AS)
http://aboutoracle.blogspot.com/2006/11/tip-requestgetremoteaddr-and.html
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34165975
>>
uncomment the line :

UseWebCacheIp On
>>

Did you do that?
0
 

Author Comment

by:javaCaravan0
ID: 34166682
where can I find this line in JBOSS AS (what xml file do I have to look at?)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34166738
That's to do with Oracle, not JBoss
0
 
LVL 10

Expert Comment

by:Hegemon
ID: 34166820
request.getRemoteAddr() should return client's IP unless the JBoss server is fronted by another web server, most likely Apache. Also see this discussion:

http://www.coderanch.com/t/445496/Tomcat/request-getRemoteAddr
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34166839
>> (it is an issue with Oracle AS)

Are you actually using that?
0
 

Author Comment

by:javaCaravan0
ID: 34166916
no, I'm using JBOSS AS
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34166995
I thought you were - since you posted a link about it ;). Just make sure that nothing along the same lines is happening in your case
0
 
LVL 92

Expert Comment

by:objects
ID: 34168666
> How can I use XMLHttpRequest  to get the IP address

http://www.jibbering.com/2002/4/httprequest.html
0
 

Author Comment

by:javaCaravan0
ID: 34273702
I'll be closing it by next week
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count8 challlenge 13 87
countPairs challenge 7 58
best (free) software to access postgres db (java) 1 18
create a gui in perl 3 46
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now