Hallo Gemeinde,
die Func ReadAllConsoles ruft nacheinander die Func SSHRead auf. Dadurch werden nacheinander die 4 SSH-Consolen unserer KVM-Switche ausgelesen in ein Array.
Das Array wird formatiert und gibt die Portnummer des gesuchten Computers und den Namen zurück. Da der Auslese-Vorgang etwas dauern kann habe ich einen Spinner hinzugefügt.
Wenn der Fall eintritt, dass der Computer zwar gefunden wurde aber trotzdem nicht zur Verfügung steht, da er heruntergefahren ist, wird eine Warnung ausgegeben und die Suche abgebrochen.
Leider schaffe ich es nicht, für diesem Fall, die Spinner-Gui zu beenden und auszublenden. GuiDelete gibt zwar die 1 zurück aber das Fenster bleibt weiter sichtbar.
Hat jemand eine Idee woran das Liegen könnte?
Code
Func SSHRead($sKVM)
Local $sFoundNumber = -1
LogWrite(857, "SSHRead started for KVM: " & $sKVM)
; Decrypt login credentials
Local $s_Account = DeencryptionKVM($s_Konto, $sWert)
Local $s_Pwd = DeencryptionKVM($s_Parole, $sWert)
; Run SSH command using Plink
Local $sOutFile = @TempDir & "\output.txt"
Local $iPID = Run(@ComSpec & " /c " & $sVar4 & " " & $s_Account & "@" & $sKVM & " -pw " & $s_Pwd & " -batch > " & $sOutFile & " 2>&1", @TempDir, @SW_HIDE)
LogWrite(867, "SSH command launched. PID: " & $iPID)
Sleep(1500) ; Wait for output
; Clear credentials from memory
$s_Account = ""
$s_Pwd = ""
; Open and read output file
Local $hFile = FileOpen($sOutFile, $FO_READ)
If $hFile = -1 Then
LogWrite(877, "Error opening output file.")
Return SetError(1, 0, -1)
EndIf
Local $aArray = FileReadToArray($hFile)
FileClose($hFile)
If @error Or Not IsArray($aArray) Then
LogWrite(885, "Error reading SSH output.")
Return SetError(2, 0, -1)
EndIf
; Keep only relevant lines (indices 10–41)
For $i = UBound($aArray) - 1 To 42 Step -1
_ArrayDelete($aArray, $i)
Next
For $i = 9 To 0 Step -1
_ArrayDelete($aArray, $i)
Next
LogWrite(897, "SSH output trimmed. Entries remaining: " & UBound($aArray))
; Search through array
For $i = 0 To UBound($aArray) - 1
Local $sOriginalLine = $aArray[$i]
Local $sLine = StringStripWS($sOriginalLine, $STR_STRIPALL)
; Check for availability flags
Local $bUnavailable = (StringInStr($sLine, "NotAvailable") Or StringInStr($sLine, "down"))
; Extract computer name part
Local $sCleanLine = StringRegExpReplace($sLine, "_[^_]*$", "") ; Remove trailing suffix after "_"
Local $sName = StringRegExpReplace($sCleanLine, "^\d+-", "") ; Remove leading number and dash
$sName = StringRegExpReplace($sName, "^CERT[1-5]-", "") ; Remove CERTx- prefix
; Compare with entered computer name
If StringCompare($sName, $s_Compname, 0) = 0 Then
If $bUnavailable Then
LogWrite(915, "Computer '" & $s_Compname & "' found but not available: '" & $sOriginalLine & "'")
Return SetError(10, 0, -1) ; Code 10 = found but not available
EndIf
; Extract port number (digits before first "-")
Local $sNum = StringRegExpReplace($sLine, "^(\d+)-.*$", "\1")
LogWrite(921, "Computer '" & $s_Compname & "' found on port: " & $sNum)
Return $sNum
EndIf
Next
; Terminate plink.exe if still running
Local $plinkPID = ProcessExists("plink.exe")
If $plinkPID Then
ProcessClose($plinkPID)
ProcessWaitClose($plinkPID)
LogWrite(931, "plink.exe process terminated.")
EndIf
FileDelete($sOutFile)
LogWrite(935, "Computer '" & $s_Compname & "' NOT found on " & $sKVM)
Return SetError(3, 0, -1)
EndFunc ;==>SSHRead
Func ReadAllConsoles()
LogWrite(941, "Starting console scan for computer: " & $s_Compname)
Disablecontrols()
ShowSpinnerGUI("🔍 Searching for '" & $s_Compname & "'...")
Local $aKVMs[5] = [$sKVM1, $sKVM2l, $sKVM3, $sKVM4]
Local $sFoundPort = ""
Local $bFoundButUnavailable = False
For $i = 0 To UBound($aKVMs) - 1
Local $sPort = SSHRead($aKVMs[$i])
Switch @error
Case 0
$sFoundPort = $sPort
LogWrite(957, "Computer found and available. Port: " & $sPort)
ExitLoop
Case 10
LogWrite(962, "Computer found on KVM but not available.")
$bFoundButUnavailable = True
ExitLoop
Case Else
LogWrite(968, "Computer not found on " & $aKVMs[$i])
EndSwitch
Next
; Spinner GUI jetzt sicher beenden, bevor irgendetwas angezeigt oder abgebrochen wird
HideSpinnerGUI()
Sleep(500)
If $bFoundButUnavailable Then
LogWrite(975, "Computer found but not available (down or not reachable).")
;MsgBox(48, "Information", "The computer '" & $s_Compname & "' was found, but is not available (down or unreachable).")
EnableControls()
Return
EndIf
If $sFoundPort = "" Then
LogWrite(982, "Computer '" & $s_Compname & "' not found on any KVM.")
;MsgBox(48, "Not Found", "The computer '" & $s_Compname & "' could not be found on any KVM.")
EnableControls()
Return
EndIf
LogWrite(989, "Generating URL with found port: " & $sFoundPort)
GenerateUrl("", $sFoundPort)
EndFunc ;==>ReadAllConsoles
; Die Spinner Funktionen:
Func ShowSpinnerGUI($sText)
; Größe des Spinner-Fensters
Local $iWidth = 400
Local $iHeight = 80
; Position der Haupt-GUI ermitteln
Local $aPos = WinGetPos($Form1_1)
Local $iX, $iY
If @error Then
; Falls nicht bekannt, einfach zentriert am Bildschirm
$iX = -1
$iY = -1
Else
; Zentriert relativ zur Haupt-GUI
$iX = $aPos[0] + Int(($aPos[2] - $iWidth) / 2)
$iY = $aPos[1] + Int(($aPos[3] - $iHeight) / 2)
EndIf
; Fenster mit Popup und Dialog-Rahmen erstellen
$hSpinnerGUI = GUICreate("Search in progress...", $iWidth, $iHeight, $iX, $iY, BitOR($WS_POPUP, $WS_DLGFRAME), $WS_EX_TOPMOST)
; Label mit Text und Spinner
$idLabel = GUICtrlCreateLabel($sText & " " & $aSpinner[$iSpinnerIndex], 10, 30, 380, 40, $SS_CENTER)
; Hintergrundfarbe setzen (weiß)
GUISetBkColor(0xFFFFFF, $hSpinnerGUI)
; Schriftgröße und Fett
;GUICtrlSetFont($idLabel, 12, 600)
GUICtrlSetFont($idLabel, 12, 600, 0, "Consolas") ; 12pt, fett, nicht kursiv, Consolas
; Fenster anzeigen
GUISetState(@SW_SHOW, $hSpinnerGUI)
; Timer für Spinner starten
AdlibRegister("UpdateSpinnerLabel", 100)
EndFunc ;==>ShowSpinnerGUI
; Im Update-Loop nur Spinner-Zeichen austauschen, z.B. so:
Func UpdateSpinnerLabel()
; Nur das rotierende Zeichen ändern, Text bleibt gleich lang
Local $sBaseText = "🔍 search for '" & $s_Compname & "'... "
$iSpinnerIndex = Mod($iSpinnerIndex + 1, UBound($aSpinner))
GUICtrlSetData($idLabel, $sBaseText & " " & $aSpinner[$iSpinnerIndex])
EndFunc ;==>UpdateSpinnerLabel
Func HideSpinnerGUI()
AdlibUnRegister("UpdateSpinnerLabel")
If $hSpinnerGUI Then
Local $s_Handle = GUIDelete($hSpinnerGUI)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $s_Handle = ' & $s_Handle & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
$hSpinnerGUI = 0
EndIf
EndFunc ;==>HideSpinnerGUI
Alles anzeigen