// JP opened flex table

Click to See Complete Forum and Search --> : How to get the name of the web server ?


yna54
February 5th, 2002, 08:46 AM
How to get the name of the server running in a port in a host, for example I
wrote a program that scan all the ip address that start from 155.108.000.000
for the port 80, how to know if the web server that is running is IIS,
Apache or Tomcat.
thanks

Norm
February 5th, 2002, 10:16 AM
Do a HTTP GET to the server and look at the Server: Response Header field.

Norm

yna54
February 5th, 2002, 10:32 AM
THANKS , could you please show me how to do it ?
Here is the function that scan port in ip address :

public static void scan(InetAddress remote) {
int[] ports = {80, 8080};
String hostname = remote.getHostName();
for (int i = 0 ; i < 2; i++) {
System.out.println("current port scanning is :" + ports[i]);
try {
Socket s = new Socket(remote, ports[i]);
System.out.println("A server is listening on port " + ports[i]
+ " of " + hostname);
if (ports[i] == 80)
System.out.println("Maybe a WEB SERVER is running in this hostname " + " " + hostname + " " + "at port" + " " + ports[i]);
s.close();
}
catch (IOException e) {
// The remote host is not listening on this port
}
}

}





Thanks

Norm
February 5th, 2002, 10:56 AM
Once you have connected to the server's port you need to send the HTTP GET request and read the HTTP response from the server. Get an HTML book to find the syntax for the HTTP protocol or look online for its RFC.
Or you could use the URLConnection class to do the GET and retrieve the response header info using one of its methods like getHeaderField().

The HTTP Get request is something like:
GET / HTTP/1.0<crlf><crlf>


Norm

yna54
February 5th, 2002, 12:57 PM
I used getHeaderField(int n), but could not understand what the int n is for, I found out the web servers are not the same, for example for IIS I have to set n=1 and for apache I have to set n=2, could you please explain , thanks

Norm
February 5th, 2002, 06:07 PM
You need to understand what a HTTP Response header looks like. Also if you would read the doc for the getHeaderField you would see what it does. You really need to read the doc for methods you use, not just copy them off the forum.

Here's what is in my doc for that method:

getHeaderField

public String getHeaderField(int n)

Returns the value for the nth header field. It returns null if there are fewer than n fields.

This method can be used in conjunction with the getHeaderFieldKey method to iterate through all the headers in the message.

Parameters:
n - an index.Returns:
the value of the nth header field.See Also:
getHeaderFieldKey(int)

Give the above you see that you also need to use the getHeaderFieldKey() method to get the key value for the ith header field.
What you are seeing is that different servers send the header fields in different order. Use the get..key() method to find the index for "Server" and then use the get..Field() method to get its value.


Norm

//JP added flex table