# Script Processor Conditional

**URL:** <https://discuss.elastic.co/t/script-processor-conditional/347004>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [November 13, 2023, 11:19am UTC](https://discuss.elastic.co/t/script-processor-conditional/347004 "2023-11-13T11:19:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![karnamonkster](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karnamonkster/32/67266_2.png) [@karnamonkster](https://discuss.elastic.co/u/karnamonkster)\
**Post date:** [November 13, 2023, 11:19am UTC](https://discuss.elastic.co/t/script-processor-conditional/347004/1 "2023-11-13T11:19:41Z")

</div>

Hi,

I have a field - **src-station-id** in the logs that brings in **IP** and **Hostnames** as string.  
Now in order to create a new field when src-station-id only contains IP, I am trying to get a script processor identify that as IP or String.

```auto
def fieldValue = ctx.src-station-id;
          if (fieldValue != null) {
            if (fieldValue instanceof String) {
              if (fieldValue =~ /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/) {
                ctx.sourcefieldType = "IP";
              } else {
                ctx.sourcefieldType = "String";
              }
            } else {
              ctx.sourcefieldType = "Not a String";
            }
          }

```

If _ctx.sourcefieldType = IP_ , src-station-id will be converted to **source.ip** as IP type once identified correctly.  
For some reason this script processor does not detect the IP correctly all the times. There must be a better way to handle it. Please suggest 🙂

---

<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:** [December 11, 2023, 11:20am UTC](https://discuss.elastic.co/t/script-processor-conditional/347004/2 "2023-12-11T11:20:01Z")

</div>

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