Extract String When Meeting a Specific Character Format (1 Viewer)

vBeh?

New member
Local time
Today, 17:42
Joined
Mar 11, 2022
Messages
5
Hi,

I'm trying to compile a list of all MAC addresses contained within an exported router logfile

Some MAC addresses are recorded in isolation within the DeviceID field.

However there are many more MAC addresses which appear in the "Action" field as part of logged event. IE

2.4G Client associate from AA:f3:61:5a:AA:01 (IP=192.168.1.198) RSSI=-55
ARP [add] br0(wl0) 192.168.1.125 AA:08:16:1a:AA:2a
LAN [ADD] ARP 192.168.1.111 with AA:42:12:aaAA96:b8 from br0(wl0)
WHW INFO A station STA(AA:08:16:1A:A1:AA) leave WHW infrastructure

There are far too many events and variations of those events (different formats and value placements within the strings for a simple LEFT / RIGHT approach

I was thinking along the lines of the inSTR using the colon as the static value in the function, and returning the two characters to either the left or right of the colon?

It's outside my experience however and was hoping someone could help please?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 00:42
Joined
May 7, 2009
Messages
19,230
use Regular expression:
only:
LAN [ADD] ARP 192.168.1.111 with AA:42:12:aaAA96:b8 from br0(wl0), did not pass the test:
Code:
'https://stackoverflow.com/questions/30761312/regex-matching-mac-address
Public Function fncExtractMAC(ByVal pString As String)
Dim var, ret As String
With CreateObject("vbscript.regexp")
    .Global = True
    .ignorecase = True
    .pattern = "([0-9a-fA-F]{2}[:]){5}([0-9a-fA-F]{2})"
    For Each var In .Execute(pString)
        ret = Trim$(var.Value)
        If Len(ret) Then
            fncExtractMAC = ret
            Exit For
        End If
    Next
End With
End Function
?fncExtractMAC("WHW INFO A station STA(AA:08:16:1A:A1:AA) leave WHW infrastructure")
AA:08:16:1A:A1:AA

?fncExtractMAC("ARP [add] br0(wl0) 192.168.1.125 AA:08:16:1a:AA:2a")
AA:08:16:1a:AA:2a

?fncExtractMAC("WHW INFO A station STA(AA:08:16:1A:A1:AA) leave WHW infrastructure")
AA:08:16:1A:A1:AA
 

vBeh?

New member
Local time
Today, 17:42
Joined
Mar 11, 2022
Messages
5
That's amazing, thank you so much!

I have a folder / document where I store all the scripts and codes which I've picked up from people such as your good self along the way. This is going in there!

If I wanted to produce a slightly edited version to get IP addresses in the same way, would this be



Public Function fncExtractIP(ByVal pString As String)
Dim var, ret As String
With CreateObject("vbscript.regexp")
.Global = True
.ignorecase = True

.pattern = "([0-9a-fA-F]{2}[.]){5}([0-9a-fA-F]{2})" **

**I beleive the values in {here} relate to the character space counts to move from for each [.]? If that's correct I'm not quite following the FA-F etc?

For Each var In .Execute(pString)
ret = Trim$(var.Value)
If Len(ret) Then
fncExtractIP = ret
Exit For
End If
Next
End With
End Function
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 00:42
Joined
May 7, 2009
Messages
19,230
try this, you have IPv4, there:
Code:
'arnelgp
Public Function fncExtractIP4(ByVal pString As String)
Dim var, ret As String
With CreateObject("vbscript.regexp")
    .Global = True
    .ignorecase = True
    .pattern = "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
    For Each var In .Execute(pString)
        ret = Trim$(var.Value)
        If Len(ret) Then
            fncExtractIP = ret
            Exit For
        End If
    Next
End With
End Function
 

vBeh?

New member
Local time
Today, 17:42
Joined
Mar 11, 2022
Messages
5
Amazing and thank you so much again. I'm sorry the reply took so long
 

Users who are viewing this thread

Top Bottom