Getting Data In

Reverse-order a DNS at search time

ahamilton
New Member

Hi all,

In certain search, Splunk returns DNS hostnames, for example:
a.monetate.net.akadns.net
evsecure-ocsp.verisign.com

To facilitate sorting, I'd like to have the names displayed in reverse order across the dots, e.g.:
net.akadns.net.monetate.a
com.verisign.evsecure-ocsp

I've found some examples that suggest how to change word order but Splunk rejects it during search:

sourcetype=dns | rex field=domain mode=sed "s/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//"

Yields: Error in 'rex' command: Failed to initialize sed. Invalid option string: (.)(.*\n)/&\2\1/;//D;s/.//

Ideas?

folkstalk [dot] com/2011/12/methods-to-reverse-string-using-unix.html
programmingforums [dot] org/post188712.html

Tags (2)
0 Karma

matthewg
Explorer

Starting with a field: domain, you can do the following:

| eval domain_reversed=split(domain,".")
| eval domain_reversed=mvreverse(domain_reversed)
| eval domain_reversed=mvjoin(domain_reversed,".")
| sort domain_reversed

This splits the field into a multivalued field called domain_reversed with the values separated by the dot, then reverses the order of the resulting multivalued field, then joins the results back together and sorts on the resulting field.

 

0 Karma

yplambert
New Member

I've not written it yet, but my plan to handle this is to write a dynamic lookup that does the equivalent of this (in perl):

$sortable_name=$hostname;
$sortable_name=~s{(\d+)}{sprintf "%09d", $1}g;
$sortable_name=join('.', reverse(split(/\./, $sortable_name)));
return $sortable_name;

So given a field with the hostname, it'd return a new field with a "sortable name." You could sort on that field, and then remove it from the results.

0 Karma
Get Updates on the Splunk Community!

Preparing your Splunk Environment for OpenSSL3

The Splunk platform will transition to OpenSSL version 3 in a future release. Actions are required to prepare ...

Deprecation of Splunk Observability Kubernetes “Classic Navigator” UI starting ...

Access to Splunk Observability Kubernetes “Classic Navigator” UI will no longer be available starting January ...

Now Available: Cisco Talos Threat Intelligence Integrations for Splunk Security Cloud ...

At .conf24, we shared that we were in the process of integrating Cisco Talos threat intelligence into Splunk ...