# IP match failed

**URL:** <https://discuss.elastic.co/t/ip-match-failed/342475>\
**Category:** Logstash\
**Created:** [September 6, 2023, 9:03pm UTC](https://discuss.elastic.co/t/ip-match-failed/342475 "2023-09-06T21:03:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![javierelastic](https://avatars.discourse-cdn.com/v4/letter/j/919ad9/32.png) [@javierelastic](https://discuss.elastic.co/u/javierelastic)\
**Post date:** [September 6, 2023, 9:03pm UTC](https://discuss.elastic.co/t/ip-match-failed/342475/1 "2023-09-06T21:03:09Z")

</div>

Hi everyone!  
Something strange happens to me.  
I'm trying to see if a source ip matches a pattern I indicate. The ip is 100.44.1.128 and it tells me that it matches "^10.\*"  
How is it possible?

```auto
if [IPorigen] =~ "^10.*" or [IPorigen] =~ "^127.0.*" or [IPorigen] == "0.0.0.0" or [IPorigen] =~ "^192.168.*" { 
			mutate {
				add_tag => ["IP interna"]
			}
		}

```

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/e/9/e9f920bf80269f1119bcd672e8b43995ecef6dfd.png)

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [September 6, 2023, 9:34pm UTC](https://discuss.elastic.co/t/ip-match-failed/342475/2 "2023-09-06T21:34:57Z")

</div>

> [@javierelastic](#):
>
> The ip is 100.44.1.128 and it tells me that it matches "^10.\*"

Because it is a regex, and the `10.*` will match anything that starts with `10`, so it will match `10`, `100`, `101` etc.

Try to escape the `.`, using `^10\.*`

If it helps, I use the following filters to tag internal and external IPs for the field `source.ip`, you can adapt to your use case.

```auto
filter {
    if [source][ip] and ":" not in [source][ip] {
        mutate {
            add_field => {
                "[@metadata][srcIP]" => "%{[source][ip]}"
            }
        }
        mutate {
            convert => { "[@metadata][srcIP]" => "string" }
        }
        mutate {
            split => { "[@metadata][srcIP]" => "." }
        }
    }
    if [@metadata][srcIP] {
        if [@metadata][srcIP][0] in ["0","10","127"] {
            mutate {
                add_tag => ["internal-ip"]
            }
        } else if [@metadata][srcIP][0] == "192" and [@metadata][srcIP][1] == "168" {
            mutate {
                add_tag => ["internal-ip"]
            }
        } else if [@metadata][srcIP][0] == "169" and [@metadata][srcIP][1] == "254" {
            mutate {
                add_tag => ["internal-ip"]
            }
        } else if [@metadata][srcIP][0] == "172" and [@metadata][srcIP][1] in ["16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"] {
            mutate {
                add_tag => ["internal-ip"]
            }
        } else {
            mutate {
                add_tag=> ["external-ip"]
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [September 6, 2023, 9:52pm UTC](https://discuss.elastic.co/t/ip-match-failed/342475/3 "2023-09-06T21:52:22Z")

</div>

You might find the [cidr](https://www.elastic.co/guide/en/logstash/current/plugins-filters-cidr.html) filter useful.

---

<div class="post-metadata">

**Author:** ![javierelastic](https://avatars.discourse-cdn.com/v4/letter/j/919ad9/32.png) [@javierelastic](https://discuss.elastic.co/u/javierelastic)\
**Post date:** [September 7, 2023, 10:05am UTC](https://discuss.elastic.co/t/ip-match-failed/342475/5 "2023-09-07T10:05:25Z")

</div>

Thanks for the explanation and your code suggestion. 10\ does not work correctly. It detects "10" in other parts of IP address and the result is even worse

---

<div class="post-metadata">

**Author:** ![javierelastic](https://avatars.discourse-cdn.com/v4/letter/j/919ad9/32.png) [@javierelastic](https://discuss.elastic.co/u/javierelastic)\
**Post date:** [September 7, 2023, 11:27am UTC](https://discuss.elastic.co/t/ip-match-failed/342475/6 "2023-09-07T11:27:38Z")

</div>

thanks for the advice.  
I have applied the cidr filter and it is easier to make it work for my case.

```auto
cidr{
			address => ["%{IPorigen}"]
			network => ["10.0.0.0/8", "127.0.0.0/8", "0.0.0.0/32", "192.168.0.0/16"]
			add_tag => ["IP interna"]
		}

```

---

<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:** [October 5, 2023, 11:27am UTC](https://discuss.elastic.co/t/ip-match-failed/342475/7 "2023-10-05T11:27:57Z")

</div>

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