Hallo allerseits, ich habe ein kleines script was mir ein Logfile auswerten soll, dieses Logfile hat folgende Struktur:
Code
2024/06/23 18:02:36 - Client connected
2024/06/23 18:02:36 - action=login pcname=N210708_b mac=5C61994A3157 ip=192.168.178.22 time=18:02:36
2024/06/23 18:06:55 - Client connected
2024/06/23 18:06:55 - action=logout pcname=N210708_B mac=5C61994A3157 ip=192.168.178.22 time=18:06:55
2024/06/23 18:10:30 - Client connected
2024/06/23 18:10:30 - action=login pcname=N210708_B mac=5C61994A3157 ip=192.168.178.22 time=18:10:30
2024/06/23 18:13:50 - Client connected
2024/06/23 18:13:50 - action=logout pcname=N210708_B mac=5C61994A3157 ip=192.168.178.22 time=18:13:50
2024/06/23 18:18:20 - Server started and listening on port 8085
Mir ist es inzwischen geglückt die Sortierfunktion zum Laufen zu bringen. Das einzige Problem was ich habe ist die Sortierung der IPS, hat da jemand ne Idee? Für sachdienliche Hinweise wär ich sehr dankbar
AutoIt
#include <Array.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <StructureConstants.au3>
#include <WinAPI.au3>
#include <ListViewConstants.au3>
;only one Instance
$g_szVersion = "My OnlineReader"
If WinExists($g_szVersion) Then
Exit
EndIf
AutoItWinSetTitle($g_szVersion)
Global $sFilePath = "\\192.168.100.166\C$\windows\logfile.txt"
Global $csvFilePath = "\\cms.lch-bln.de\freigaben\inv2\Summary_Short.csv"
Global $aLines, $iLines
Global $csvData
Global $loggedInPCs[1][5] ; 2d Array initialisieren
Global $sortOrder = True ; True = Ascending, False = Descending
Global $lastSortedColumn = -1
; Create the GUI
Global $hGUI = GUICreate("Online PCs", 600, 400)
Global $hListView = GUICtrlCreateListView("PC Name|MAC Address|IP Address|Bezeichnung", 10, 10, 580, 300, $LVS_REPORT)
Global $hButton = GUICtrlCreateButton("Update", 250, 320, 100, 30)
GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
Global $B_DESCENDING[_GUICtrlListView_GetColumnCount($hListView)]
; CSV dazuladen
_LoadCSVData()
; Listview erzeugen
_UpdateListView()
; Register WM_NOTIFY handler
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY_Handler")
; Event loop
While True
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
Case $hButton
; Clear and update the ListView when the button is clicked
_GUICtrlListView_DeleteAllItems($hListView)
_UpdateListView()
EndSwitch
WEnd
Func _LoadCSVData()
; Check if the CSV file exists
If Not FileExists($csvFilePath) Then
MsgBox(0, "Error", "CSV file not found: " & $csvFilePath)
Exit
EndIf
; Read the CSV file into an array
If _FileReadToArray($csvFilePath, $csvData) = 0 Then
MsgBox(0, "Error", "Failed to read CSV file: " & $csvFilePath)
Exit
EndIf
EndFunc
Func _UpdateListView()
; Reset the loggedInPCs array
ReDim $loggedInPCs[1][5]
$loggedInPCs[0][0] = ""
$loggedInPCs[0][1] = ""
$loggedInPCs[0][2] = ""
$loggedInPCs[0][3] = ""
$loggedInPCs[0][4] = False
; Check if the file exists
If Not FileExists($sFilePath) Then
MsgBox(0, "Error", "File not found: " & $sFilePath)
Return
EndIf
; Read the file into an array
If _FileReadToArray($sFilePath, $aLines) = 0 Then
MsgBox(0, "Error", "Failed to read file: " & $sFilePath)
Return
EndIf
$iLines = UBound($aLines)
; jede Zeile im log durchgehen
For $i = 1 To $iLines - 1
; Check "action=login"
If StringInStr($aLines[$i], "action=login") Then
Local $pcname = _GetParameter($aLines[$i], "pcname")
Local $mac = _FormatMAC(_GetParameter($aLines[$i], "mac"))
Local $ip = _GetParameter($aLines[$i], "ip")
_SetPCStatus($pcname, $mac, $ip, True)
ElseIf StringInStr($aLines[$i], "action=logout") Then
Local $pcname = _GetParameter($aLines[$i], "pcname")
; PC logged out
_SetPCStatus($pcname, "", "", False)
EndIf
Next
; Liste von Onlien PC erstellen
For $i = 0 To UBound($loggedInPCs) - 1
If $loggedInPCs[$i][4] Then
; Bezeichnung von externer CSV einlesen
Local $bezeichnung = _GetBezeichnung($loggedInPCs[$i][0])
GUICtrlCreateListViewItem($loggedInPCs[$i][0] & "|" & $loggedInPCs[$i][1] & "|" & $loggedInPCs[$i][2] & "|" & $bezeichnung, $hListView)
EndIf
Next
EndFunc
Func _GetParameter($sLine, $sParam)
Local $iStart = StringInStr($sLine, $sParam & "=")
If $iStart = 0 Then Return ""
$iStart += StringLen($sParam) + 1
Local $iEnd = StringInStr($sLine, " ", 0, 1, $iStart)
If $iEnd = 0 Then $iEnd = StringLen($sLine) + 1
Return StringMid($sLine, $iStart, $iEnd - $iStart)
EndFunc
Func _FormatMAC($mac)
$mac = StringReplace($mac, "-", ":")
$mac = StringRegExpReplace($mac, "(..)(..)(..)(..)(..)(..)", "\1:\2:\3:\4:\5:\6")
Return $mac
EndFunc
Func _SetPCStatus($pcname, $mac, $ip, $status)
For $i = 0 To UBound($loggedInPCs) - 1
If $loggedInPCs[$i][0] = $pcname Then
$loggedInPCs[$i][1] = $mac
$loggedInPCs[$i][2] = $ip
$loggedInPCs[$i][4] = $status
Return
EndIf
Next
;neuen PC hinzufügen
If $loggedInPCs[0][0] = "" Then
$loggedInPCs[0][0] = $pcname
$loggedInPCs[0][1] = $mac
$loggedInPCs[0][2] = $ip
$loggedInPCs[0][3] = ""
$loggedInPCs[0][4] = $status
Else
ReDim $loggedInPCs[UBound($loggedInPCs) + 1][5]
$loggedInPCs[UBound($loggedInPCs) - 1][0] = $pcname
$loggedInPCs[UBound($loggedInPCs) - 1][1] = $mac
$loggedInPCs[UBound($loggedInPCs) - 1][2] = $ip
$loggedInPCs[UBound($loggedInPCs) - 1][3] = ""
$loggedInPCs[UBound($loggedInPCs) - 1][4] = $status
EndIf
EndFunc
Func _GetBezeichnung($pcname)
For $i = 1 To UBound($csvData) - 1 ; Kopfpalte überspringen
Local $csvLine = StringSplit($csvData[$i], ";", 1)
Local $csvPCName = StringStripWS($csvLine[1], 3) ;
If StringReplace($csvPCName, '"', '') = $pcname Then
Local $csvBezeichnung = StringStripWS($csvLine[2], 3)
Return StringReplace($csvBezeichnung, '"', '')
EndIf
Next
Return ""
EndFunc
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
$hWndListView = $hListView
If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
If ($hWndFrom = $hWndListView) And ($iCode = $LVN_COLUMNCLICK) Then
$tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
_GUICtrlListView_SimpleSort($hWndListView, $B_DESCENDING, DllStructGetData($tInfo, "SubItem"))
EndIf
EndFunc ;==>WM_NOTIFY
Func _SortListView($iCol)
Local $aListViewData[0]
Local $iItemCount = _GUICtrlListView_GetItemCount($hListView)
; Listview in Array lesen
For $i = 0 To $iItemCount - 1
Local $sRow = ""
For $j = 0 To _GUICtrlListView_GetColumnCount($hListView) - 1
$sRow &= _GUICtrlListView_GetItemText($hListView, $i, $j)
If $j < _GUICtrlListView_GetColumnCount($hListView) - 1 Then $sRow &= "|"
Next
_ArrayAdd($aListViewData, $sRow)
Next
; Array sortieren
_ArraySort($aListViewData, $sortOrder, $iCol)
; Liste löschen und neu befüllen
_GUICtrlListView_DeleteAllItems($hListView)
For $i = 0 To UBound($aListViewData) - 1
Local $aRow = StringSplit($aListViewData[$i], "|")
GUICtrlCreateListViewItem($aRow[1] & "|" & $aRow[2] & "|" & $aRow[3] & "|" & $aRow[4], $hListView)
Next
; Umschalten Sortierreihenfolge
If $lastSortedColumn = $iCol Then
$sortOrder = Not $sortOrder
Else
$sortOrder = True ; Start with ascending order for new column
EndIf
$lastSortedColumn = $iCol
EndFunc
Alles anzeigen
lg und eine schöne Woche noch