Solved

Determining the last time a perl script ran

Posted on 2006-11-30
7
232 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
ID: 18044226

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
ID: 18044330
Good question mate.

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

Author Comment

by:Simon336697
ID: 18044381
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
Give your grad a cloud of their own!

With up to 8TB of storage, give your favorite graduate their own personal cloud to centralize all their photos, videos and music in one safe place. They can save, sync and share all their stuff, and automatic photo backup helps free up space on their smartphone and tablet.

 
LVL 70

Expert Comment

by:Chris Dent
ID: 18044442

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
ID: 18044588
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
ID: 18044643

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
ID: 18049548
Chris thank you so much.

I will let you know.

Simon
0

Featured Post

Save on storage to protect fatherhood memories

You're the dad who has everything. This Father's Day, make sure your family memories are protected. My Passport Ultra has automatic backup and password protection to keep your cherished photos and videos safe. With up to 3TB, you have plenty of room to hold the adventures ahead.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes you might need to configure routing based not only on destination IP address, but also on a combination of destination IP address (or hostname) and destination port number. I will describe a method how to accomplish this with free tools. …
We recently endured a series of broadcast storms that caused our ISP to shut us down for brief periods of time. After going through a multitude of tests, we determined that the issue was related to Intel NIC drivers on some new HP desktop computers …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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