Convert decimal IP to ipv4 dotted decimal

Hello,

I am reading data from an SQL table which contains IP addresses in decimal format, and I need to convert these to dotted decimal. e.g.

convert this: 1978211278
to this: 117.233.27.206

I am trying to do this with a Ruby filter using the ipaddr method:

		if [sourceipv4] {
			ruby{
				code => "require 'ipaddr'
						decimalip = event['sourceipv4']
						event['ip'] = IPAddr.new('decimalip',Socket::AF_INET).to_s()"
			}
		}

...where sourceipv4 is my decimal ip address, but this gives me this error:

{:timestamp=>"2016-12-05T14:41:55.019000-0800", :message=>"Ruby exception occurred: address family mismatch", :level=>:error}
{:timestamp=>"2016-12-05T14:41:55.020000-0800", :message=>"Ruby exception occurred: address family mismatch", :level=>:error}
{:timestamp=>"2016-12-05T14:41:55.020000-0800", :message=>"Ruby exception occurred: address family mismatch", :level=>:error

Can anyone assist please?

Thank you

Found the problem. I had some extra single quotes, and the integers were negative so I had to drop the minus sign.

This version works:

		if [sourceipv4] {
			ruby{
				code => "require 'ipaddr'
						decimalip = event['sourceipv4'].abs
						event['ip'] = IPAddr.new(decimalip,Socket::AF_INET).to_s"
			}
2 Likes

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