perldoc -f gethostbyname or Net::DNS
--Bob Niederman, http://bob-n.com All code given here is UNTESTED unless otherwise stated.
| [reply] |
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.
| [reply] |
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.
| [reply] |
"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.
| [reply] |
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 ;)
| [reply] [d/l] [select] |