1

I'm curious if there's a simple way to convert a bash command into a ruby script.

If you type this in your command line, you'll get a list of ip networks.

whois -h whois.radb.net -- '-i origin AS35995' | grep -Eo "([0-9.]+){4}/[0-9]+"

Is there an easy way to do something similar in Ruby, or do I have to manually code that?

Sorry for the noob question and thanks in advance for any help!

Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
CottonEyeJoe
  • 720
  • 1
  • 7
  • 28

2 Answers2

5

You could leave the command as is and use @AndreyDeineko's valid solution.

You could execute the grep command inside Ruby with String#scan:

abc   = 'AS35995'
whois = `whois -h whois.radb.net -- '-i origin #{abc}'`
# route:      8.25.194.0/23
# descr:      Twitter
# origin:     AS35995
# admin-c:    NETWO3685-ARIN
# tech-c:     NETWO3685-ARIN
# notify:     noc@twitter.com
# mnt-by:     MAINT-AS13414
# changed:    ck@twitter.com 20121028  #17:03:09Z
# source:     RADB

# route:      8.25.196.0/23
# descr:      Twitter
# origin:     AS35995
# ....

p routes = whois.scan(/[\d\.]{7,}\/\d+/)
#=> ["8.25.194.0/23", "8.25.196.0/23", "192.133.78.0/23", "8.25.194.0/24", "8.25.195.0/24", "8.25.196.0/24", "8.25.197.0/24", "185.45.4.0/24", "103.252.112.0/23", "185.45.4.0/23"]

This regex is simple but might match too much. From this link, it looks like a more accurate regex would be :

/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/\d+\b/

Route is now an array of strings, containing the route values from whois.

Finally, it seems you could also execute whois with this gem.

Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
  • nice! thanks! This is exactly what i was looking for! – CottonEyeJoe Jan 30 '17 at 13:38
  • 1
    as usually - perfect, involved answer. +1 to gold ;) – Andrey Deineko Jan 30 '17 at 13:45
  • @AndreyDeineko : Thanks! It has been a good month. My next goal is to be ahead of JonSkeet in [January](http://stackexchange.com/leagues/1/month/stackoverflow/2017-01-01/6419007#6419007) 1 day to go! :) – Eric Duminil Jan 30 '17 at 13:47
  • Still, I need to answer many questions to get enough points. He has accumulated more than 30000 questions, and he gets 200 points a day even if he's gone fishing. – Eric Duminil Jan 30 '17 at 13:51
  • 1
    Just a quick add for those who want to handle that dynamically: @abc = "AS35995" whois = `whois -h whois.radb.net -- "-i origin #{@abc}"` – CottonEyeJoe Jan 30 '17 at 14:13
2

Well, you have quite a few options.

Here is a backticks option (symply surround any bash command with backticks):

puts `whois -h whois.radb.net -- '-i origin AS35995' | grep -Eo "([0-9.]+){4}/[0-9]+"`
# 8.25.194.0/23
# 8.25.196.0/23
# 192.133.78.0/23
# 8.25.194.0/24
# 8.25.195.0/24
# 8.25.196.0/24
# 8.25.197.0/24
# 185.45.4.0/24
# 103.252.112.0/23
# 185.45.4.0/23

system option:

system("whois -h whois.radb.net -- '-i origin AS35995' | grep -Eo '([0-9.]+){4}/[0-9]+'")
# 8.25.194.0/23
# 8.25.196.0/23
# 192.133.78.0/23
# 8.25.194.0/24
# 8.25.195.0/24
# 8.25.196.0/24
# 8.25.197.0/24
# 185.45.4.0/24
# 103.252.112.0/23
# 185.45.4.0/23
Community
  • 1
  • 1
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145