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

Fellow Monks, I have a script that gets a list of all servers in the domain from the PDC. My first question... When i get the results to pipe out to a text file...it is printed as ServerName1$,ServerName2$. the $ is screwing me up. How can i get the $ to disappear. I ultimitaly want to read in this file in another script and resolve the Server name to ip address. I can't seem to get the ip address resolver to work right either. Can anyone help. Here are both my scripts and thanks in advance. Ray
# Ray Espinoza # GetDomainServers.pl # Get servers in my domain and and sends the output to a #text file. ############################################################ use Win32::NetAdmin; open (OUT,">list.txt") || die "Cannot create machine list. :$!"; # This will get the list of machine names from the PDC my @users; Win32::NetAdmin::GetUsers("\\\\PDC",'',\@users); my @computers = grep { /\$$/ } @users; @computers = join("\n",@computers); print OUT @computers; close(OUT); # Ray Espinoza # getip.pl # this will take a file and read it in and resolve hostname # to ip address ##################################################################### print "What file do you want to read?"; chomp($InFile = <STDIN>); open(IN,$InFile) || die "Cannot open $InFile: $!"; open(OUT,">results.txt") || die "Cannot create results.txt: $!"; while (<IN>) { ($addr) = (gethostbyname($hostname))[4]; print OUT "$hostname 's address is ", join(".",unpack("C4", $addr)), "\n"; } close(IN); close(OUT);

Replies are listed 'Best First'.
Re: getting server names in the domain and looking up ips
by tachyon (Chancellor) on Aug 07, 2001 at 21:42 UTC

    This will get rid of the terminal $ for you. It is also a good idea to specify a path when opening files.

    use Win32::NetAdmin; my $path = 'c:'; my $file = 'list.txt'; open ( OUT, ">$path/$file" ) || die "Cannot create machine list. :$!"; my @users; Win32::NetAdmin::GetUsers("\\\\PDC",'',\@users); my @computers = grep { /\$$/ } @users; for my $box (@computers) { chop $box; # removes terminal char, must be $ from grep print OUT "$box\n"; # print each $box on a newline } close OUT;

    In the second script you read from a file using a while loop but never look at the contents of that file! Each line will be assigned to $_ in turn. You need to chomp off the newline as well.

    while (<IN>) { my $line = $_; chomp $line; # now do something with $line which we got from the file }

    As a final note the join is not doing what I think you think it is doing :-) Join returns a scalar not a list thus assigning it to an array does not make. You will note that after the join the whole array is in element 0.

    @ary = qw(1 2 3 4 5); print "first ", $ary[0], "\n"; @ary = join("\n",@ary); print "second", $ary[0], "\n";

    Hope this helps

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: getting server names in the domain and looking up ips
by VSarkiss (Monsignor) on Aug 07, 2001 at 21:42 UTC

    Well, getting rid of the $ isn't too bad. Just nail it after you read in the list. (TIMTOWTDI, but this is simple and effective):

    my @computers = grep { /\$$/ } @users; s/\$$// foreach @computers; # remove trailing $ @computers = join("\n",@computers);

    As for gethostbyname, it returns an array of IP addresses for the hostname (remember, there may be more than one). An easy way to cope is to move the elements you want using perlfunc:splice:

    @addr = gethostbyname($hostname); # note: an array now splice(@addr, 0, 4); # get rid of other stuff foreach (@addr) { print OUT join('.', unpack('C4', $_)), "\n"; }

    HTH

      i made some of the changes that you suggested and it still doesn't work. I am still getting the $ after the server names. and the ip stuff is returning my own ip address. Any help is appreciated. Thanks again, Ray
Re: getting server names in the domain and looking up ips
by rchiav (Deacon) on Aug 07, 2001 at 21:58 UTC
    Both above answers are good. (I checked to make sure no one else posted before I hit the submit button :)

    Here's another TIMTOWTDI approach to the original problem..

    #!/usr/bin/perl -w use strict; use Win32::OLE qw(in); my ($domain, $container, $i); $domain = 'YourDomain'; $container = Win32::OLE->GetObject("WinNT://$domain"); $container->{FILTER} = ["computer"]; foreach my $i (in $container) { print "$i->{NAME}\n"; }
    This uses ADSI via Win32::OLE. No trailing $ for the machine account. Take a look at this and scroll down to "IADsComputer" to see what else is available for machine information. ADSI isn't the answer for everything, but it's handy for the quick things like this.

    Rich