Hi!
I want to create a link which automatically checks, which webserver listens behind a specific IP adress.
As you all know you can find out the webserver name by telnet-ing port 80 on the target machine. Thats what my PHP-script should do, and give me back the output (so that I can scan the output for the "Server:" line).
Unfortunetly I cant get the telnet output. If you have tried getting the server information via telnet you know that you have to hit <enter> once or twice to get something displayed in your commandline. Like:

telnet localhost 80
GET / HTTP/1.1 (you dont see what you are typing, but it works)
<enter><enter><enter>

Ok, now I tried to get this output to my var:

$output = shell_exec('telnet localhost 80 && "GET / HTTP/1.1\r\n"');
echo $output;

Unfortunetly that does not work. I guess it is because of the not-ending telnet session I normally quit with hitting <enter>. I tried to use "\r" and "\n" instead ... or

telnet localhost 80 && "GET / HTTP/1.1\r\n" && quit

but nothing worked. Do you have any idea ?
Many thanks for reading,

Reiners

    you might try fsockopen() to port 22 or port 23. fsockopen i think would block until the connection is made at which point you can put the newline chars and then close the socket.

    http://php.net/fsockopen

      You should use a proper HTTP implementation instead of writing your own. Fortunately PHP has one built in, which even speaks HTTP correctly (i.e. sends the mandatory 1.1 "Host" header).

      1. Create a stream context using stream_context_create or something like that
      2. Open the home page of the web site using fopen, with the context you created
      3. Fetch the metadata with stream_get_meta_data or something
      4. Find the "Server" line in the metadata and read the server out of that

      It's far more correct.

      You should not use IP addresses however, as most web servers won't send back a successful response if you query them by IP address because they use virtual hosting- use the host name instead (You may still get the Server: header though).

      Mark

        Write a Reply...