Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Determining the last time a perl script ran

Posted on 2006-11-30
7
Medium Priority
?
248 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
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 
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 2000 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

Plug and play, no additional software required!

The ATEN UE3310 USB3.1 Gen1 Extender Cable allows users to extend the distance between the computer and USB devices up to 10 m (33 ft). The UE3310 is a high-quality, cost-effective solution for professional environments such as hospitals, factories and business facilities.

Question has a verified solution.

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

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 …
Resolve DNS query failed errors for Exchange
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

730 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