Anthony Okusanya wrote:
Does anyone know of perl module that can get a Microsoft servers Uptime.
There is a utility in the resourcekit called Uptime that does this but
I am looking for a module in perl with the same funcitionality.
-----------------------
Here are 3 different methods depending on your needs and preferences.
They are chopped out of larger scripts so may have unnecessary/archaic
code in them but should be a good start
Method #1 (Lanman)
use strict;
use Win32::Lanman;
my $server = shift || die "No server name entered\n";
my %info;
if(!Win32::Lanman::NetRemoteTOD($server, \%info))
{
print "$server: could not access clock; error: ";
# get the error code
print Win32::Lanman::GetLastError(), "\n";
exit 1;
}
my $upmsecs = $info{'msecs'};
$upmsecs is the uptime in milliseconds. This counter rolls around in 49
days
-----------------------------------------------
Method #2 (SNMP) Assumes the servers are running SNMP
use strict;
our ($hostname, $community, $port, $session, $error, $response);
use Net::SNMP;
#list your servers and their snmp readonly community name
my %servers = (
"myserver01.mydomain.com" => "public",
"myserver02.mydomain.com" => "readonly",
"myserver03.mydomain.com" => "guessme",
);
$port = 161; #SNMP uses port 161
my $hostgroup;
my $hashref;
my $sysUpTime = '1.3.6.1.2.1.1.3.0'; #Don't change this
foreach $hostname (sort(keys(%servers))) {
$community = $servers{$hostname};
($session, $error) = Net::SNMP->session(
-hostname => $hostname,
-community => $community,
-port => $port
);
print $hostname, "\t";
if (!defined($session)) {
printf("ERROR creating SNMP session: %s.\n", $error);
next;
}
if (!defined($response = $session->get_request($sysUpTime))) {
printf("ERROR - No response: %s.\n", $session->error);
$session->close;
next;
}
print $response->{$sysUpTime}, "\n";
$session->close;
}
}
-----------------------------------------------------
Method #3 WMI scripting. (Windows 2K and above.) Target Machine must
have DCOM enabled
Bootuptime is in the format YYYYMMDDHHMMSS I believe.
In my timezone (GMT + 2) there is a 2 hour offset in the wrong direction
eg I booted my PC at 09:30 (07:30 GMT) but the script says it booted at
11:30
If anyone can tell me why please do!
use strict;
use Win32::OLE qw( in );
my $Machine = "\\\\server1";
my $CLASS
=
"winmgmts:{impersonationLevel=impersonate,(security,systemprofile)}!$Machine\\Root\\cimv2";
my $WMI = Win32::OLE->GetObject( $CLASS ) || die "Could not create class
$CLASS\n";
my $wmiInfo;
print "\n-------- Operating System Info --------\n";
my $SysSettings = $WMI->ExecQuery('Select * from Win32_OperatingSystem') or
die "Could not retrieve sysSettings from $server ".Win32::OLE->LastError(
)."\n";
foreach $wmiInfo (in ($SysSettings)){
print join ("\t", "OS Name",$wmiInfo->Name ), "\n";
print join ("\t", "Version",$wmiInfo->Version ), "\n";
print join ("\t", "Service Pack",$wmiInfo->ServicePackMajorVersion .
"." . $wmiInfo->ServicePackMinorVersion ), "\n";
print join ("\t", "OS Manufacturer",$wmiInfo->Manufacturer ), "\n";
print join ("\t", "Windows Directory",$wmiInfo->WindowsDirectory ),
"\n";
print join ("\t", "Locale",$wmiInfo->Locale ), "\n";
print join ("\t", "LastBootupTime", int $wmiInfo->LastBootupTime ),
"\n";
}
Rgds,
Andy
[EMAIL PROTECTED]
_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs