Ruby example of route cmd

This commit is contained in:
Doug MacEachern 2009-06-29 17:21:03 -07:00
parent 38e2e85132
commit 118a3e71fa
1 changed files with 31 additions and 0 deletions

View File

@ -0,0 +1,31 @@
require 'rbsigar'
def flags(flags)
f = ""
if (flags & Sigar::RTF_UP) != 0
f += "U"
end
if (flags & Sigar::RTF_GATEWAY) != 0
f += "G"
end
if (flags & Sigar::RTF_HOST) != 0
f += "H"
end
f
end
def gw(addr)
addr == "0.0.0.0" ? "*" : addr
end
def dest(addr)
addr == "0.0.0.0" ? "default" : addr
end
puts "Kernel IP routing table"
fmt = "%-15s %-15s %-15s %-5s %-6s %-3s %-s\n"
printf fmt, "Destination", "Gateway", "Genmask", "Flags", "Metric", "Ref", "Iface"
Sigar.new.net_route_list.each do |route|
printf fmt, dest(route.destination), gw(route.gateway), route.mask,
flags(route.flags), route.metric.to_s, route.refcnt.to_s, route.ifname
end