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

  1. // HOSTNAME.SCP - Sample Script which updates the CLIENT.NSM file so that
  2. // it can be used in a DHCP environment with the Control's Connect by Hostname
  3. // Copyright (c) 2000, Vector Networks Limited
  4. // All Rights Reserved
  5. //
  6. // Revision History:
  7. // 5.3 19-Jul-00 AB  - Created.
  8. //     25-Jul-00 DB  - Remove $INCLUDE "RINSTR.SCP" to GTCTLDIR.SCP
  9. //     03-Aug-00 DB  - Fix the problem incrementing integers by not DIMming the
  10. //                     variables as Integers.
  11. // 6.0 12-Oct-00 DB  - Add $INCLUDE "PARSE.SCP" and "RINSTR.SCP" 
  12. //     13-Oct-00 DB  - Move ReadLine inside Do Until EOF loop
  13.  
  14. // This Script reads the Control's CLIENT.NSM file, updating the addresses of any
  15. // TCP/IP Clients to include the Client Name as a Hostname using the format:
  16. //  <client_name>|<ip_address> (<client_name>)|....
  17.  
  18. $INCLUDE "GTCTLFL.SCP"
  19. $INCLUDE "PARSE.SCP"
  20. $INCLUDE "RINSTR.SCP"
  21. $INCLUDE "TOKENS.SCP"
  22.  
  23. Function Main ()
  24.     Dim Profile as String, ControlDir as String
  25.     Dim ClientFile as String, NewFile as String, BackupFile as String
  26.     Dim Handle, Line, Name, SplitLine as List, IP, x, y
  27.     Dim ChangedLines, Lines, Loc as Integer
  28.  
  29.     Lines = 0
  30.     ChangedLines = 0
  31.  
  32.     ControlDir = GetControlDir ()
  33.  
  34. //    ClientFile = Trim (GetControlFile ("CLIENT.NSM", Profile))
  35.     ClientFile = GetControlFile ("CLIENT.NSM", Profile)
  36.  
  37.     Print "Target Client file: ", ClientFile
  38.  
  39.     Loc = RInStr (ClientFile, "\")
  40.     NewFile = Left (ClientFile, Loc) + "client.new"
  41.     BackupFile = Left (ClientFile, Loc) + "client.old"
  42.  
  43.     Print "Control directory: ", ControlDir
  44.     Print "Temporary file: ", NewFile
  45.     Print "Backup file name: ", BackupFile
  46.  
  47.     Handle = Open (ClientFile, FILE_READ)
  48.  
  49.     If Handle !=0 Then
  50.         Print "Reading file " + ClientFile + "..."
  51.         Print "File handle: ", Handle
  52.  
  53.         If FileExists (NewFile) then
  54.             Print "Overwriting file ", NewFile
  55.             Handle2 = Open (NewFile, FILE_OVERWRITE)
  56.         Else
  57.              Print "Creating file ", NewFile
  58.              Handle2 = Open (NewFile, FILE_CREATE) 
  59.         Endif
  60.  
  61.         If Handle2 !=0 Then
  62.             Print "File handle: ", Handle2
  63.  
  64.             do until EOF(Handle)
  65.                 Line = ReadLine (Handle)
  66.                 Lines = Lines + 1
  67.                 Print "Line ", Lines, " <", Line, ">"
  68.  
  69.                 If Trim (Line) != "" Then
  70.                     SplitLine = TokenList (Line, "|")
  71.                     If Items (SplitLine) < 5 Then
  72.                         Print "ERROR: Insufficient tokens found in line ", Lines, ":"
  73.                         For Each Token in SplitLine
  74.                             Print Token
  75.                         Next
  76.                         Exit Function
  77.                     Endif
  78.  
  79.                     Name = GetItem (SplitLine, 1)
  80.                     IP = GetItem (SplitLine, 2)
  81.                     Trans = GetItem (SplitLine, 3)
  82.                     Loc = GetItem (SplitLine, 4)
  83.                     Net = GetItem (SplitLine, 5)
  84.  
  85.                     If Trans = "2" AND Loc = "0" AND Net = "Local" AND InStr (IP, " (", IGNORECASE) = 0 then
  86.                         Print "Correcting: ", Name, " Address: ", IP
  87.                         IP = IP + " (" + Name + ")"
  88.                         Print "New address: ", IP
  89.                         ChangedLines = ChangedLines + 1
  90.                     Else
  91.                         Print "Skipping Client: ", Name
  92.                     Endif
  93.  
  94.                     Line = Name + "|" + IP
  95.  
  96.                     y = Items (SplitLine)
  97.                     For x = 3 to y
  98.                         Line = Line + "|" + GetItem (SplitLine, x) 
  99.                     Next
  100.  
  101.                     WriteLine Handle2, Line
  102.                 Endif
  103.             Loop
  104.  
  105.             Close (Handle2)
  106.         Endif
  107.  
  108.         Close (Handle)
  109.  
  110.         If FileExists (BackupFile) then Delete (BackupFile)
  111.         If Rename (ClientFile, BackupFile) then 
  112.             Rename (NewFile, ClientFile)
  113.         Endif
  114.     Endif
  115.  
  116.     Print "Summary"
  117.     Print Lines, " lines processed"
  118.     Print ChangedLines, " lines changed"
  119. End Function
  120.