Splunk Search

Calculate network address

Nico99
Explorer

Hello everyone

I want to calculate the network address from an IP and a mask:

IP = 192.168.1.10
Mask = 255.255.255.0

Desired result = 192.168.1.0

Unfortunately I can't find a function or method to do this.

I looked for the 'cidrmatch' function but it only seems to return a boolean. Is there another way?

Thanks for your help!

0 Karma
1 Solution

yuanliu
SplunkTrust
SplunkTrust

If you are willing to accept some acrobat, ipmask can be used even with variable net masks.

 

| map search="| makeresults |fields - _*
  | eval Network = ipmask(\"$Mask$\", $IP$), IP = $IP$, Mask = $Mask$"

 

Emulated data below should give

IPMaskNetwork
192.168.1.10255.255.255.0192.168.1.0
10.54.3.8255.255.246.010.54.2.0

Here is the emulation for you to play with and compare with real data

 

| makeresults format=csv data="IP, Mask
192.168.1.10, 255.255.255.0
10.54.3.8, 255.255.246.0"

 

But again, to say 192.168.1.0 is a network address is (very) classism.  The CIDR expressions should be

IPMaskNetwork
192.168.1.10255.255.255.0192.168.1.0/24
10.54.3.8255.255.248.010.54.0.0/21

N'est-ce pas?  This can be obtained with a bit of bit math, like this:

 

| map search="| makeresults |fields - _*
  | eval Mask = split($Mask$, \".\"), Mask = 32 - sum(mvmap(Mask, log(256 - Mask,2))),
  Network = ipmask(\"$Mask$\", $IP$) . \"/\" . Mask, IP = $IP$, Mask = $Mask$"

 

 

View solution in original post

Tags (1)
0 Karma

yuanliu
SplunkTrust
SplunkTrust

If you are willing to accept some acrobat, ipmask can be used even with variable net masks.

 

| map search="| makeresults |fields - _*
  | eval Network = ipmask(\"$Mask$\", $IP$), IP = $IP$, Mask = $Mask$"

 

Emulated data below should give

IPMaskNetwork
192.168.1.10255.255.255.0192.168.1.0
10.54.3.8255.255.246.010.54.2.0

Here is the emulation for you to play with and compare with real data

 

| makeresults format=csv data="IP, Mask
192.168.1.10, 255.255.255.0
10.54.3.8, 255.255.246.0"

 

But again, to say 192.168.1.0 is a network address is (very) classism.  The CIDR expressions should be

IPMaskNetwork
192.168.1.10255.255.255.0192.168.1.0/24
10.54.3.8255.255.248.010.54.0.0/21

N'est-ce pas?  This can be obtained with a bit of bit math, like this:

 

| map search="| makeresults |fields - _*
  | eval Mask = split($Mask$, \".\"), Mask = 32 - sum(mvmap(Mask, log(256 - Mask,2))),
  Network = ipmask(\"$Mask$\", $IP$) . \"/\" . Mask, IP = $IP$, Mask = $Mask$"

 

 

Tags (1)
0 Karma

Nico99
Explorer

Good morning,

Thank you for the feedback.

Unfortunately the netmask is not fixed...

I'll try with the app
https://splunkbase.splunk.com/app/6595 

 

0 Karma

Nico99
Explorer

Hi yuanliu

Thank you for the feedback.

It's perfect! 🙂

 

 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

If your netmask is fixed, you can use the ipmask function

 

| eval result=ipmask("255.255.255.0", IP)

 

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...