home *** CD-ROM | disk | FTP | other *** search
- // LICDCOLL.SCP - Sample script to collect licence data form all available
- // Clients.
- // Copyright (c) 2003, Vector Networks Limited
- // All Rights Reserved
- //
- // Revision History:
- // 5.3 02-Aug-00 AB - Created.
- // 03-Aug-00 DB - Change to Lookup rather than use Known Clients.
- // 07-Aug-00 DB - Add pre-defined variables for Client and Group names.
- // 6.0 12-Oct-00 DB - Trim predefined variables before use.
- // - Change predefined ClientName for Lookup
- // from " " to "*".
- // 8.1 08-Oct-03 DB - Change PRODUCT.DAT size checking code for PC-Duo.
-
- // This Script is used to collect licence information from Clients. It can be
- // run several times. The data is collected into the LICDATA subdirectory
- // of the Control's install directory. From there, it can be analysed using the
- // LICDNLSR Script.
- //
- // The Clients can be selected, using pre-defined variables, between all
- // those responding to a browse (using the Lookup function), those in a
- // specific Group (using the GetClientsInGroup function) or all of Known
- // Clients (using the GetAllClients function).
- //
- // The Script attempts to collect licence data from all of the selected
- // Clients. It does this by connecting to the Clients in turn. Once
- // connected, it copies the Client's licence file (NSM.LIC) to a local
- // temporary file (LIC.TMP). From this file it finds out the serial number
- // and the maximum number of Clients permitted by this licence. If there
- // is not already a copy of the licence file in the data directory, LIC.TMP
- // is renamed to "serialno.LIC", and the Client's name is added to a
- // newly-created "serialno.LST" file. This file will eventually hold the
- // names of all of the Clients that are using that licence. If there already
- // is a copy of the licence file, the Script adds the new Client's name to
- // the licence list file (which is sorted alphabetically).
- //
- // All new licence files/Clients that are found are logged with the date and
- // time of discovery in LICCOLL.LOG (using the AddToLog function).
- //
- // To restart data collection, you can delete the entire LICDATA directory
- // and all of its contents. This Script will recreate the directory when
- // necessary.
-
- // Predefined variables:
- // ClientName = "" Template name for Lookup (use "*" for any)
- // GroupName = "" Selects all Clients from this Group
- // Username = "SCRIPTING/GBBLLYDGKK" Username and password for Connect
-
- $INCLUDE "ADDTOLOG.SCP"
- $INCLUDE "GTCTLDIR.SCP"
- $INCLUDE "PARSE.SCP"
- $INCLUDE "RINSTR.SCP"
- $INCLUDE "STRXCMP.SCP"
- $INCLUDE "TOKENS.SCP"
-
- Function Main ()
- Dim ClientName as String, GroupName as String, Username as String
- Dim Clients as List, SerialNo as String, MaxSlaves as String
- Dim ClFile as String, CClient as String, ClientCount, LicenceCount
- Dim LicDataDir as String, LTmpFile as String, LClsFile as String, LCpFile as String
- Dim ClFile2 as String, Prodcode as String, Entry as String, Entry2 as String
- Dim Comp as Integer, LstItms as Integer
- Dim Handle1, Handle2, Handle3, Handle4, Handle5, Handle6
- Dim TClsFile as String, AddEntry as Integer
- Dim NewLics as List, NewCls as List, LogFile as String
- Dim LcLstFl as String, LicEntry as String, x, z as Integer, TLLstfl as String
- Dim ChngLst as Integer, MSlaves as String
-
- SetTransport (T_TCPIP)
-
- // Initialise counter variables
-
- ClientCount = 0
- LicenceCount = 0
-
- // Trim predefined variables before use
-
- ClientName = Trim (ClientName)
- GroupName = Trim (GroupName)
- UserName = Trim (UserName)
-
- // Use GetControlDir to find the Control's install directory,
- // from this the file names for the log file (stored in LogFile),
- // the temporary copy of the licence file (stored in LicDataDir)
- // and the Licence list file (stored in LcLstFl) are found.
- //
- // We store all of our data in a subdirectory, to keep it
- // away from the Control's installed program files.
-
- LicDataDir = GetControlDir ()
- LicDataDir = LicDataDir + "\LICDATA"
-
- If !DirExists (LicDataDir) Then
- MkDir (LicDataDir)
- Endif
-
- LogFile = LicDataDir + "\LICCOLL.LOG"
- LcLstFl = LicDataDir + "\LICENCE.LST"
- TLLstfl = LicDataDir + "\LICENCE.TMP"
- LTmpFile = LicDataDir + "\LIC.TMP"
-
- // The NewLics and NewCls lists store all new licence files
- // and Clients found. They need to have one item in so that
- // an error does not occur when the Script checks the
- // number of items in the list,
-
- AddItem (NewLics, "New Licence files:")
- AddItem (NewCls, "New Clients:")
-
- // If GroupName is defined, get all the Clients in the Group
-
- If GroupName != "" Then
- Print "Selecting all Clients from Group: ", GroupName
- GetClientsInGroup (GroupName, Clients)
- Else
-
- // If ClientName is defined, see if there are any
- // Clients running which have matching names
- // Use ClientName = " " to get all Available Clients
- // Otherwise, get all of the Known Clients
-
- If ClientName != "" Then
- If ClientName != "*" Then
- Print "Browsing for Clients matching name: ", ClientName
- Else
- ClientName = ""
- Print "Browsing for Available Clients"
- Endif
- Lookup (ClientName, Clients)
- Else
- Print "Selecting all Known Clients"
- GetAllClients (Clients)
- Endif
- Endif
-
- // Make sure that at least one Client has been found.
-
- If Items (Clients) > 0 then
-
- // Execute the following Script on all Clients found.
-
- For each CClient in Clients
- Trim (CClient)
-
- Print "----------------------------------------------------"
- Print "Trying Client ", GetClientName (CClient), "..."
-
- // Attempt to connect to the Client.
-
- If Connect (CClient, UserName) then
- ClientCount = ClientCount + 1
-
- Print "Connected to Client ", GetClientName (CClient)
-
- // Use GetInstallDir to discover the location of the
- // required files.
-
- ClFile = ">" + GetInstallDir ("") + "\NSM.LIC"
- ClFile2 = (">" + GetInstallDir ("") + "\PRODUCT.DAT")
-
- // Copy the Client's NSM.LIC to the control system.
- // Then, go through the file checking for the
- // required lines, which begin with serial_no (the line
- // which contains the licence serial number) and
- // maxslaves (the line which contains the maximum
- // number of slaves). When one of these lines is found
- // the information is stored and the script continues
- // through the file until both lines have been found.
-
- If Copy (ClFile, LTmpFile) then
- Print "Copying Client's NSM.LIC to LIC.TMP"
- Handle1 = Open (LTmpFile , FILE_READ)
-
- Print "Reading LIC.TMP"
-
- // Initialise the serial number and maximum Clients
-
- MaxSlaves = ""
- SerialNo = ""
-
- do until EOF (Handle1)
- line = Readline(Handle1)
- if left (line, 9) = "serial_no" then
- SerialNo = line
- Print "Serial number found: ", SerialNo
- endif
-
- if left (line, 9) = "maxslaves" then
- MaxSlaves = line
- Print "Maximum number of Clients found: ", MaxSlaves
- endif
-
- if SerialNo != "" and MaxSlaves != "" then exit do
- loop
-
- // Close the file.
-
- Close (Handle1)
-
- // Trim the two lines down so that we are left with
- // only the important parts. Use the serial number
- // to work out the file name of the local copy of the
- // licence file (i.e. the permanent one).
-
- x = Len (SerialNo) - 10
- SerialNo = Right (SerialNo, x)
- LCpFile = LicDataDir
- LCpFile = LCpFile + "\" + SerialNo + ".LIC"
-
- // Strip off "serial_no="
-
- x = Len (MaxSlaves) - 10
- MaxSlaves = Right (MaxSlaves, x)
-
- // Check the existence and size of PRODUCT.DAT to
- // determine the three letter product code. If the file is
- // 213 bytes long, the Client is PC-Duo (NSP). If it is
- // 235 bytes long, the Client is NetSupport PC-Duo (NSP).
- // If the file is 182 bytes long, the Client is REMCON
- // PC-Duo (RCP). If the file does not exist then the
- // Client is NetSupport Manager (NSM).
-
- If FileExists (ClFile2) then
- Print "Checking PRODUCT.DAT"
- If GetFileInfo (ClFile2, FI_SIZE) = 213 then
- Prodcode = "PCD"
- Endif
- If GetFileInfo (ClFile2, FI_SIZE) = 235 then
- Prodcode = "NSP"
- Endif
- If GetFileInfo (ClFile2, FI_SIZE) = 182 then
- Prodcode = "RCP"
- Endif
- else
- Prodcode = "NSM"
- endif
-
- Print "Client product code is ", ProdCode
-
- // Put the serial number and Client name into a string
- // which will be added to the file containing all
- // Clients using the same licence.
-
- Entry = GetClientName (CClient) + "|" + ProdCode
- LicEntry = SerialNo + "|" + MaxSlaves
-
- // Use the serail number to work out the name of the
- // Client list for the current licence and the file name
- // of a temporary file which might be used used later.
-
- TClsfile = LicDataDir + "\" + SerialNo + ".TMP"
- LClsFile = LicDataDir + "\" + SerialNo + ".LST"
-
- // If there is already a copy of the licence file
- // in the data directory, open it and see if it is
- // an older version which should be replaced.
- // This is done by comparing the maxslaves
- // values. We keep the licence which has the
- // greater number of maxslaves.
-
- If FileExists (LCpFile) then
- Handle5 = Open (LcLstFl, FILE_READ)
- If Handle5 then
- y = 1
-
- Do until EOF (Handle5)
- Line = ReadLine (Handle5)
- z = InStr (Line, "|")
- MSlaves = GetToken (Line, "|", 2)
- If SerialNo = Left (Line, z - 1) then
- If CInt (MaxSlaves) > CInt (MSlaves) then
- ChngLst = TRUE
- exit do
- endif
- endif
- y = y + 1
- loop
-
- Close (Handle5)
- else
- Print "Could not read local copy of licence file!"
- endif
-
- If ChngLst = TRUE then
-
- // Create a temporary file to store the new list.
-
- Handle5 = Open (LcLstFl, FILE_READ)
- Handle6 = Open (TLLstFl, FILE_CREATE)
- x = 1
-
- Do until EOF (Handle5)
- Line = ReadLine (Handle5)
- If y = x then
- WriteLine Handle6, LicEntry
- else
- WriteLine Handle6, Line
- endif
- x = x + 1
- loop
-
- Close (Handle5)
- Close (Handle6)
-
- Delete (LcLstFl)
- Rename (TLLstFl, LcLstFl)
-
- Delete (LCpFile)
- Rename (LTmpFile, LCpFile)
-
- AddToLog (LogFile, "Licence " + SerialNo + " updated to " + MaxSlaves + " maximum Clients")
- endif
-
- Print "Client list file found"
- Handle2 = Open (LClsFile, FILE_READ)
-
- If Handle2 then
- x = 1
- Print "Searching list file"
-
- // Search through the file, comparing the
- // current line to the "entry" string.
-
- Do until EOF (Handle2)
- Entry2 = ReadLine (Handle2)
-
- // Use the StrCmp function in STRXCMP.SCP
-
- CName = GetToken (Entry, "|", 1)
- C2Name = GetToken (Entry2, "|", 1)
- Comp = StrCmp (CName, C2Name)
-
- If Comp = 1 then
-
- // If the Entry string is greater than the current
- // line, continue on through the file.
-
- Print "Continuing through file"
- else
- If Comp = 0 then
-
- // If the current line matches the entry,
- // Client has already been logged in the
- // list file, so the script can stop looking
- // through the file. AddEntry is false as
- // the Entry string does not need to be
- // added.
-
- Print "Matching entry found"
- Close (Handle2)
- AddEntry = FALSE
- exit do
- else
-
- // If the current line is greater then the
- // Entry string, this Client has not been
- // logged and must be entered at the
- // line before this one. AddEntry is made
- // true so that later on the Entry will be
- // added and x is made one less so that
- // the Entry is added one line before
- // the current one.
-
- AddEntry = TRUE
- Print "Higher entry found at line ", x
- x = x - 1
- exit do
- endif
- endif
- x = x + 1
-
- // If the end of the file has been reached and
- // no match or higher entry found, the Entry
- // string must be added onto the end of the
- // file. To do this, the file needs to be closed
- // (as it was only opened for read). The file
- // can then be opened for append and the
- // Entry stirng added to it. AddEntry is false
- // as the Entry has already been added.
-
- if EOF (Handle2) then
- Close (Handle2)
- Print "No higher entry found, adding to end of file"
- Handle4 = Open (LClsFile, FILE_APPEND)
- WriteLine Handle4, Entry
- Print "Added to end of file"
- AddItem (NewCls, "New Client: " + CClient + " added to " + SerialNo + ".LST")
- AddToLog (LogFile, "New Client: " + CClient + " added to " + SerialNo + ".LST")
- Close (Handle4)
- AddEntry = FALSE
- Exit do
- endif
- loop
- else
- print "Couldn't open Client list file: ", LClsFile
- endif
-
- If AddEntry = TRUE then
-
- // If AddEntry is true, the Entry needs to be
- // added at the correct point (x). A temporary
- // file needs to be opened so that the list
- // can be rewritten into that file to include
- // the new Client. The old file can then be
- // deleted and the new file renamed.
-
- Close (Handle2)
- Handle4 = Open (LClsFile, FILE_READ)
- Print "Adding ", entry, " to list file at line: ", x + 1
- If FileExists (TClsFile) then Delete (TClsFile)
- Print "Creating temporary file: ", TClsFile
- Handle3 = Open (TClsFile, FILE_CREATE)
- y = 1
-
- // Go through the file, copying each line into
- // the temporary file. When line x is reached,
- // write the line and then the Entry. But, if
- // x is 0, write the Entry before copying the file.
-
- Do until EOF (Handle4)
- If x = 0 and y = 1 then
- Print "Added ", Entry, " at line 1"
- WriteLine Handle3, Entry
- y = y + 1
- endif
- Entry2 = Readline (Handle4)
- Print "Writing line ", y, ": ", Entry2
- WriteLine Handle3, Entry2
- if y = x then
- WriteLine Handle3, Entry
- Print "Added ", entry, " at line ", y + 1
- y = y + 1
- endif
- y = y + 1
- loop
- Close (Handle3)
- Close (Handle4)
- Delete (LClsFile)
- Rename (TClsFile, LClsFile)
- AddItem (NewCls, "New Client: " + CClient + " added to " + SerialNo + ".LST")
- AddToLog (LogFile, "New Client: " + CClient + " added to " + SerialNo + ".LST")
- endif
- else
-
- // If a copy of the licence file is not found in
- // the controls install directory make the copy
- // with the file name serialno.LIC and create
- // a list file for the licence serialno.LST
- // Add the Client details to the list file.
-
- LicenceCount = LicenceCount + 1
- Print "Copying the licence file to ", LCpFile
- Copy (LTmpFile, LCpFile)
- AddItem (NewLics, "New licence: " + SerialNo + " copied to " + LCpfile + " from Client " + GetClientName(CClient)))
- AddToLog (LogFile, "New licence: " + SerialNo + " copied to " + LCpfile)
-
- // Now add an entry to the LICENCE.LST file with
- // the serial number and maxslaves values for the
- // current licence.
-
- If FileExists (LcLstFl) then
- Handle5 = Open (LcLstFl, FILE_APPEND)
- else
- Print "Creating ", LcLstFl
- Handle5 = Open (LcLstFl, FILE_CREATE)
- endif
-
- If Handle5 Then
- WriteLine Handle5, LicEntry
- Close (Handle5)
- Endif
-
- Handle1 = Open (LClsFile, FILE_CREATE)
-
- If Handle1 then
- WriteLine Handle1, Entry
- Close (Handle1)
- AddItem (NewCls, "New Client: " + CClient + " added to " + SerialNo + ".LST")
- AddToLog (LogFile, "New Client: " + CClient + " added to " + SerialNo + ".LST")
- else
- Print "Unable to create Client list file: ", LClsFile
- endif
- endif
- else
-
- // Check to see why NSM.LIC could not be read.
-
- If LASTERROR = 10 then
- Print "File Transfer on ", CClient, " has been disabled."
- AddToLog (LogFile, "File Transfer on " + CClient + " has been disabled.")
- else
- Print "Error (", LASTERROR, ") reading NSM.LIC on ", CClient
- AddToLog (LogFile, "Error reading NSM.LIC on " + CClient)
- endif
- endif
-
- // Disconnect from the Client and continue on to the next.
-
- Disconnect (CClient)
- else
- Print "Could not connect to Client ", GetClientName (CCLient)
- endif
- Next
-
- // Print a summary
-
- Print ""
- Print "Number of Clients found: ", ClientCount
- Print "New Licences found: ", LicenceCount
- Print "------------"
- For y = 1 to Items (NewLics,1)
- Print GetItem (NewLics, y)
- Next
- Print "------------"
- For y = 1 to Items (NewCls,1)
- Print GetItem (NewCls, y)
- Next
- else
- print "No Clients found!"
- endif
- End Function
-