Skip to content


hello Ruby, goodbye perl, awk…

Ruby has been around for a while, I've been meaning to look at this for ages and when Jon Tirsen mentioned DamageControl is written in it, it was yet another push for me to have a squiz.

I had a superficial look mainly from a scripting point of view and it looks great, I might never have to write another awk script again!

Here is the socketcount program from a recent post rewritten in ruby.

#!/usr/bin/ruby
#socketcount in ruby
if $*.size > 0
regex = $*[0]
else
regex = '^..p'
end
result = %x{sudo netstat -tpa | grep #{regex} | grep -v LISTEN }
# All Socket connections counted (originating and terminating)
h = {}
result.each() { |s|
service = s.split(' ')[4]
h[service] = h[service].to_i + 1
}
h.each { | key, value | print key, " ",value, "n" }

It suits my purposes for scripting and seems to be pretty impressive in other aspects.

Post to Twitter Tweet This Post

Posted in Uncategorized. Tagged with , .

One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. I agree with you. I have been a die-hard addict of awk/sed/grep. Its hard to not use them. However lately ive been forcing myself to look at using ruby for this. I know it is good for this type work. Thanks for the example.

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.


Twitter links powered by Tweet This v1.6.1, a WordPress plugin for Twitter.