A couple of quick comments:
- Your pattern match does not account for the fact that the Listen directive may also include a listening port, ie: 1.2.3.4:8080
- You don't tell us anything about your gethost() method. Are you sure that it always returns a meaningful value?
Here is a bit of working (tested) code that emulates what you are trying to do (except for poulating the hash), and does hostname lookups using gethostbyaddr. Perhaps this will help:
#!/usr/bin/perl -w
use strict;
use Socket;
while (<DATA>) {
if (/^Listen/) {
chomp;
my ($ip) = $_ =~ m/^Listen\s([\d\.]+):?/;
my $host = gethostname($ip) || "unknown";
print "IP:$ip HOST:$host\n";
}
}
sub gethostname {
my $ip = shift;
my $iaddr = inet_aton($ip);
return gethostbyaddr($iaddr, AF_INET);
}
__DATA__
Listen 1.2.3.4
Listen 5.6.7.8:9735
rubbish
Listen 209.131.36.158
Listen 11.12.1.4:8080
more rubbish
Output:
IP:1.2.3.4 HOST:unknown
IP:5.6.7.8 HOST:unknown
IP:209.131.36.158 HOST:f1.www.vip.sp1.yahoo.com
IP:11.12.1.4 HOST:unknown
To check the contents of your hash, you might consider using Data::Dumper.
Cheers,
Darren
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.