'----------------------------------------------------------------------------------------------------------------
' Note: This function is duplicated elsewhere; any changes here should be reflected in the other implementations!
Function FixOutput(origStr)
' Create regular expression (set to replace all occurences)
Dim regEx
Set regEx = New RegExp
regEx.Global = True
' Replace all occurences of "\rText" with "\r\nText"
regEx.Pattern = "\r([^\n])" ' Set pattern: \r not followed by \n
FixOutput = regEx.Replace(origStr, vbCrLf + "$1") ' Make replacement: \r\n + following byte
' Make sure all our line endings are Windows standard (\r\n), not just \n
regEx.Pattern = "([^\r])\n"
FixOutput = regEx.Replace(FixOutput, "$1" + vbCrLf)
' Eliminate most non-printable low-byte ASCII characters (avoids OpsMgr XML encoding problems)
' (Keep white space like tab, \r, \n)
regEx.Pattern = "[\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F]"
FixOutput = regEx.Replace(FixOutput, "")
End Function
' Note: This function is duplicated elsewhere; any changes here should be reflected in the other implementations!
Function WriteLog(strLogText)
Dim objfs, objf, regEx, logOutput
if (Logging = 1) Then
' Add the Date/Time header to each line
Set regEx = New RegExp
regEx.Global = True
regEx.Pattern = "\r\n"
logOutput = regEx.Replace(logOutput, vbCrLf & "[" & Date & " " & Time & "] ")
Set objfs = CreateObject("Scripting.FileSystemObject")
Set objf = objfs.OpentextFile(TempFolder & "\" & Wscript.ScriptName & ".log", 8, True)
objf.WriteLine("[" & Date & " " & Time & "] " & logOutput)
Set objfs = Nothing
Set objf = Nothing
End If
End Function
Dim Logging, TempFolder
TempFolder = WScript.CreateObject("Scripting.FileSystemObject").GetSpecialFolder(2)
If Wscript.CreateObject("Scripting.FileSystemObject").FileExists(TempFolder & "\EnableOpsMgrModuleLogging") then
Wscript.Echo "Debug: Logging to file '" & TempFolder & "\" & Wscript.ScriptName & ".log'"
Logging = 1
End If
'----------------------------------------------------------------------------------------------------------------
Dim oArgs, SecureInput
Set oArgs = WScript.Arguments
' Split the SecureInput using a fixed string "@@secureseparator@@". Cannot use " " as passwords may contain spaces.
SecureInput = Split(WScript.StdIn.ReadLine(), "@@secureseparator@@")
If (oArgs.Count < 5) OR (UBound(SecureInput) < 2) Then
WScript.Echo "Usage: SSH.vbs Host Port SUCommand Command InstallPackage"
WScript.Echo "Secure Input: Username Password SUPassword"
WScript.Echo "Create file '" & TempFolder & "\EnableOpsMgrModuleLogging' to enable logging"
Wscript.Quit 1
End If
'Create SSH Object
Set scxssh = CreateObject("sshcom.scxssh")
'Set Connection info
scxssh.ConnectWithPassword Host, Port, Username, Password
Dim sStart, sEnd, sCompressionSuffix, sUnCompressCommand, sInstallCommand, sCheckFileCommand, sRemoveCommand
'Check if InstallPackage has compression suffix (only .Z and .gz are supported)
'If InstallPackage is compressed there will be a period "." appended
sStart = InStr(command, InstallPackage & ".")
If sStart > 0 then
If (Mid(command, sStart+Len(InstallPackage), 2)) = ".Z" then
sCompressionSuffix = ".Z"
sUnCompressCommand = Mid(command, 1, sStart+Len(InstallPackage)+1)
sInstallCommand = Mid(command, sStart+Len(InstallPackage)+3, Len(command))
ElseIf (Mid(command, sStart+Len(InstallPackage), 3)) = ".gz" then
sCompressionSuffix = ".gz"
sUnCompressCommand = Mid(command, 1, sStart+Len(InstallPackage)+2)
sInstallCommand = Mid(command, sStart+Len(InstallPackage)+4, Len(command))
Else
WriteLog("Unknown compression suffix, aborting! (" & InstallPackage & ")")
WScript.Quit 1
End If
Else
'InstallPackage is not compressed
sCompressionSuffix=""
sUnCompressCommand=""
sInstallCommand=command
End If
'Add SU prefix to InstallCommand
sInstallCommand = SUCommand & " '" & sInstallCommand & "'"
'Debug printouts
'WScript.Echo "Original Command: " & command & " Len: " & Len(command)
'WScript.Echo "CompressionSuffix: " & sCompressionSuffix & " Len: " & Len(sCompressionSuffix)
'WScript.Echo "UnCompressCommand: " & sUnCompressCommand & " Len: " & Len(sUnCompressCommand)
'WScript.Echo "InstallCommand: " & sInstallCommand & " Len: " & Len(sInstallCommand)
'If file is compressed, decompress it
If sUnCompressCommand <> "" then
WriteLog("Executing command: " & sUnCompressCommand)
Status = 1
' Need to escape \n
Status = scxssh.ExecuteCommand3(Replace(sUnCompressCommand, "\n", "\\n"))
Result = scxssh.GetResponse()
WriteLog(Result)
' Handle Failed Command Execution
If CInt(Status) <> 0 then
WriteLog("Uncompresscommand Failed")
WScript.Quit CInt(Status)
End If
End If
'Open a shell for execution
Status = 1
Status = scxssh.OpenShell()
Result = scxssh.GetResponse()
WriteLog(Result)
' Handle Failed Connection
If CInt(Status) <> 0 then
WriteLog("Failed to connect")
WScript.Quit CInt(Status)
End If
Status = 1
Status = scxssh.ShellCommand3(sInstallCommand)
Result = scxssh.GetResponse()
WriteLog(Result)
' Handle Failed Command Execution
If CInt(Status) <> 0 then
WriteLog("InstallCommand Failed")
WScript.Quit CInt(Status)
End If
If Username <> "root" Then
'Send root password (install command uses 'su')
Status = 1
Status = scxssh.ShellCommand3(SUPassword)
Result = scxssh.GetResponse()
WriteLog(Replace(Result,SUPassword,"<Password>"))
' Handle Failed Command Execution
If CInt(Status) <> 0 then
WriteLog("SUPWCommand Failed")
WScript.Quit CInt(Status)
End If
End If
'Close the Shell and get returncode from install command
Status = 1
Status = scxssh.CloseShell()
Result = scxssh.GetResponse()
WriteLog(Result)
'Remove the staging folder, otherwise next installation may fail if another user is used.
sRemoveCommand = "rm -rf /tmp/scx-" & Username
WriteLog("Executing command: " & sRemoveCommand)
scxssh.ExecuteCommand3 sRemoveCommand
Result = scxssh.GetResponse()
WriteLog(Result)
'Close the SSH session
scxssh.Shutdown()
'Return status from the installation, convert from HResult
WScript.Quit CInt(Status) </Script></Contents>
<Unicode>1</Unicode>
</File>
</Files>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="InstallElevatedAgent"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.BaseData</OutputType>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>