Last active
December 27, 2022 13:38
-
-
Save JohnLBevan/cfe31200b8fc5f0881d62188b9421823 to your computer and use it in GitHub Desktop.
Turn filezilla logs into something you can work with
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class FzLogEntry { | |
| [long]$SessionId | |
| [DateTime]$DateTime | |
| [string]$User | |
| [version]$ClientIp # I've used version assuming it's always IPv4... if that assumption's wrong we may have to amend to string | |
| [string]$Msg | |
| FzLogEntry(){} | |
| } | |
| Function Get-FzLogData { | |
| [OutputType('FzLogEntry[]')] | |
| [CmdletBinding()] | |
| Param ( | |
| [Parameter(Mandatory = $true, ValueFromPipeline = $true)] | |
| [string[]]$Path | |
| ) | |
| Process { | |
| foreach ($p in $Path) { | |
| Get-Content -Path $p | ConvertTo-FzLogEntry | |
| } | |
| } | |
| } | |
| Function ConvertTo-FzLogEntry { | |
| [OutputType('FzLogEntry')] | |
| [CmdletBinding(DefaultParameterSetName = 'Default')] | |
| Param ( | |
| [Parameter(ParameterSetName = 'Default', Mandatory = $true, ValueFromPipeline = $true)] | |
| [Parameter(ParameterSetName = 'Unparsable', Mandatory = $true, ValueFromPipeline = $true)] | |
| [string[]]$Line | |
| , | |
| [Parameter(ParameterSetName = 'Unparsable', Mandatory = $true)] # I don't really have a need for this; but it may be helpful if debugging to know when lines are not processed / have a gap in your results... | |
| [Switch]$IncludDefaultForUnparsable | |
| , | |
| [Parameter(ParameterSetName = 'Unparsable')] | |
| [FzLogEntry]$DefaultValue = $null | |
| ) | |
| Begin { | |
| [string]$regexPattern = @' | |
| ^\( | |
| (?<SessionId>\d+) | |
| \)\s | |
| (?<DateTime>\S+\s\S+(?:\s\S+)?) | |
| \s\-\s | |
| (?<User>(?:\([^\)]+\))|(?:\S+)) | |
| \s\( | |
| (?<ClientIp>[^>]+) | |
| \)>\s* | |
| (?<Msg>.*) | |
| $ | |
| '@ -replace '[\r\n]+', '' | |
| } | |
| Process { | |
| foreach ($l in $Line) { | |
| if ($l -match $regexPattern) { | |
| $fiddle = $Matches | |
| $fiddle.Remove(0) | |
| $fiddle.DateTime = $fiddle.DateTime -replace '^(\d+)\/(\d+)\/(\d+)', '$3-$2-$1' #correct date format to something that PS can auto convert to a DateTime. Assumes dd/mm/yyyy format in the logs | |
| [FzLogEntry]$fiddle | |
| } else { | |
| if ($IncludDefaultForUnparsable.IsPresent) { | |
| $DefaultValue | |
| } | |
| } | |
| } | |
| } | |
| } | |
| # Example usage | |
| $parsedLog = '\\myServer\c$\Program Files (x86)\FileZilla Server\Logs\fzs-2020-05-13.log' | Get-FzLogData | |
| $clientIps = @('172.123.45.67', '172.123.45.89') # got these valeus via: $parsedLog | ?{$_.User -eq 'AccountICareAbout'} | % ClientIP | sort -Unique | |
| $parsedLog | ?{$_.ClientIp -in $clientIps} | ft -autosize # filter on IPs rather than username to get full session info for each user (we could filter on sessionIds instead if we don't want the possiblility of multiple users from the same IP to contend with; it's all about requirements) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment