# How to check if host.ip in particular CIDR

**URL:** <https://discuss.elastic.co/t/how-to-check-if-host-ip-in-particular-cidr/298648>\
**Category:** Elasticsearch\
**Tags:** ingest-pipeline\
**Created:** [March 2, 2022, 4:39pm UTC](https://discuss.elastic.co/t/how-to-check-if-host-ip-in-particular-cidr/298648 "2022-03-02T16:39:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adriann](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/adriann/32/77780_2.png) [@Adriann](https://discuss.elastic.co/u/Adriann)\
**Post date:** [March 2, 2022, 4:39pm UTC](https://discuss.elastic.co/t/how-to-check-if-host-ip-in-particular-cidr/298648/1 "2022-03-02T16:39:18Z")

</div>

Hello,

I want to add an ingest pipeline script that will add particular fields if host.ip falls into the CIDR notation range.

I was trying some stuff but still, that does not work  
The script does not return errors but fields are not added.

```auto
CIDR block = new CIDR("10.10.0.0/21"); # or def block = new CIDR("10.10.0.0/21")
for (int i = 0; i < ctx.host?.ip?.length; ++i) {
    if (block.contains(ctx["host.ip"])) {
        ctx["host.bo"] = "Value1";
        ctx["host.bo_name"] = "Value2";
    }
}

```

If you can spot the error and guide me It would be very helpful.

Stack is on 7.16-2

---

<div class="post-metadata">

**Author:** ![Adriann](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/adriann/32/77780_2.png) [@Adriann](https://discuss.elastic.co/u/Adriann)\
**Post date:** [March 4, 2022, 2:27pm UTC](https://discuss.elastic.co/t/how-to-check-if-host-ip-in-particular-cidr/298648/2 "2022-03-04T14:27:46Z")

</div>

This started to work. I was not able to manage to add a field via ctx.\_source... I used a set before that processor instead.

```auto
CIDR block = new CIDR("10.10.0.0/20");
for (int i = 0; i < ctx?.host?.ip.length; ++i) {
    if (block.contains(ctx.host.ip[i])) {
        ctx?.host.bo_name = "Value";
    }
}

```

Now I just wonder what would be the performance impact of using runtime field instead of ingest pipeline script processor.

---

<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:** [April 1, 2022, 2:27pm UTC](https://discuss.elastic.co/t/how-to-check-if-host-ip-in-particular-cidr/298648/3 "2022-04-01T14:27:52Z")

</div>

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