Solved

Determining the last time a perl script ran

Posted on 2006-11-30
7
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 71

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
Simple, centralized multimedia control

Watch and learn to see how ATEN provided an easy and effective way for three jointly-owned pubs to control the 60 televisions located across their three venues utilizing the ATEN Control System, Modular Matrix Switch and HDBaseT extenders.

 
LVL 71

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 71

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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 …
An article on effective troubleshooting
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month11 days, 3 hours left to enroll

632 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