Ri-Del has asked for the wisdom of the Perl Monks concerning the following question:

I'm not even sure if I am asking this correctly, so please let me try and explain a little more. I've had minimal Perl experience and am trying to learn.

I am attempting to write a harvester that will move across my network so that I can keep an eye on what machines are up.

Essentially what I would like to figure out how to do is...

If I have a domain... foo.com and I have two other machines that exist as bar.foo.com and baz.foo.com. If I tell my script to look at foo.com it will print out some form of output showing that bar and baz exist.

I have been investigating Net::DNS, Sys::Hostname and so on in an attempt to figure this out on my own and after writing about 4 or 5 scripts that are useful, I still have not been able to accomplish what I am trying to do.

If anyone can point me to where I should go to learn how to do this or help me with how to actually write it, I would grateful.

  • Comment on How can I determine all the hosts on my network?

Replies are listed 'Best First'.
Re: How can I determine all the hosts on my network?
by mugwumpjism (Hermit) on Jun 15, 2001 at 12:35 UTC

    What you need to do is perform a zone transfer and extract all of the A records for the domain in question.

    #!/usr/bin/perl use Net::DNS; use Socket; use strict; my $domain = shift || "foo.com"; # Create a resolver object my $res = new Net::DNS::Resolver; # Ask the local resolver what the name servers for the domain in # question. my @ns; my $query = $res->query($domain, "NS") or die "query failed; ". $res->errorstring; for my $rr ($query->answer) { next if $rr->type ne "NS"; my $dotted_quad = join(".", (unpack("C4", gethostbyname($rr->nsdname)))); print "Found name server ".$rr->nsdname." ($dotted_quad)\n"; push @ns, $dotted_quad; } $res->nameservers(@ns); # ok, now we do a zone transfer my @zoneinfo = $res->axfr($domain) or die "query failed; ". $res->errorstring; # now loop through the answers and print those that are A records for my $rr (@zoneinfo) { next if $rr->type ne "A"; print "Found ". $rr->name ." A ".$rr->address."\n"; }

    An alternative method might be to process the output of the "nslookup" or "dig" commands.

Re: How can I determine all the hosts on my network?
by BMaximus (Chaplain) on Jun 15, 2001 at 11:26 UTC
    You might want to enable arpwatch on your server and then look in to arp.dat. I'm not really sure as to the format of the file. However looking in to the file with perl may be unnecessary as arpwatch itself notifies you through email of any changes.

    Here's an excerpt from the man page.

    Arpwatch keeps track for ethernet/ip address pairings. It syslogs activity and reports certain changes via email.

    This will keep track as to what machines you have. As far as monitoring them to make sure they're up or down. I would suggest using Big Brother rather than reinventing the wheel.

    BMaximus
Re: How can I determine all the hosts on my network?
by mattr (Curate) on Jun 15, 2001 at 18:18 UTC
    You could use a unix utility that would do a scan based on TCP commands. Or you could do a high level ping scan. If you know the machines are all going to be friendly you could try to connect to a given port.. Um, how big is your network?

    Why not check out nmap at insecure.org? Something like that can help you especially if you want to check the security of those hosts. You can also catch machines which have been set up to be quiet on your network, something which I don't think I'd trust ping to do. I know it's kind of like asking for a match and being given a tanker full of gasoline so be careful with the stuff. You should be able to parse the output file.

    Probably all you need to do is write your own script to ping a small company network for something short and sweet. But know that professional tools like those above are available.

Re: How can I determine all the hosts on my network?
by rreck (Initiate) on Jun 15, 2001 at 14:44 UTC
    One drawback to the "use DNS" approach is that machines that aren't in DNS or uncorrectly configured wont be detected. I can imagine if I were doing this I might try to cycle through possible IP addresses and use ping. I could write the thing I am talking about in about 15 minutes. It would be embarassingly crappy, yet functional. (that is why i yam heer). If you ask me nice enough I could probably get talked into it.
Re: How can I determine all the hosts on my network?
by archon (Monk) on Jun 15, 2001 at 23:23 UTC
    the best solution for you will depend on several things.

    if you are on the name server, you can parse the zone files.
    if your machines are all on the same subnet, you can check the broadcast address.
    you could do a zone xfer as someone else suggested.

    you said you are trying to see "what machines are up." you might want to look into netsaint or big brother. both of those require you to have a list of your IPs to start out with, though. your question implies maybe you have dynamic ips?

    you also said you had some scripts that were useful, but lacking. what point(s) are you stuck at?

      The point I was stuck at was that I had as of yesterday used objects in Perl for the first time, and had not quite figured out the Tao of what I was doing.

      I had tried some example scripts on how to use the DNS stuff and then had tried modifying them to see if I understood what they were doing and how to use them. So while I had figured out how to obtain my own hostname and resolve its ip address. I really couldn't find a way to do what I was attempting.

      However, since then I have discovered CPAN, learned how to install modules, and found that the world of Perl was far bigger than I had first thought.

      Thanks for responding, I appreciate it!

Re: How can I determine all the hosts on my network?
by Ri-Del (Friar) on Jun 16, 2001 at 00:06 UTC

    Well, thanks everyone for helping out! A special thank you to mugwumpjism, your script does exactly what I need. Plus I had to look up join and unpack so that I could learn what they did. =) Extra bonus!

    I will look into those other programs that were suggested (no sense in reinventing the wheel!). However, it sounds like what I really need to do is go buy a book on DNS, right? This is an internship I'm doing at a company, as the dedicated programmer for their IS department. So perhaps I could have solved this problem with some more knowledge of DNS? For example, I have no idea what a zone transfer is, so I'm going to go look that up as well.