Skip to content

Instantly share code, notes, and snippets.

@samsouder
Created December 17, 2009 17:56
Show Gist options
  • Save samsouder/258904 to your computer and use it in GitHub Desktop.
Save samsouder/258904 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -wKU
def ip_route_match(command, regex)
command.split("\n").select { |l| l =~ regex }.first.to_s[/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/] || '0.0.0.0'
end
c = '10.128.0.0/24 dev eth1 proto kernel scope link src 10.128.0.48
127.0.0.0/8 dev lo scope link
default via 10.128.0.254 dev eth1'
# c = %x{/sbin/ip route list match #{ip}}
p ip_route_match(c, /^default via /) # => '10.128.0.254'
p ip_route_match(c, /via /) # => '10.128.0.254'
p ip_route_match(c, /^default crap /) # => '0.0.0.0'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment