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

I'm running the simple (so I thought) program below to build a list with all my servers. I don't want servers that begin with "w" nor "l" showing on the list. Here's my code: open (PP, "net view |") || die "$!\n"; while (<PP>){ $machine = split; if ($machine =~ m@^\\^wl.@i){ push(@servers,$machine); } } close PP || die "$!\n"; open (OUT,">servers.log") || die "$!\n"; while (@servers){ print OUT "$_\n"; } print "List built!\n"; close OUT || die "$!\n"; I think the problem is with the regexp but I can't see it! Can someone help me out? Thanks

Replies are listed 'Best First'.
Re: Net View Parsing
by athomason (Curate) on May 24, 2000 at 02:44 UTC
    The problem with your regexp is that since you're matching two backslashes, you need four in the regexp. I've condensed your code a bit; this should work for what you need.
    open (PP, "net view |") || die "$!\n"; my @servers = map /^(\\\\[^lw].*?)\W/i, <PP>; close PP || die "$!\n"; open (OUT,">servers.log") || die "$!\n"; print OUT join "\n", @servers; print "List built!\n"; close OUT || die "$!\n";
    If you know for sure that net will be installed and working, you could shorten the first three lines to my @servers = map /^(\\\\[^lw].*?)\W/i, `net view`;
      Thanks! Seems like I'm doing something else wrong becouse even fixing the regexp, with the tip you gave me, I can't get the list of servers.
        Well, let's be a bit more diagnostic then. First, what is the format of a line of output from "net view" on your machine? I'm running W2K; you may be seeing different output than I am. Second, try this snippet:
        open (PP, "net view |") || die "$!\n"; print <PP>;
        You should just see the output of 'net view'. If not, try backquotes like I mentioned at the end of my previous post. If that still doesn't work, make sure 'net' is actually in the path as seen by the script. The regexp is pretty generous: all it checks for is two leading backslashes followed by not 'l' or 'w'. Assuming there are computers that match that description, something should get printed. If you're still having troubles, come back with some more details on how it fails.
ActiveState Perl has modules for Win32 Networking
by Corion (Patriarch) on May 24, 2000 at 16:42 UTC
    You don't need to parse the output of NET VIEW, since (at least) the ActiveState Perl build has a module Win32::NetResource which can do network enumeration and even network connections (like (un)mapping a new network drive). This might of course be too much, as NET VIEW already does produce the output you need and does all the error handling, but if you maybe want finer control and maybe are not too interested in running under DOS or Windows 3.1, that module could be of interest to you.
RE: Net View Parsing
by BBQ (Curate) on May 24, 2000 at 09:36 UTC
    This is the 1st time I've ever seen someone quote regex with an @. I know you can do it any way you like, but don't you find it a bit confusing? I always thought the standard was the exclamation mark...

    i.e.: m!whatever!gi

    #!/home/bbq/bin/perl
    # Trust no1!
Re: Net View Parsing
by Robert (Initiate) on May 24, 2000 at 22:32 UTC
    Folks, Thanks a lot for your help.