Find bug in Section3 of this vbscript
In below script there is a bug in section3, I am not able to solve it. I am getting password from section2 and that password I want to copy in Section3.
In below script, whenever I am replacing string in config.cfg, all other lines in that file are getting deleted, and I can see only
"/AgentSetup/defaultAccount" = { REPLACE="SA/EzmeGJz3xqYSY+s+8S2On89D+tU1ikPe" },"
'Common
Set objfso = Createobject("scripting.filesystemobject")
Set objshell = Createobject("wscript.shell")
ScriptName = WScript.ScriptFullName
FilePath = Left(ScriptName, InstrRev(ScriptName, "\"))
strprofiles = objshell.expandenvironmentstrings("%programfiles%")
Const ForReading = 1
'........................................................................................................
'Section1: set paths and run 1st command which generates encrypted passwords
Path1 = strprofiles & "\BMC Software\common\security\bin_v3.0\Windows-x86\esstool.exe"
msgbox path1
Path2 = "cmd.exe /c " & chr(34) & strprofiles & "\BMC Software\common\security\bin_v3.0\Windows-x86\esstool.exe" & chr(34) & " encryptor -e Patrol" & " > c:\temp.txt"
msgbox path2
'Path2 =
'If objfso.fileexists (path1) then
'objshell.run path2,1,true
'end if
'........................................................................................................
'Section2: Encrypted password is getting stored in strpassword
Set objFile = objFSO.OpenTextFile("C:\Text.txt", ForReading)
strcontents = objFile.ReadAll
arrcontents = Split(strcontents, vbnewline)
For Each strline in arrcontents
if instr(1,Lcase(strline),"patrol->",1) then
arrpassword = Split(strline,">")
end if
Next
strPassword = arrpassword(1)
msgbox strPassword
'........................................................................................................
'Section3: create cfg file, and replace it's line with this encrypted password: strPassword
path3 = "cmd.exe /c " & chr(34) & "pconfig.exe" & chr(34) & " +get +Defaults" & " > c:\config.cfg"
msgbox path3
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\config.cfg", ForReading)
'strText = objFile.ReadAll
objFile.Close
strNewText = Replace("""/AgentSetup/defaultAccount"" = { REPLACE=""SA/password"" },""", "password", strpassword)
msgbox strnewtext
Set objFile = objFSO.OpenTextFile("c:\config.cfg", ForWriting)
objFile.WriteLine strNewText
objFile.Close
In below script, whenever I am replacing string in config.cfg, all other lines in that file are getting deleted, and I can see only
"/AgentSetup/defaultAccount" = { REPLACE="SA/EzmeGJz3xqYSY+s+8S2On89D+tU1ikPe" },"
'Common
Set objfso = Createobject("scripting.filesystemobject")
Set objshell = Createobject("wscript.shell")
ScriptName = WScript.ScriptFullName
FilePath = Left(ScriptName, InstrRev(ScriptName, "\"))
strprofiles = objshell.expandenvironmentstrings("%programfiles%")
Const ForReading = 1
'........................................................................................................
'Section1: set paths and run 1st command which generates encrypted passwords
Path1 = strprofiles & "\BMC Software\common\security\bin_v3.0\Windows-x86\esstool.exe"
msgbox path1
Path2 = "cmd.exe /c " & chr(34) & strprofiles & "\BMC Software\common\security\bin_v3.0\Windows-x86\esstool.exe" & chr(34) & " encryptor -e Patrol" & " > c:\temp.txt"
msgbox path2
'Path2 =
'If objfso.fileexists (path1) then
'objshell.run path2,1,true
'end if
'........................................................................................................
'Section2: Encrypted password is getting stored in strpassword
Set objFile = objFSO.OpenTextFile("C:\Text.txt", ForReading)
strcontents = objFile.ReadAll
arrcontents = Split(strcontents, vbnewline)
For Each strline in arrcontents
if instr(1,Lcase(strline),"patrol->",1) then
arrpassword = Split(strline,">")
end if
Next
strPassword = arrpassword(1)
msgbox strPassword
'........................................................................................................
'Section3: create cfg file, and replace it's line with this encrypted password: strPassword
path3 = "cmd.exe /c " & chr(34) & "pconfig.exe" & chr(34) & " +get +Defaults" & " > c:\config.cfg"
msgbox path3
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\config.cfg", ForReading)
'strText = objFile.ReadAll
objFile.Close
strNewText = Replace("""/AgentSetup/defaultAccount"" = { REPLACE=""SA/password"" },""", "password", strpassword)
msgbox strnewtext
Set objFile = objFSO.OpenTextFile("c:\config.cfg", ForWriting)
objFile.WriteLine strNewText
objFile.Close
0 Comments
[ + ] Show comments
Answers (6)
Please log in to answer
Posted by:
anonymous_9363
15 years ago
It's a bit hard to decipher what's happening here. Show us:
- The relevant line which gets written to the text file
- The text within that line which should be replaced
- The text which will be used to replace it
- Is the file temp.txt or text.txt?!? Your code shows both! Herein is the most powerful lesson: if you use variables, these issues don't arise. Think about using variables like strFileNameTemp, strTextToSearchFor, strTextToReplace, strTextReplacementMarker, etc, etc.
- The relevant line which gets written to the text file
- The text within that line which should be replaced
- The text which will be used to replace it
- Is the file temp.txt or text.txt?!? Your code shows both! Herein is the most powerful lesson: if you use variables, these issues don't arise. Think about using variables like strFileNameTemp, strTextToSearchFor, strTextToReplace, strTextReplacementMarker, etc, etc.
Posted by:
captain_planet
15 years ago
I think you may be using the 'Replace' function incorrectly. Try this below, though you'd probably do a more accurate string replacement using a regular expression.....:
'Section3: create cfg file, and replace it's line with this encrypted password: strPassword
path3 = "cmd.exe /c " & chr(34) & "pconfig.exe" & chr(34) & " +get +Defaults" & " > c:\config.cfg"
msgbox path3
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\config.cfg", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "SA/password", "SA/" & strpassword)
msgbox strnewtext
Set objFile = objFSO.OpenTextFile("c:\config.cfg", ForWriting)
objFile.WriteLine strNewText
objFile.Close
Posted by:
abking99
15 years ago
Site policy = 'SOFTWARE\BMC Software\Patrol\SecurityPolicy_v3.0\site'
Appl policy = 'SOFTWARE\BMC Software\Patrol\SecurityPolicy_v3.0\agent'
ModuleName C:\Program Files\BMC Software\common\security\bin_v3.0\Windows-x86\bmcpwk80.dll
BPW Module, Version 1.0|ess3.0.11.0|win32|Oct 24 2007|19:34:56
patrol->EzmeGJz3xqYSY+s+8S2On89D+tU1ikPe
BPW Module, Version 1.0|ess3.0.11.0|win32|Oct 24 2007|19:34:56
Anticipated decryption error -1.
Posted by:
abking99
15 years ago
Set objFile = objFSO.OpenTextFile("c:\config.cfg", ForReading)
so I was getting that error....but code which captain provided,still does not work, it is replacing password field, but wherever it finds password field in cfg file, it replace every password field. I want it to replace only in my line.
Posted by:
anonymous_9363
15 years ago
It's doing exactly what you're telling it to do, that's why.
You have strText set to be the entire file (because you're using the 'ReadAll' method). Then your replacement code tells it to replace strText with the password text.
What you need to do is either loop through the file line-by-line (using the ReadLine method) and, when you get to the line containing the text to be replaced, replace it then, or use a regular expression to "search" strText and replace the text which matches the expression with the password text.
You have strText set to be the entire file (because you're using the 'ReadAll' method). Then your replacement code tells it to replace strText with the password text.
What you need to do is either loop through the file line-by-line (using the ReadLine method) and, when you get to the line containing the text to be replaced, replace it then, or use a regular expression to "search" strText and replace the text which matches the expression with the password text.
Posted by:
captain_planet
15 years ago
Yeah - either loop through it, use a regular expression, or try this below. I do prefer regular expressions but they have been known to bamboozle me sometimes (most times....[;)]), so this is a simpler (though possibly less robust?) solution:
Const ForWriting = 2
Const ForReading = 1
Dim newConfigData : newConfigData = ""
Dim cfgFilePath : cfgFilePath = "c:\config.cfg"
Dim lineToReplace : lineToReplace = Chr(34) & "/AgentSetup/defaultAccount" & Chr(34) & " = { REPLACE=" & Chr(34) & "SA/password" & Chr(34) & " }," & Chr(34)
Dim replacementLine : replacementLine = ""
'retrieve your encypted password by whatever means
strpassword = "EzmeGJz3xqYSY+s+8S2On89D+tU1ikPe"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(cfgFilePath) Then
'open cfg file in readonly mode
Set objFile = objFSO.OpenTextFile(cfgFilePath , ForReading)
'read the whole cfg file and store it in a variable
originalConfigData = objFile.ReadAll
'Perform string replacement
replacementLine = replace(lineToReplace, "password", strpassword)
newConfigData = Replace(originalConfigData, lineToReplace, replacementLine)
'Write new config.cfg data
Set objFile = objFSO.OpenTextFile(cfgFilePath , ForWriting)
objFile.WriteLine newConfigData
'close the handle which has the cfg file open
objFile.Close
Else
'config.cfg doesnt exist
End If
Set objFSO = Nothing
Set objFile = Nothing
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
so that the conversation will remain readable.