1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
'Option Explicit
'variable declaration
'Dim strByteMatch, strIpMatch, strPattern, CSVfile, LOGfile, FolderCFG, objFSO, RegExMatch, strLog, objTextFile, File, arrStr, strNAME, strIP, arrIP, strREPLACEMENT, objCFGFile, strCFGcontent, intOCT1, intOCT2, intOCT3, intOCT4
'Pattern for IP check
strByteMatch = "(25[0-5]|2[0-4]\d|[01]?\d\d?)"
strIpMatch = strByteMatch & "\." & strByteMatch & "\." & strByteMatch & _
"\.(" & strByteMatch & "|(" & strByteMatch & "-" & strByteMatch & "))"
strPattern = "^" & strIpMatch & "(," & strIpMatch & ")*$"
'Set File variable
CSVfile = "..\Replacement_IP.csv"
LOGfile = "..\LOG\Replacement_IP.log"
FolderCFG = "..\ConfigFiles\"
'set FSO object
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Does file exist?
If objFSO.FileExists(CSVfile)=false then
Log("Error : File " & CSVfile & " not found!")
wscript.echo "Error : File " & CSVfile & " not found!"
wscript.Quit 1
end If
'Open CSV file, read mod.
Set objTextFile = objFSO.OpenTextFile(CSVfile, 1)
Do while not (objTextFile.AtEndOfStream)
'search loop filename to edit
For Each File in objFSO.GetFolder(FolderCFG).Files
If Right(LCase(File.Name), 3) = LCase("cfg") And InStr(LCase(File.Name),LCase("XXX")) <> 0 Then
'split CSV data's in array
arrStr = Split(objTextFile.ReadLine, ";")
'Check CSV IP replacement
If Not RegExIsMatch(arrStr(1), strPattern) Then
Log("Error : IP invalid in " & CSVfile & ", row : " & objTextFile.Line & ", col : 2")
WScript.Echo "Error : IP invalid in " & CSVfile & ", row : " & objTextFile.Line & ", col : 2"
wscript.Quit 1
End If
If Not RegExIsMatch(arrStr(2), strPattern) Then
Log("Error : IP invalid in " & CSVfile & ", row : " & objTextFile.Line & " col : 3")
WScript.Echo "Error : IP invalid in " & CSVfile & ", row : " & objTextFile.Line & " col : 3"
wscript.Quit 1
End If
'Name IP to replace
strNAME = arrStr(0)
'IP to replace
strIP = arrStr(1)
'IP replacement
strREPLACEMENT = arrStr(2)
'Open CFG file, read mod
Set objCFGFile = objFSO.OpenTextFile(FolderCFG & File.Name, 1)
'Get content of CFG file
strCFGcontent = objCFGFile.ReadALL
'close CFG file
objCFGFile.close
LOG("Replace " & strNAME & " in " & File.Name & " : " & strIP & " by " & strREPLACEMENT)
strCFGcontent = Replace(strCFGcontent, strIP, strREPLACEMENT)
Set objCFGFile = objFSO.OpenTextFile(FolderCFG & File.Name, 2)
objCFGFile.WriteLine strCFGcontent
objCFGFile.close
End if
Next
Loop
objTextFile.close
Log("Replacement IP it's done.")
Log("-------------------------")
wscript.Quit 0
' -------- FUNCTION SECTION --------
'Function check IP
Function RegExIsMatch(strString,strPattern)
Dim RegEx
RegExMatch=False
Set RegEx = New RegExp
RegEx.IgnoreCase = True
RegEx.Global=True
RegEx.Pattern=strPattern
If RegEx.Test(strString) Then RegExIsMatch=True
End Function
'function LOG
Function LOG (strLog)
Dim objLogFile
'Open LOG file, write append mod.
Set objLogFile = objFSO.OpenTextFile(LOGfile, 8, true)
objLogFile.WriteLine Date & " - " & Time & " " & strLog
'debug wscript.echo strLog
objLogFile.close
End Function |
Partager