Splunk Search

How to extract an IP address across different pattern of events?

luca1
New Member

I'm trying to extract IP (v4) addresses from different events. For instance, for an event such as:

[...] sent ping to 1.1.1.1:514 [...]

this rex command works just fine:

| rex field=_raw " (?<node_ip_1>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):"

and for events such as:

[...] paired node xxxxxx@2.2.2.2 [...]

this slightly changed regex works as well:

rex field=_raw"@(?<node_ip_2>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"

And so on. 🙂 The problem is that these IP addresses are located in different places according to the type of events, so one rex command does not fit all patterns. Field extraction wizard for regular expressions also gets confused and only gets it right in some cases, but mostly gets it wrong.

Is there a way to tell rex to match pattern_1 OR pattern2 OR pattern3 and so on?

Or am I looking at this the wrong way?

0 Karma

luca1
New Member

Just to clarify, I have 3 types of messages - by which I mean the IP address is surrounded by different characters:

(the underscore char represents a space):

  1. _HOST-IP-ADDRESS_ ( <- not interested in extracting this)

  2. _IP-ADDRESS:

  3. @IP-ADDRESS:

  4. _IP_ADDRESS_/

Thus far I created 3 separate searches for which rex works just fine. Can I reduce it to one search only.
Basically, a way to tell rex: if IP_ADDRESS is preceded by space OR @ AND followed by colon OR space/

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi luca1,
did you symply tried with (?<IP>\d+\.\d+\.\d+\.\d+) ?
You can test it at https://regex101.com/r/uUg67f/1
It extract your IP addresses from each log.

If yoy want also extract the port (when present), you can add another different extraction \d+\.\d+\.\d+\.\d+:(?<port>\d+) .

Bye.
Giuseppe

luca1
New Member

Hi Giuseppe,
I forgot to mention that the host IP is also in the events, so I need a way to differentiate it from the (source) IP I'm after; hence the leading space or "@" characters as needed, I think. 🙂 Is there any way to match several leading / trailing characters in regex, something like [\s|@|:] just an example.

Luca

0 Karma
Get Updates on the Splunk Community!

Exciting News: The AppDynamics Community Joins Splunk!

Hello Splunkers,   I’d like to introduce myself—I’m Ryan, the former AppDynamics Community Manager, and I’m ...

The All New Performance Insights for Splunk

Splunk gives you amazing tools to analyze system data and make business-critical decisions, react to issues, ...

Good Sourcetype Naming

When it comes to getting data in, one of the earliest decisions made is what to use as a sourcetype. Often, ...