Script that shows IP address of a server.

B

Blue

First of all, I am not a Perl programmer. I have searched Google for 40
minutes but could not find this simple code.

I need a simple script that show the IP address of the server (where the
script is residing).

I believe this is something very simple but I just could not find it.

Can someone please help?

Thanks.
 
T

Tony Curtis

Blue said:
First of all, I am not a Perl programmer. I have searched Google for 40
minutes but could not find this simple code.

I need a simple script that show the IP address of the server (where the
script is residing).

I believe this is something very simple but I just could not find it.

Which address? It's a network interface that has an address, not the
machine itself.

hth
t
 
B

Brian McCauley

A server does not have "an" IP address.
Most hosts have not less than two IP addresses, and many have more.
My development machine has five interfaces each with an IP address.

You can make a TCP connection to somewhere then look at the address of
the local socket. This should give you a fair guess the IP address
"facing" your chosen target.
 
B

Blue

Sorry about the confusion. I found the solution with PHP. Thanks.

<html>
<body>
<font face=arial size=2>
<?php
$server = $_SERVER['SERVER_ADDR'];
print "<b>SERVER_ADDR:</b> $server";
print "<p>";
$http = $_SERVER['HTTP_HOST'];
print "<b>HTTP_HOST:</b> $http";
print "<p>";
$remote = $_SERVER['REMOTE_ADDR'];
print "<b>REMOTE_ADDR:</b> $remote";

?>
 
B

Brian McCauley

Sorry about the confusion. I found the solution with PHP.

Oh, so your question wasn't related to Perl.

When an HTTP server executes server-side code (either embedded in a
templating system or standalone like GCI) then the contextual
information that the web server passes to the executable code typically
will incude the IP addresses of the local and remote ends of the TCP/IP
connection on which the HTTP request was received.

For example the GCI interface will populate environment variables.

Ob-perl: in Perl envirnment variables are presented as the hash %ENV.

However it is very hard to think of a use for this particular bit of
information other than perhaps in debugging some particularly obscure
networking issues. Given your apparent level of understanding I doubt
you are doing this.

So, I'm curious, what value do you think this information will be to
you?
 
S

Stan R.

Brian said:
Oh, so your question wasn't related to Perl.

When an HTTP server executes server-side code (either embedded in a
templating system or standalone like GCI) then the contextual
information that the web server passes to the executable code
typically will incude the IP addresses of the local and remote ends
of the TCP/IP connection on which the HTTP request was received.

For example the GCI interface will populate environment variables.

Ob-perl: in Perl envirnment variables are presented as the hash %ENV.

However it is very hard to think of a use for this particular bit of
information other than perhaps in debugging some particularly obscure
networking issues. Given your apparent level of understanding I doubt
you are doing this.

So, I'm curious, what value do you think this information will be to
you?

s/GCI/CGI/g; ?
 
S

Stan R.

At the foundation, the OP's question was even more broken than
originally thought. He asked (looking upthread a few steps),

"I need a simple script that show the IP address of the server (where
the script is residing)"

At no point in the question did he mention any of the the clue words
"Web, HTTP, Apache, or CGI" which would have led to the (indeed
simple) solution he was looking for.

What he REALLY wanted to say was:

"I need to know the IP address of the server-side socket for a live
HTTP connection to a web server."

Actually, while he said he's used PHP in the end, PHP /is/ capable of
running outside of a web server, though I suppose in such case the
$SERVER variable wouldn't be valid anymore? Even so, I think this was a
little too "rush to judgment" esque, albeit not without reason.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top