How can script find its host's name/IP address?

K

kj

How can script find the hostname (or at least the IP address) of
the host it is running on?

TIA,

kj
 
K

kj

How can script find the hostname (or at least the IP address) of
the host it is running on?


Sorry, I shouldn't have written "hostname"; what I want is the full
hostname+domainname combination, as in foo.bar.com (as opposed to
just foo).

Also, I should have mentioned that I looked at the Perl FAQ, but
didn't get a satisfactory answer to my question. The Perl FAQ does
include a question on how to get the "hostname/domainname/IP
address", but it doesn't really answer the question of how to get
the domainname. It only explains how to get the hostname and the
IP address. It gives advice on how to get the name of the local
DNS server, but says nothing on how that script would use this
information to obtain the host's domainname.

kj
 
M

Matt Garrish

kj said:
How can script find the hostname (or at least the IP address) of
the host it is running on?

The same way you'd do it in any other language: read the environment
variables.

Matt
 
K

KKramsch

The same way you'd do it in any other language: read the environment
variables.

What makes you think there is an environment variable that has the
domainname info? When I run a script on my Linux box that prints
all of %ENV, not a single %ENV value shows the machine's domain
name.

Karl
 
G

Greg Beeker

I'm not sure how to get this into perl, but you may use the shell
script line below:
nslookup `hostname` | grep Name | awk '{print $2}'
I tested this on AIX51 and Solaris 8, and it gives the fully qualified
hostname.domainname.
 
C

Chris Mattern

kj said:
How can script find the hostname (or at least the IP address) of
the host it is running on?

TIA,

kj

One thing to remember is that your host doesn't have an IP address--
network interfaces have IP addresses. One solution that comes to
me is simply invoking the external ifconfig (or whatever the
equivalent might be on your system) command, which will
tell you all about all the addresses the various network interfaces
on your host may have. Which one you want to pick out is then,
of course, up to you.
--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
M

Matt Garrish

KKramsch said:
^^^^^^^^
^^^^^^^^^^^^

What makes you think there is an environment variable that has the
domainname info? When I run a script on my Linux box that prints
all of %ENV, not a single %ENV value shows the machine's domain
name.

What does the domain name have to do with the question as he presented it?

print $ENV{SERVER_NAME};

or if you're on IIS using asp

$Request->ServerVariables("server_name")

Matt
 
C

Chris Mattern

Matt said:
What does the domain name have to do with the question as he presented it?

print $ENV{SERVER_NAME};

or if you're on IIS using asp

$Request->ServerVariables("server_name")
The OP has not stated anywhere that this is a CGI script.

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
B

Brian McCauley

kj said:
Sorry, I shouldn't have written "hostname"; what I want is the full
hostname+domainname combination, as in foo.bar.com (as opposed to
just foo).

Also, I should have mentioned that I looked at the Perl FAQ, but
didn't get a satisfactory answer to my question. The Perl FAQ does
include a question on how to get the "hostname/domainname/IP
address", but it doesn't really answer the question of how to get
the domainname. It only explains how to get the hostname and the
IP address. It gives advice on how to get the name of the local
DNS server, but says nothing on how that script would use this
information to obtain the host's domainname.

The built-in function gethostbyaddr can be used to get from ip address
to FQDN provided everything is correctly configured and running.

This thread, of course, has little to do with Perl. This is more of an
OS issue.

Perhaps it would help if you could tell us what it is you are going to
do with this information and on what operating system.
 
M

Matt Garrish

Tad McClellan said:
Perl is not CGI.

CGI is not Perl.

I never would have guessed...

I read the OP's poor first attempt at a question as a stealth cgi question.
If I'm wrong on that account; I'm wrong. However, contrary to the followup
reply, there most definitely *is* an environment variable *in the cgi
environment* to find the hostname. Clearer?

Matt
 
K

KKramsch

I never would have guessed...
I read the OP's poor first attempt at a question as a stealth cgi question.
If I'm wrong on that account; I'm wrong. However, contrary to the followup
reply, there most definitely *is* an environment variable *in the cgi
environment* to find the hostname. Clearer?

Before you get your panties in a wad, read my "followup reply"
again: I need not mention CGI either (because I did not read the
original post a "stealth cgi question").

Karl
 
A

Alan J. Flavell

However, contrary to the followup reply, there most definitely *is*
an environment variable *in the cgi environment*

OK, as the thread has drifted to a stealth CGI discussion (regardless
that the original question almost certainly wasn't)...
to find the hostname.

You're talking about the host where the CGI script is running, rather
than the address of the client or proxy that's making the request,
right?

This of course is nothing specific to Perl. But, for the convenience
of Perl users, FAQ9 refers, and points to the relevant CGI resources.
I'd have to recommend looking them up to get the fuller picture.

You're talking about the SERVER_NAME, I take it?

best-practice CGI spec (draft):
http://cgi-spec.golux.com/draft-coar-cgi-v11-03-clean.html#6.1.15
 
M

Matt Garrish

Alan J. Flavell said:
OK, as the thread has drifted to a stealth CGI discussion (regardless
that the original question almost certainly wasn't)...


You're talking about the host where the CGI script is running, rather
than the address of the client or proxy that's making the request,
right?

This of course is nothing specific to Perl. But, for the convenience
of Perl users, FAQ9 refers, and points to the relevant CGI resources.
I'd have to recommend looking them up to get the fuller picture.

You're talking about the SERVER_NAME, I take it?

That is what started all this. Hence my original reply:

"The same way you'd do it in any other language: read the environment
variables."

And the follow up where I pointed that variable out. The confusion seems to
lie with the fact that I didn't specifically say "cgi environment" the first
time around (for those, like the person who responded, who were not aware of
what I was geting at).

I still look at the sparsity of the original post and see someone who wants
to write a (portable) cgi script that doesn't require knowing the server
it's running on in advance. As I said, if I'm wrong, I'm wrong. I had to
correct the poster who claimed that such a thing does not exist, though.

And I did find it a bit galling that I get told that Perl != CGI when that
was the gist of my original reply, so my apologies to Tad if my response was
a bit blunt.

Matt
 
J

Joe Smith

kj said:
How can script find the hostname (or at least the IP address) of
the host it is running on?

For the IP address(es):

unix% perl -le '$ENV{PATH}.=":/sbin:/usr/sbin"; /inet (?:addr:)?(\S+)/
and $1 ne "127.0.0.1" and push @a,$1 foreach `ifconfig -a`; print "@a"'

C:\>perl -le "/IP Address.*: ([0-9.]+)/ and push @a,$1 foreach
`ipconfig`; print \"@a\""

-Joe
 
K

KKramsch

In said:
I still look at the sparsity of the original post and see someone who wants
to write a (portable) cgi script that doesn't require knowing the server
it's running on in advance. As I said, if I'm wrong, I'm wrong. I had to
correct the poster who claimed that such a thing does not exist, though.

And who was that poster, pray tell, and where did he/she claim that
"such a thing does not exist"?

Karl
 

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

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top