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

I'm trying to help with a small forensics script and I'm having problems resolving a list of IP addersses to machine names on our network. Below is what I found on a web site that says it SHOULD work but it just locks up my command prompt (Windows XP). All I want to do is scan an IP and get the machine name back- I could care less about anything else. Is there a simple way to do with without the use of a non-standard module? Our environment does not allow us too much flexibility on installing mods.
#!/usr/bin/perl use warnings; use strict; my @tracert = `tracert ####`; my $i; foreach $i(@tracert) { print "$i\n"; }

Replies are listed 'Best First'.
Re: Tracert or resolve machine name
by ikegami (Patriarch) on May 19, 2009 at 16:04 UTC

    it just locks up my command prompt (Windows XP).

    Your program takes a long time to run because tracert takes a long time to run.

    All I want to do is scan an IP and get the machine name back-

    tracert has nothing to do with that. You want gethostbyaddr.

    use Socket qw( inet_aton AF_INET); my $addr = '209.197.123.153'; my ($name) = gethostbyaddr(inet_aton($addr), AF_INET) or do { die "No name for addr $addr\n" if $!==0; die "gethostbyaddr: $!\n"; }; print("$name\n"); # perlmonks.pair.com
Re: Tracert or resolve machine name
by mr_mischief (Monsignor) on May 19, 2009 at 13:51 UTC
    You probably want nslookup rather than tracert if all you want is a name service lookup and not a trace of a route. That's not exactly a Perl question. Oh, and if you mean by "forensics script" that you're just mapping your network, then fine. If you mean "forensics" as in you're an "expert witness" providing evidence and testimony in someone's trial, you should probably resign and let someone more familiar with the tools of the trade take over.
Re: Tracert or resolve machine name
by Utilitarian (Vicar) on May 19, 2009 at 13:51 UTC
    How long does it lock up the terminal for? This script just runs traceroute and displays the output from a string. If no route is found it should display that and exit. Otherwise it's taking a while to find a route the host #####

    EDIT:
    You could add a -w $milliseconds and a -h 30 to the tracert in order to limit the amount of time taken to return ie:

    perl -e "@trace=`tracert -w 200 -p 30 perlmonks.org`;foreach $i (@trac +e){print \"$i\n\";}"

      On another w32 system:

      perl -e "@trace=`tracert -w 200 -p 30 perlmonks.org`;foreach $i (@trac +e){print \"$i\n\";}" -p is not a valid command option. Usage: tracert [-d] [-h maximum_hops] [-j host-list] [-w timeout] targ +et_name Options: -d Do not resolve addresses to hostnames. -h maximum_hops Maximum number of hops to search for target. -j host-list Loose source route along host-list. -w timeout Wait timeout milliseconds for each reply.

      Remove the -p 30 (did you mean -h 30 as suggested by your text? If so, that appears to be the default and is thus unnecessary) and the output (omitting the first few hops) is as expected:

      Tracing route to perlmonks.org [66.39.54.27] over a maximum of 30 hops +: ... 9 59 ms 59 ms 59 ms 0.so-5-2-0.XL3.IAD8.ALTER.NET [152.63. +36.25] 10 70 ms 77 ms 73 ms pairnetworks-gw.customer.alter.net [63 +.65.185.2] 11 * * * Request timed out. 12 75 ms 71 ms 73 ms ads.perlmonks.org [66.39.54.27] Trace complete.

      which underscores the value of mr_mischief's suggestion.

Re: Tracert or resolve machine name
by imrags (Monk) on May 20, 2009 at 11:05 UTC
    try Socket
    This module can help you to resolve IP address/DNS etc.
    Raghu