Solved

Using a perl script to get ntp time server info from registry on server 2003

Posted on 2008-10-02
7
823 Views
Last Modified: 2008-11-20
I have a perl script that I want to use to check and see if a windows 2003 server is correctly configured to get time from an ntp server.  It returns an error saying that the ntp server is not configured  Below is a portion of my code that deals with the time server check.  the rest of my script works fine  just the time server check fails.  Any help would be appreciated as I need to run this on approx 40 2003 servers each month.


use Win32::Registry;
    my $tserv;  
$::HKEY_LOCAL_MACHINE->Open("SOFTWARE\\Policies\\Microsoft\\W32Time\\Parameters", $tserv)
        or $tserv_error = 1;

if ($systype =~ backup) {
print "This script is being run on a backup controller, it will not show the correct timeserver IP";
}

if ($tserv_error = 1) {
      print "Error - It looks like the time server has not been configured on this system in gpedit.msc\n\n";  
      $ntpsource = "0.0.0.0";
      $ntpprotocol = "ERROR";
      }
      else
      {
    my ($type, $value);  
$tserv->QueryValueEx("NtpServer", $type, $value);
$tserv->QueryValueEx("Type", $type, $ntpprotocol);
($ntpsource, $junk) = split(',',$value);
print "TIMESERVER: $ntpsource\n";  
print "TIMESERVERTYPE: $ntpprotocol\n";  
      }
0
Comment
Question by:tnnetworks
  • 3
  • 3
7 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 22625187
Of the code you posted, which line generates the error?
0
 

Author Comment

by:tnnetworks
ID: 22625814
it dies not generate any errors it just never gets the time servers IP or type.  The output is sent to a txt file  The output of the txt file is here:  the 61341 is the server name  but I have NTP configured correctly and event view shows successful time syncs.

61341 - TIMESERVER: 0.0.0.0
61341 - TIMESERVERTYPE: ERROR
0
 
LVL 39

Expert Comment

by:Adam314
ID: 22625855
If you open up regedit, and look at the KEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\W32Time\Parameters\Type, what does it show?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Expert Comment

by:MadShiva
ID: 22626078
Hi !

I have try your script, and i get the same result.

When i have look at the key, there is not folder W32Time. I have check again on a windows server 2003 and again the folder was not here. But in another workstation i have find where is located the folder.

Try to change

$::HKEY_LOCAL_MACHINE->Open("SOFTWARE\\Policies\\Microsoft\\W32Time\\Parameters", $tserv)

by

$::HKEY_LOCAL_MACHINE->Open("SYSTEM\\CurrentControlSet\\Services\\W32Time\\Parameters", $tserv)


Best Regards
0
 

Author Comment

by:tnnetworks
ID: 22629613
Under the registry settings when I follow the complete path  KEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\W32Time\Parameters\Type   type show as NTP

There are 3 registry keys under KEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\W32Time\Parameters\

They are
Default  No Value Set
NtpServer REG_SZ 10.1.36.94,0x1
Type   REG_SZ NTP
0
 

Author Comment

by:tnnetworks
ID: 22629617
Hi Mad  I tried what you are suggesting however I get the same results.  The script comes back and tells me that it looks like the time server is not configured.
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 22707062
The Win32::Registry module is obsolete.  You should be using the Win32::TieRegistry module instead.  Try this code... I've tested it, and it works on my machine.
use Win32::TieRegistry (Delimiter => '/');
 

my $W32Time = $Registry->{'HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/W32Time/Parameters'}

  or die "Error - It looks like the time server has not been configured on this system in gpedit.msc\n\n";
 

my $value = (split(/,/, $W32Time->{"/NtpServer"}))[0];

my $type = $W32Time->{"/Type"};
 

print "Value=$value\n";

print "Type=$type\n";

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now