# Convert IP string to Integer

**URL:** https://discuss.elastic.co/t/convert-ip-string-to-integer/290351
**Category:** Logstash
**Created:** [November 28, 2021, 6:02am UTC](https://discuss.elastic.co/t/convert-ip-string-to-integer/290351 "2021-11-28T06:02:49Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![rojin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rojin/32/97697_2.png) [@rojin](https://discuss.elastic.co/u/rojin)
#### Post date: [November 28, 2021, 6:02am UTC](https://discuss.elastic.co/t/convert-ip-string-to-integer/290351/1 "2021-11-28T06:02:49Z")

</div>

Hi!, Is there a way to convert an IP address to an integer in logstash filters?  
Suppose I have the grok pattern:

```auto
%{IPv4: clientip} - %{DATA:name}....

```

I want to have the equivalent byte-like integer of the IP address to be able to use JDBC query to fetch something from my MySQL table. I want to compare this integer with 2 columns and check if the IP is in the range of those values.  
Is that possible?

---

<div class="post-metadata">

### Author: ![AquaX](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aquax/32/92006_2.png) [@AquaX](https://discuss.elastic.co/u/AquaX)
#### Post date: [December 1, 2021, 7:58pm UTC](https://discuss.elastic.co/t/convert-ip-string-to-integer/290351/2 "2021-12-01T19:58:23Z")

</div>

If you want to pick apart an IP address then create another grok pattern for that field.

```auto
grok {
match => { "clientip" => "%{INT:octet1}\.%{INT:octet2}\.%{INT:octet3}\.%{INT:octet4}"
}

```

```auto
{
  "octet1": [
    [
      "192"
    ]
  ],
  "octet2": [
    [
      "168"
    ]
  ],
  "octet3": [
    [
      "1"
    ]
  ],
  "octet4": [
    [
      "1"
    ]
  ]
}

```

You may need to then use a mutate filter to convert those fields into integers.

---

<div class="post-metadata">

### Author: ![rojin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rojin/32/97697_2.png) [@rojin](https://discuss.elastic.co/u/rojin)
#### Post date: [December 11, 2021, 6:33am UTC](https://discuss.elastic.co/t/convert-ip-string-to-integer/290351/3 "2021-12-11T06:33:18Z")

</div>

Actually, I needed the whole integer value by using the formula; For instance (192 \* 256^3 + 168 \* 256^2 + ...) But thanks for the reply anyway 🙂

---

<div class="post-metadata">

### Author: ![AquaX](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aquax/32/92006_2.png) [@AquaX](https://discuss.elastic.co/u/AquaX)
#### Post date: [December 13, 2021, 2:48pm UTC](https://discuss.elastic.co/t/convert-ip-string-to-integer/290351/4 "2021-12-13T14:48:18Z")

</div>

Ok! That’s easy to do with a ruby script once you’ve picked the numbers apart.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [January 10, 2022, 2:49pm UTC](https://discuss.elastic.co/t/convert-ip-string-to-integer/290351/5 "2022-01-10T14:49:13Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
