wolverina has asked for the wisdom of the Perl Monks concerning the following question:

I'm changing directories using chdir, and printing out the path. I want the path to show the IP address, and not the host name. Is there a way to do this? Im using this line at the moment...
chdir("$ENV{DOCUMENT_ROOT}/dir1/dir2") || die "Can't open = $!\n";
which prints out www.myhostname.com/dir1/dir2. I want the script to print out myIPaddress/dir1/dir2. -Lisa.

Replies are listed 'Best First'.
Re: Host IP address
by bobn (Chaplain) on Dec 11, 2003 at 23:15 UTC

    perldoc -f gethostbyname
    or Net::DNS

    --Bob Niederman, http://bob-n.com

    All code given here is UNTESTED unless otherwise stated.

      thanx.. got it. ;) -Lisa
Re: Host IP address
by zengargoyle (Deacon) on Dec 12, 2003 at 00:16 UTC

    also keep in mind that a hostname may map to multiple IP addresses and a hostname may map to different IP addresses at different times. this may not give you problems.

Re: Host IP address
by davido (Cardinal) on Dec 11, 2003 at 23:11 UTC
    The script could perform a DNS lookup on $ENV{DOCUMENT_ROOT}.

    Always exercise caution in trusting $ENV variables though. Some are wholly unreliable much of the time, while others can be easily spoofed by a custom script trying to behave like a browser.

    That means that you could be exposing yourself to a security issue by passing 'chdir' data directly from an environment variable. Put the -T switch in the shebang line of your script and watch the fireworks the next time you run it.


    Dave

      "Always exercise caution in trusting $ENV variables though. Some are wholly unreliable much of the time, while others can be easily spoofed by a custom script trying to behave like a browser."
      Correct me if I'm wrong, but a DOCUMENT_ROOT is quite hard to spoof client side, since it is a server side variable, unlike for example HTTP_USER_AGENT. In the latter case, the script relies on (l)user input, and thus it's easy to spoof. DOCUMENT_ROOT is specified by the http daemon, not the client.

      "That means that you could be exposing yourself to a security issue by passing 'chdir' data directly from an environment variable."
      In this specific case, I doubt you're exposing yourself to a security risk.

      --
      b10m
Re: Host IP address
by b10m (Vicar) on Dec 12, 2003 at 11:10 UTC
    AFAIK, DOCUMENT_ROOT will only print out the absolute path to your, well, document root. This will not print the server's hostname. So I presume you have a directory called 'www.myhostname.com'. (E.g. /usr/local/www/www.myhostname.com/).

    Of course you could use a DNS lookup (as suggested) on that portion of the path, but I tend to dislike that, if this script is only used on a few directories of yourself with fixed ip addresses. Each time you run the script, the DNS query will be performed and the script will have to wait for it's output. This can be time consuming, not to mention the bandwidth you waste ;)

    I would suggest to simply use the IP address instead of the $ENV{DOCUMENT_ROOT} if possible, so use something like:
    my $ip = "123.123.123.123"; chdir("$ip/dir1/dir2") || die "Can't open = $!\n";
    If you have multiple directories (hostnames), you might want to create a hash with all the ip addresses you use.
    %ip_address = ('www.myhostname.com','123.123.123.123','www.herhostname +.net','456.456.456.456');
    So you can look it up way faster ($ip_address{'www.myhostname.com'} will give "123.123.123.123").

    This, of course, is handy for simple scripts with just a few hostnames and fixed ip addresses. If you have a lot, it can be time consuming to enter it all, so forget about bandwidth and use the DNS lookup, as suggested by others ;)

    --
    b10m