Ever since i learned perl people at my company have been asking me to compile different information on our Network of Windows computers. I've collected a group of subroutines that i use over and over. i finally decided to package all of them together. apologies for lack of pod this is pretty beta.

So far this is all very windows specific and it does shell out to the Ps tools collection of software, so some routines here require that be installed. Also `net view` is called which should be avilible on most windows computers by default.

If anything here can be done via a standard module let me know. I tried to avoid excessive system calls, but was not entirely successful. Since i just grep through the return values from psinfo on some calls, I worry that they may eventually break with another version of that toolkit :(

An example script:

#!/usr/bin/perl -w use strict; use NetQuery; my $nq = new NetQuery; my @list = $nq->get_all_pcs(); for(@list){ if ($nq->is_alive($_)) { printf("%20s \t %20s\n",$_,$nq->get_clock_speed($_)); } }

And the Module:

package NetQuery; use strict; use Win32::TieRegistry(Delimiter=>"/"); use Socket; use Net::Ping; sub new{ my ($class) = shift; my $self ={}; bless $self, $class; return $self; } #Function: is_alive #Description: #Arguments: Computer Name #Return Value: Boolean true(1) or false(0) sub is_alive{ my $self = shift; my $cpu = shift; my $p = Net::Ping->new('icmp'); return $p->ping($cpu); } #Function: get_ipaddress #Arguments: Computer Name #Return Value: IP address (ie "192.168.0.2") sub get_ipaddr{ my $self = shift; my $cpu = shift; return inet_ntoa(inet_aton($cpu)) } #Function: get_clock_speed #Arguments: Computer Name #Return Value: processor speed(i.e. "450 MHz") sub get_clock_speed{ my $self = shift; my $cpu = shift; my $info= `psinfo \\\\$cpu`; my ($processor)= ($info =~ /Processor\s+speed: \s+? (\d+\.?\d+?\s+\w+) #processor info /sxg); return ($processor || "unknown"); } #Function: get_memory #Arguments: Computer Name #Return Value: amount of availible Memory(i.e. "128 MB") sub get_memory{ my $self = shift; my $cpu = shift; my $info= `psinfo \\\\$cpu`; my ($memory)= ($info =~ /Physical\s+memory: \s+ (\d+\s+\w+) #memory /sxg); return ($memory || "unknown"); } #Function: get_all_pcs #Arguments: none #Return Value: all computers visible on the domain sub get_all_pcs{ my $self = shift; return map{ m|\\+(\w+)| }`net view`; } #Function: get_logged_on #Arguments: Computer Name #Return Value: user currently logged on sub get_logged_on{ my $self = shift; my $cpu = shift; my $log = `psloggedon \\\\$cpu`; my($loggedon) = ($log =~ /locally:\n\s+.*?\b(\w+?)\n/)[0]; return $loggedon || "unknown"; } #Function: get_sp #Arguments: Computer Name #Return Value: Service Pack number sub get_sp { my $self = shift; my $cpu= shift; my $sp = $Registry->{"//$cpu/LMachine/SOFTWARE/" . "Microsoft/Windows NT/CurrentVersion/CSDVersion"}; if( ! $sp || $sp !~ /(\d+)/ ) { return "unknown"; } return $sp; } 1;

Replies are listed 'Best First'.
Re: Common Windows Network info
by VSarkiss (Monsignor) on Sep 16, 2002 at 18:00 UTC

    I'd recommend changing the interface. The code creates an instance object, but doesn't use it. I'd say don't bother trying to make it look OO when it really isn't.

    The module is providing a series of routines, which don't depend on each other. This is a great situation for using Exporter. Set up an @EXPORT_OK array and put all your routines in there, then let the caller pick.

Re: Common Windows Network info
by Mr. Muskrat (Canon) on Sep 16, 2002 at 16:39 UTC
    Too bad the ps tools require Windows NT/2k to work.
      I'd imagine that most businesses using Windows use an NT variant, though. Does it work on XP? I'd imagine that it would, as it shares common underpinnings.