Solved

Determining the last time a perl script ran

Posted on 2006-11-30
7
231 Views
Last Modified: 2013-12-23
Hi guys!

We have an NT domain where we have a perl script that should run every hour.
This changes the daily password on the machines it is run on.

What Id like to find out is retrieve a list of all machines where this script has not run in the last hour or two.
Let's say the perl script is called "run.pl".

Any help greatly appreciated.

Simon
0
Comment
Question by:Simon336697
  • 4
  • 3
7 Comments
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

Does the Perl script write to a log file at all?

It would be easy enough to pick up the creation or modified date of that log file. I'm not sure how else you would see if a Perl Script ran or not.

Chris
0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Good question mate.

I believe it does write to a log file.
0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Chris!

The perl script runs every hour on each NT box
It outputs to a file called everyhour.log.
We can manually open this file and check the last 20 lines for:

Password change success
or
Password change failed


To give you the full picture Chris (which explains totally why Im trying to call on a guru like you) of what we have to do:

Manually we have to do the following if the password is not correct on the NT box.

1) Change the hjbox user account to the correct password for the DAY
2) To ensure that the the perl script updates the password on the hour and for every day, it looks at a file called setpass.log which has the current date/time. If this is wrong, then the perl script will fail.

The format of the file is:

Production: 0:hjbox:27:6:20:05

where 27 is the day of the month
6 is the month number
20 is the century
05 is the year

If the date in the log file is not shown as today's date, we need to manually set this date in the log file.

Simon
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

To ensure I understand the process you want correctly this is what I'll do:

1. Read list of computers from Text File
2. For Each computer in the list look for an "everyhour.log" file and check the Modified Date.
3. If the Modified Date is more than an hour off then flag the password to be reset
4. Read the SetPass file (is this just one line?) and correct the entry if it's wrong.

Throughout the script all actions will be logged to a seperate file on the server running the VbScript.

At the moment I'm still staying away from exporting the computer list from WinNT or AD, however, that's possible (and quite easy) if you want it to happen that way.

Is all that acceptable?

Chris
0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Mate you are all spot on with your assumptions.
For step 4. where you say is it just one line, yes it is.

I cant thank you enough for doing what you are doing.

Simon
0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
Comment Utility

Okay, lets see how many mistakes I've managed to inject into this while writing it. There are a few comments in there that could do with your attention. They revolve around what to do if the SetPassword file is missing or incorrectly formatted.

Fortunately it should be easy enough for you to test it against a single machine so please let me know how you get on (and what errors I'm written in).



Option Explicit

' Script Constants

Const NEW_PASSWORD = "abcd1234"
Const COMPUTER_FILE = "computers.txt"
Const PERL_LOG_FILE = "\c$\Scripts\everyhour.log"
Const SET_PASS_FILE = "\c$\Scripts\setpass.log"

' Subroutines

Sub ReadComputerList
      Dim objFile, objStream
      Dim strComputer

      Set objFile = objFileSystem.GetFile(COMPUTER_FILE)
      Set objStream = objFile.OpenAsTextStream(1, 0)
      
      Do While Not objStream.AtEndOfStream
            strComputer = objStream.ReadLine
            If strComputer <> "" Then
                  If Not objComputers.Exists(strComputer) Then
                        objComputers.Add strComputer, ""
                  End If
            End If
      Loop
      
      Set objStream = Nothing
      Set objFile = Nothing
End Sub

Sub CheckLogFile
      Dim objFile
      Dim strComputer
      Dim dtmLastModified

      For Each strComputer in objComputers
            On Error Resume Next
            If objFileSystem.FileExists("\\" & strComputer & PERL_LOG_FILE) Then
                  Set objFile = objFileSystem.GetFile("\\" & strComputer & PERL_LOG_FILE)
                  dtmLastModified = CDate(objFile.DateLastModified)
                  objComputers(strComputer) = dtmLastModified
            Else
                  objLogFile.WriteLine strComputer & ": Failed To Get File " & PERL_LOG_FILE
                  objComputers.Remove strComputer
            End If
            On Error Goto 0
      Next
End Sub

Sub CheckPasswordChange
      Dim strComputer
      Dim dtmLastModified
      Dim intMinutesSinceChange

      For Each strComputer in objComputers
            dtmLastModified = objComputers(strComputer)
            intMinutesSinceChange = DateDiff("n", dtmLastModified, Now)

            If intMinutesSinceChange <= 60 Then
                  objLogFile.WriteLine strComputer & ": Last Changed " & dtmLastModified & ": No Change Required"
                  objComputers.Remove strComputer
            End If
      Next
End Sub

Sub CheckSetPasswordFile
      Dim objFile, objStream
      Dim strComputer, strLine, strDay, strMonth, strYear
      Dim arrLine

      For Each strComputer in objComputers
            If objFileSystem.FileExists("\\" & strComputer & SET_PASS_FILE) Then
                  Set objFile = objFileSystem.GetFile("\\" & strComputer & SET_PASS_FILE
                  Set objStream = objFile.OpenAsTextStream(1, 0)

                  strLine = objStream.ReadLine
                  arrLine = Split(strLine, ":")
                  
                  Set objStream = Nothing
            
                  If UBound(arrLine) < 6 Then
                        objLogFile.WriteLine strComputer & ": Error In File " & SET_PASS_FILE
                        
                        ' Not changing it here without knowing all the fields required.
                        
                        objComputers.Remove strComputer
                  Else
                        strDay = DatePart("d", Date)
                        strMonth = DatePart("m", Date)
                        strYear = DatePart("yyyy", Date)
                        If strDay <> arrLine(3) Or strMonth <> arrLine(4) Or Left(strYear, 2) <> arrLine(5) _
                                    Or Right(strYear, 2) <> arrLine(6) Then
                                    
                              arrLine(3) = strDay
                              arrLine(4) = strMonth
                              arrLine(5) = Left(strYear, 2)
                              arrLine(6) = Right(strYear, 2)
                              strLine = Join(arrLine, ":")
                              
                              objFile.Delete
                              Set objFile = Nothing
                              
                              Set objFile = objFileSystem.OpenTextFile("\\" & strComputer & SET_PASS_FILE, 2, True, 0)
                              objFile.WriteLine strLine
                              
                              objLogFile.WriteLine strComputer & ": Replaced Line in " & SET_PASS_FILE &_
                                          ": New Line: " & strLine
                        End If
                  End If
                  
                  Set objFile = Nothing
            Else
                  objLogFile.WriteLine strComputer & ": Failed To Get File " & SET_PASS_FILE
                  
                  ' What would you like to do if the file is missing? Discarding at the moment (no password change)
                  
                  objComputers.Remove strComputer
            End If
      Next
End Sub

Sub ResetPasswords
      Dim objUser
      Dim strComputer

      For Each strComputer in objComputers
            On Error Resume Next : Err.Clear

            Set objUser = GetObject("WinNT://" & strComputer & "/hjbox, user")
            If Err.Number <> 0 Then
                  objLogFile.WriteLine strComputer & ": Failed to attach to User: " & Err.Description
            Else
                  Err.Clear
                  objUser.SetPassword NEW_PASSWORD
                  If Err.Number <> 0 Then
                        objLogFile.WriteLine strComputer & ": Failed to set Password: " & Err.Description
                  Else
                        objLogFile.WriteLine strComputer & ": Password Reset Successfully"
                  End If
            End If
            Set objUser = Nothing

            On Error Goto 0
      Next
End Sub

'
' Main Code
'

' Global Variables

Dim objFileSystem, objComputers, objLogFile

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objComputers = CreateObject("Scripting.Dictionary")

Set objLogFile = objFileSystem.OpenTextFile("CheckPassword.log", 2, True, 0)

ReadComputerList
CheckLogFile
CheckPasswordChange
CheckSetPasswordFile

If objComputers.Count > 0 Then
      ResetPasswords
End If

Set objLogFile = Nothing
Set objComputers = Nothing
Set objFileSystem = Nothing
0
 
LVL 1

Author Comment

by:Simon336697
Comment Utility
Chris thank you so much.

I will let you know.

Simon
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

A brief overview to explain gateways, default gateways and static routes OR NO - you CANNOT have two default gateways on the same server, PC or other Windows-based network device. In simple terms a gateway is formed when a computer such as a serv…
The Need In an Active Directory enviroment, the PDC emulator provide time synchronization for the domain. This is important since Active Directory uses Kerberos for authentication.  By default, if the time difference between systems is off by more …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

11 Experts available now in Live!

Get 1:1 Help Now