From 118a3e71fa2d98688058f63f16c6898f2fe9f4fe Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Mon, 29 Jun 2009 17:21:03 -0700 Subject: [PATCH] Ruby example of route cmd --- bindings/ruby/examples/route.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 bindings/ruby/examples/route.rb diff --git a/bindings/ruby/examples/route.rb b/bindings/ruby/examples/route.rb new file mode 100644 index 00000000..5978b7cd --- /dev/null +++ b/bindings/ruby/examples/route.rb @@ -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