home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Demo / PCDUO / data1.cab / Script_Samples / LICDCOLL.SCP < prev    next >
Encoding:
Text File  |  2003-11-28  |  23.3 KB  |  522 lines

  1. // LICDCOLL.SCP - Sample script to collect licence data form all available
  2. // Clients.
  3. // Copyright (c) 2003, Vector Networks Limited
  4. // All Rights Reserved
  5. //
  6. // Revision History:
  7. // 5.3 02-Aug-00 AB - Created.
  8. //     03-Aug-00 DB  - Change to Lookup rather than use Known Clients.
  9. //     07-Aug-00 DB  - Add pre-defined variables for Client and Group names.
  10. // 6.0 12-Oct-00 DB  - Trim predefined variables before use.
  11. //                   - Change predefined ClientName for Lookup
  12. //                     from " " to "*".
  13. // 8.1 08-Oct-03 DB  - Change PRODUCT.DAT size checking code for PC-Duo.
  14.  
  15. // This Script is used to collect licence information from Clients. It can be
  16. // run several times. The data is collected into the LICDATA subdirectory 
  17. // of the Control's install directory. From there, it can be analysed using the 
  18. // LICDNLSR Script.
  19. //
  20. // The Clients can be selected, using pre-defined variables, between all 
  21. // those responding to a browse (using the Lookup function), those in a 
  22. // specific Group (using the GetClientsInGroup function) or all of Known 
  23. // Clients (using the GetAllClients function).
  24. //
  25. // The Script attempts to collect licence data from all of the selected 
  26. // Clients. It does this by connecting to the Clients in turn. Once 
  27. // connected, it copies the Client's licence file (NSM.LIC) to a local
  28. // temporary file (LIC.TMP). From this file it finds out the serial number 
  29. // and the maximum number of Clients permitted by this licence. If there 
  30. // is not already a copy of the licence file in the data directory, LIC.TMP
  31. // is renamed to "serialno.LIC", and the Client's name is added to a 
  32. // newly-created "serialno.LST" file. This file will eventually hold the 
  33. // names of all of the Clients that are using that licence. If there already 
  34. // is a copy of the licence file, the Script adds the new Client's name to 
  35. // the licence list file (which is sorted alphabetically).
  36. //
  37. // All new licence files/Clients that are found are logged with the date and 
  38. // time of discovery in LICCOLL.LOG (using the AddToLog function).
  39. //
  40. // To restart data collection, you can delete the entire LICDATA directory
  41. // and all of its contents. This Script will recreate the directory when
  42. // necessary.
  43.  
  44. //  Predefined variables:
  45. //    ClientName = "" Template name for Lookup (use "*" for any)
  46. //    GroupName = "" Selects all Clients from this Group
  47. //    Username = "SCRIPTING/GBBLLYDGKK" Username and password for Connect
  48.  
  49. $INCLUDE "ADDTOLOG.SCP"
  50. $INCLUDE "GTCTLDIR.SCP"
  51. $INCLUDE "PARSE.SCP"
  52. $INCLUDE "RINSTR.SCP"
  53. $INCLUDE "STRXCMP.SCP"
  54. $INCLUDE "TOKENS.SCP"
  55.  
  56. Function Main ()
  57.     Dim ClientName as String, GroupName as String, Username as String
  58.     Dim Clients as List, SerialNo as String, MaxSlaves as String
  59.     Dim ClFile as String, CClient as String, ClientCount, LicenceCount
  60.     Dim LicDataDir as String, LTmpFile as String, LClsFile as String, LCpFile as String
  61.     Dim ClFile2 as String, Prodcode as String, Entry as String, Entry2 as String
  62.     Dim Comp as Integer, LstItms as Integer
  63.     Dim Handle1, Handle2, Handle3, Handle4, Handle5, Handle6
  64.     Dim TClsFile as String, AddEntry as Integer
  65.     Dim NewLics as List, NewCls as List, LogFile as String
  66.     Dim LcLstFl as String, LicEntry as String, x, z as Integer, TLLstfl as String
  67.     Dim ChngLst as Integer, MSlaves as String
  68.  
  69.     SetTransport (T_TCPIP)
  70.  
  71.     // Initialise counter variables
  72.  
  73.     ClientCount = 0
  74.     LicenceCount = 0
  75.  
  76.     // Trim predefined variables before use
  77.  
  78.     ClientName = Trim (ClientName)
  79.     GroupName = Trim (GroupName)
  80.     UserName = Trim (UserName)
  81.  
  82.     // Use GetControlDir to find the Control's install directory,
  83.     // from this the file names for the log file (stored in LogFile),
  84.     // the temporary copy of the licence file (stored in LicDataDir)
  85.     // and the Licence list file (stored in LcLstFl) are found.
  86.     //
  87.     //  We store all of our data in a subdirectory, to keep it
  88.     //  away from the Control's installed program files.
  89.  
  90.     LicDataDir = GetControlDir () 
  91.     LicDataDir = LicDataDir + "\LICDATA"
  92.  
  93.     If !DirExists (LicDataDir) Then
  94.         MkDir (LicDataDir)
  95.     Endif
  96.  
  97.     LogFile = LicDataDir + "\LICCOLL.LOG"
  98.     LcLstFl = LicDataDir + "\LICENCE.LST"
  99.     TLLstfl = LicDataDir + "\LICENCE.TMP"
  100.     LTmpFile = LicDataDir + "\LIC.TMP"
  101.  
  102.     // The NewLics and NewCls lists store all new licence files
  103.     // and Clients found. They need to have one item in so that
  104.     // an error does not occur when the Script checks the 
  105.     // number of items in the list, 
  106.  
  107.     AddItem (NewLics, "New Licence files:")
  108.     AddItem (NewCls, "New Clients:")
  109.  
  110.     // If GroupName is defined, get all the Clients in the Group
  111.  
  112.     If GroupName != "" Then
  113.         Print "Selecting all Clients from Group: ", GroupName
  114.         GetClientsInGroup (GroupName, Clients)
  115.     Else
  116.  
  117.         // If ClientName is defined, see if there are any 
  118.         // Clients running which have matching names
  119.         // Use ClientName = " " to get all Available Clients
  120.         // Otherwise, get all of the Known Clients
  121.  
  122.         If ClientName != "" Then
  123.             If ClientName != "*" Then
  124.                 Print "Browsing for Clients matching name: ", ClientName
  125.             Else
  126.                 ClientName = ""
  127.                 Print "Browsing for Available Clients"
  128.             Endif
  129.             Lookup (ClientName, Clients)
  130.         Else
  131.             Print "Selecting all Known Clients"
  132.             GetAllClients (Clients)
  133.         Endif
  134.     Endif
  135.  
  136.     // Make sure that at least one Client has been found.
  137.  
  138.     If Items (Clients) > 0 then
  139.  
  140.         // Execute the following Script on all Clients found.
  141.  
  142.         For each CClient in Clients
  143.             Trim (CClient)
  144.  
  145.             Print "----------------------------------------------------"
  146.             Print "Trying Client ", GetClientName (CClient), "..."
  147.  
  148.             // Attempt to connect to the Client.
  149.  
  150.             If Connect (CClient, UserName) then
  151.                 ClientCount = ClientCount + 1
  152.  
  153.                 Print "Connected to Client ", GetClientName (CClient)
  154.  
  155.                 // Use GetInstallDir to discover the location of the
  156.                 // required files.
  157.  
  158.                 ClFile = ">" + GetInstallDir ("") + "\NSM.LIC"
  159.                 ClFile2 = (">" + GetInstallDir ("") + "\PRODUCT.DAT")
  160.  
  161.                 // Copy the Client's NSM.LIC to the control system.
  162.                 // Then, go through the file checking for the
  163.                 // required lines, which begin with serial_no (the line
  164.                 // which contains the licence serial number) and
  165.                 // maxslaves (the line which contains the maximum
  166.                 // number of slaves).  When one of these lines is found
  167.                 // the information is stored and the script continues
  168.                 // through the file until both lines have been found.
  169.  
  170.                 If Copy (ClFile, LTmpFile) then
  171.                     Print "Copying Client's NSM.LIC to LIC.TMP"
  172.                     Handle1 = Open (LTmpFile , FILE_READ)
  173.  
  174.                     Print "Reading LIC.TMP"
  175.  
  176.                     //  Initialise the serial number and maximum Clients
  177.  
  178.                     MaxSlaves = ""
  179.                     SerialNo = ""
  180.  
  181.                     do until EOF (Handle1)
  182.                         line = Readline(Handle1)
  183.                         if left (line, 9) = "serial_no" then 
  184.                             SerialNo = line
  185.                             Print "Serial number found: ", SerialNo
  186.                         endif
  187.  
  188.                         if left (line, 9) = "maxslaves" then 
  189.                             MaxSlaves = line
  190.                             Print "Maximum number of Clients found: ", MaxSlaves
  191.                         endif
  192.  
  193.                         if SerialNo != "" and MaxSlaves != "" then exit do
  194.                     loop
  195.  
  196.                     // Close the file.
  197.  
  198.                     Close (Handle1)
  199.  
  200.                     // Trim the two lines down so that we are left with
  201.                     // only the important parts. Use the serial number
  202.                     // to work out the file name of the local copy of the
  203.                     // licence file (i.e. the permanent one).
  204.  
  205.                     x = Len (SerialNo) - 10
  206.                     SerialNo = Right (SerialNo, x)
  207.                     LCpFile = LicDataDir 
  208.                     LCpFile = LCpFile + "\" + SerialNo + ".LIC"
  209.  
  210.                     //  Strip off "serial_no="
  211.  
  212.                     x = Len (MaxSlaves) - 10
  213.                     MaxSlaves = Right (MaxSlaves, x)
  214.  
  215.                     // Check the existence and size of PRODUCT.DAT to
  216.                     // determine the three letter product code. If the file is 
  217.                     // 213 bytes long, the Client is PC-Duo (NSP). If it is 
  218.                     // 235 bytes long, the Client is NetSupport PC-Duo (NSP).
  219.                     // If the file is 182 bytes long, the Client is REMCON
  220.                     // PC-Duo (RCP). If the file does not exist then the 
  221.                     // Client is NetSupport Manager (NSM).
  222.  
  223.                     If FileExists (ClFile2) then
  224.                         Print "Checking PRODUCT.DAT"
  225.                         If GetFileInfo (ClFile2, FI_SIZE) = 213 then 
  226.                             Prodcode = "PCD" 
  227.                         Endif
  228.                         If GetFileInfo (ClFile2, FI_SIZE) = 235 then 
  229.                             Prodcode = "NSP" 
  230.                         Endif
  231.                         If GetFileInfo (ClFile2, FI_SIZE) = 182 then 
  232.                             Prodcode = "RCP"
  233.                         Endif
  234.                     else 
  235.                         Prodcode = "NSM"
  236.                     endif
  237.  
  238.                     Print "Client product code is ", ProdCode
  239.  
  240.                     // Put the serial number and Client name into a string
  241.                     // which will be added to the file containing all
  242.                     // Clients using the same licence.
  243.  
  244.                     Entry = GetClientName (CClient) + "|" + ProdCode
  245.                     LicEntry = SerialNo + "|" + MaxSlaves
  246.  
  247.                     // Use the serail number to work out the name of the
  248.                     // Client list for the current licence and the file name
  249.                     // of a temporary file which might be used used later.
  250.  
  251.                     TClsfile = LicDataDir + "\" + SerialNo + ".TMP"
  252.                     LClsFile = LicDataDir + "\" + SerialNo + ".LST"
  253.  
  254.                     // If there is already a copy of the licence file
  255.                     // in the data directory, open it and see if it is 
  256.                     // an older version which should be replaced. 
  257.                     // This is done by comparing the maxslaves 
  258.                     // values. We keep the licence which has the
  259.                     // greater number of maxslaves.
  260.  
  261.                     If FileExists (LCpFile) then
  262.                         Handle5 = Open (LcLstFl, FILE_READ)
  263.                         If Handle5 then
  264.                             y = 1
  265.  
  266.                             Do until EOF (Handle5)
  267.                                 Line = ReadLine (Handle5)
  268.                                 z  = InStr (Line, "|")
  269.                                 MSlaves = GetToken (Line, "|", 2)
  270.                                 If SerialNo = Left (Line, z - 1) then
  271.                                     If CInt (MaxSlaves) > CInt (MSlaves) then
  272.                                         ChngLst = TRUE
  273.                                         exit do
  274.                                     endif
  275.                                 endif
  276.                                 y = y + 1
  277.                             loop
  278.  
  279.                             Close (Handle5)
  280.                         else
  281.                             Print "Could not read local copy of licence file!"
  282.                         endif
  283.                         
  284.                         If ChngLst = TRUE then
  285.  
  286.                             // Create a temporary file to store the new list.
  287.  
  288.                             Handle5 = Open (LcLstFl, FILE_READ)
  289.                             Handle6 = Open (TLLstFl, FILE_CREATE)
  290.                             x = 1
  291.  
  292.                             Do until EOF (Handle5)
  293.                                 Line = ReadLine (Handle5)
  294.                                 If y = x then
  295.                                     WriteLine Handle6, LicEntry
  296.                                 else
  297.                                     WriteLine Handle6, Line
  298.                                 endif
  299.                                 x = x + 1
  300.                             loop
  301.  
  302.                             Close (Handle5)
  303.                             Close (Handle6)
  304.  
  305.                             Delete (LcLstFl)
  306.                             Rename (TLLstFl, LcLstFl)
  307.  
  308.                             Delete (LCpFile)
  309.                             Rename (LTmpFile, LCpFile)
  310.  
  311.                             AddToLog (LogFile, "Licence " + SerialNo + " updated to " + MaxSlaves + " maximum Clients")
  312.                         endif
  313.  
  314.                         Print "Client list file found"
  315.                         Handle2 = Open (LClsFile, FILE_READ)
  316.                         
  317.                         If Handle2 then
  318.                             x = 1
  319.                             Print "Searching list file"
  320.  
  321.                             // Search through the file, comparing the 
  322.                             // current line to the "entry" string.
  323.  
  324.                             Do until EOF (Handle2)
  325.                                 Entry2 = ReadLine (Handle2)
  326.  
  327.                                 // Use the StrCmp function in STRXCMP.SCP
  328.  
  329.                                 CName = GetToken (Entry, "|", 1)
  330.                                 C2Name = GetToken (Entry2, "|", 1)
  331.                                 Comp = StrCmp (CName, C2Name)
  332.  
  333.                                 If Comp = 1 then
  334.  
  335.                                     // If the Entry string is greater than the current
  336.                                     // line, continue on through the file.
  337.  
  338.                                     Print "Continuing through file"
  339.                                 else
  340.                                     If Comp = 0 then 
  341.  
  342.                                         // If the current line matches the entry,
  343.                                         // Client has already been logged in the
  344.                                         // list file, so the script can stop looking
  345.                                         // through the file.  AddEntry is false as
  346.                                         // the Entry string does not need to be
  347.                                         // added.
  348.  
  349.                                         Print "Matching entry found"
  350.                                         Close (Handle2)
  351.                                         AddEntry = FALSE
  352.                                         exit do
  353.                                     else
  354.  
  355.                                         // If the current line is greater then the
  356.                                         // Entry string, this Client has not been
  357.                                         // logged and must be entered at the
  358.                                         // line before this one. AddEntry is made
  359.                                         // true so that later on the Entry will be
  360.                                         // added and x is made one less so that
  361.                                         // the Entry is added one line before
  362.                                         // the current one.
  363.    
  364.                                         AddEntry = TRUE
  365.                                         Print "Higher entry found at line ", x
  366.                                         x = x - 1
  367.                                         exit do
  368.                                     endif
  369.                                 endif
  370.                                 x = x + 1
  371.  
  372.                                 // If the end of the file has been reached and
  373.                                 // no match or higher entry found, the Entry
  374.                                 // string must be added onto the end of the
  375.                                 // file. To do this, the file needs to be closed
  376.                                 // (as it was only opened for read). The file
  377.                                 // can then be opened for append and the
  378.                                 // Entry stirng added to it. AddEntry is false
  379.                                 // as the Entry has already been added.
  380.  
  381.                                 if EOF (Handle2) then 
  382.                                     Close (Handle2)
  383.                                     Print "No higher entry found, adding to end of file"
  384.                                     Handle4 = Open (LClsFile, FILE_APPEND)
  385.                                     WriteLine Handle4, Entry
  386.                                     Print "Added to end of file"
  387.                                     AddItem (NewCls, "New Client: " + CClient + " added to " + SerialNo + ".LST")
  388.                                     AddToLog (LogFile, "New Client: " + CClient + " added to " + SerialNo + ".LST")
  389.                                     Close (Handle4)
  390.                                     AddEntry = FALSE
  391.                                     Exit do
  392.                                 endif
  393.                             loop
  394.                         else 
  395.                             print "Couldn't open Client list file: ", LClsFile
  396.                         endif
  397.  
  398.                         If AddEntry = TRUE then
  399.  
  400.                             // If AddEntry is true, the Entry needs to be
  401.                             // added at the correct point (x).  A temporary
  402.                             // file needs to be opened so that the list
  403.                             // can be rewritten into that file to include
  404.                             //  the new Client.  The old file can then be
  405.                             // deleted and the new file renamed.
  406.  
  407.                             Close (Handle2)
  408.                             Handle4 = Open (LClsFile, FILE_READ)
  409.                             Print "Adding ", entry, " to list file at line: ", x + 1
  410.                             If FileExists (TClsFile) then Delete (TClsFile)
  411.                             Print "Creating temporary file: ", TClsFile
  412.                             Handle3 = Open (TClsFile, FILE_CREATE)
  413.                             y = 1
  414.  
  415.                             // Go through the file, copying each line into
  416.                             // the temporary file. When line x is reached,
  417.                             // write the line and then the Entry. But, if
  418.                             // x is 0, write the Entry before copying the file.
  419.  
  420.                             Do until EOF (Handle4)
  421.                                 If x = 0 and y = 1 then
  422.                                     Print "Added ", Entry, " at line 1"
  423.                                     WriteLine Handle3, Entry
  424.                                     y = y + 1
  425.                                 endif
  426.                                 Entry2 = Readline (Handle4)
  427.                                 Print "Writing line ", y, ": ", Entry2
  428.                                 WriteLine Handle3, Entry2
  429.                                 if y = x then 
  430.                                     WriteLine Handle3, Entry
  431.                                     Print "Added ", entry, " at line ", y + 1
  432.                                     y = y + 1
  433.                                 endif
  434.                                 y = y + 1
  435.                             loop
  436.                             Close (Handle3)
  437.                             Close (Handle4)
  438.                             Delete (LClsFile)
  439.                             Rename (TClsFile, LClsFile)
  440.                             AddItem (NewCls, "New Client: " + CClient + " added to " + SerialNo + ".LST")
  441.                             AddToLog (LogFile, "New Client: " + CClient + " added to " + SerialNo + ".LST")
  442.                         endif
  443.                     else
  444.  
  445.                         // If a copy of the licence file is not found in
  446.                         // the controls install directory make the copy
  447.                         // with the file name serialno.LIC and create
  448.                         // a list file for the licence serialno.LST
  449.                         // Add the Client details to the list file.
  450.  
  451.                         LicenceCount = LicenceCount + 1
  452.                         Print "Copying the licence file to ", LCpFile
  453.                         Copy (LTmpFile, LCpFile)
  454.                         AddItem (NewLics, "New licence: " + SerialNo + " copied to " + LCpfile + " from Client " + GetClientName(CClient)))
  455.                         AddToLog (LogFile, "New licence: " + SerialNo + " copied to " + LCpfile)
  456.  
  457.                         // Now add an entry to the LICENCE.LST file with 
  458.                         // the serial number and maxslaves values for the
  459.                         // current licence.
  460.  
  461.                         If FileExists (LcLstFl) then
  462.                             Handle5 = Open (LcLstFl, FILE_APPEND)
  463.                         else
  464.                             Print "Creating ", LcLstFl
  465.                             Handle5 = Open (LcLstFl, FILE_CREATE)
  466.                         endif
  467.  
  468.                         If Handle5 Then
  469.                             WriteLine Handle5, LicEntry
  470.                             Close (Handle5)
  471.                         Endif
  472.  
  473.                         Handle1 = Open (LClsFile, FILE_CREATE)
  474.  
  475.                         If Handle1 then
  476.                             WriteLine Handle1, Entry
  477.                             Close (Handle1)
  478.                             AddItem (NewCls, "New Client: " + CClient + " added to " + SerialNo + ".LST")
  479.                             AddToLog (LogFile, "New Client: " + CClient + " added to " + SerialNo + ".LST")
  480.                         else 
  481.                             Print "Unable to create Client list file: ", LClsFile
  482.                         endif
  483.                     endif
  484.                 else
  485.  
  486.                     // Check to see why NSM.LIC could not be read.
  487.  
  488.                     If LASTERROR = 10 then
  489.                         Print "File Transfer on ", CClient, " has been disabled."
  490.                         AddToLog (LogFile, "File Transfer on " + CClient + " has been disabled.")
  491.                     else
  492.                         Print "Error (", LASTERROR, ") reading NSM.LIC on ", CClient
  493.                         AddToLog (LogFile, "Error reading NSM.LIC on " + CClient)
  494.                     endif
  495.                 endif
  496.  
  497.                 // Disconnect from the Client and continue on to the next.
  498.  
  499.                 Disconnect (CClient)
  500.             else
  501.                 Print "Could not connect to Client ", GetClientName (CCLient)
  502.             endif
  503.         Next
  504.  
  505.         // Print a summary
  506.  
  507.         Print ""   
  508.         Print "Number of Clients found: ", ClientCount
  509.         Print "New Licences found: ", LicenceCount
  510.         Print "------------"
  511.         For y = 1 to Items (NewLics,1)
  512.             Print GetItem (NewLics, y)
  513.         Next
  514.         Print "------------"
  515.         For y = 1 to Items (NewCls,1)
  516.             Print GetItem (NewCls, y)
  517.         Next
  518.     else 
  519.         print "No Clients found!"
  520.     endif
  521. End Function
  522.