convert Route to shell commmand and format the output

This commit is contained in:
Doug MacEachern 2005-07-13 00:17:24 +00:00
parent 9c20564afa
commit 924c46fdbf
2 changed files with 50 additions and 16 deletions

View File

@ -1,10 +1,36 @@
package net.hyperic.sigar.cmd; package net.hyperic.sigar.cmd;
import net.hyperic.sigar.Sigar; import java.util.ArrayList;
import net.hyperic.sigar.NetFlags; import net.hyperic.sigar.NetFlags;
import net.hyperic.sigar.NetRoute; import net.hyperic.sigar.NetRoute;
import net.hyperic.sigar.Sigar;
import net.hyperic.sigar.SigarException;
public class Route { public class Route extends SigarCommandBase {
private static final String OUTPUT_FORMAT =
"%-15s %-15s %-15s %-5s %-6s %-3s %-s";
//like df -h -a
private static final String[] HEADER = new String[] {
"Destination",
"Gateway",
"Genmask",
"Flags",
"Metric",
"Ref",
"Iface"
};
public Route(Shell shell) {
super(shell);
setOutputFormat(OUTPUT_FORMAT);
}
public Route() {
super();
setOutputFormat(OUTPUT_FORMAT);
}
private static String flags(long flags) { private static String flags(long flags) {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
@ -17,26 +43,33 @@ public class Route {
return sb.toString(); return sb.toString();
} }
public String getUsageShort() {
return "Kernel IP routing table";
}
//netstat -r //netstat -r
public static void main(String[] args) throws Exception { public void output(String[] args) throws SigarException {
Sigar sigar = new Sigar(); NetRoute[] routes = this.sigar.getNetRouteList();
NetRoute[] routes = sigar.getNetRouteList(); printf(HEADER);
System.out.println("Destination\tGateway\tGenmask" +
"\tFlags\tMSS\tWindow\tirtt\tIface");
for (int i=0; i<routes.length; i++) { for (int i=0; i<routes.length; i++) {
NetRoute route = routes[i]; NetRoute route = routes[i];
System.out.println(route.getDestination() + "\t" + ArrayList items = new ArrayList();
route.getGateway() + "\t" + items.add(route.getDestination());
route.getMask() + "\t" + items.add(route.getGateway());
flags(route.getFlags()) + "\t" + items.add(route.getMask());
route.getMtu() + "\t" + items.add(flags(route.getFlags()));
route.getWindow() + "\t" + items.add(String.valueOf(route.getMetric()));
route.getIrtt() + "\t" + items.add(String.valueOf(route.getRefcnt()));
route.getIfname()); items.add(route.getIfname());
printf(items);
} }
} }
public static void main(String[] args) throws Exception {
new Route().processCommand(args);
}
} }

View File

@ -71,6 +71,7 @@ public class Shell extends ShellBase {
registerCommandHandler("kill", new Kill(this)); registerCommandHandler("kill", new Kill(this));
registerCommandHandler("netstat", new Netstat(this)); registerCommandHandler("netstat", new Netstat(this));
registerCommandHandler("netinfo", new NetInfo(this)); registerCommandHandler("netinfo", new NetInfo(this));
registerCommandHandler("route", new Route(this));
registerCommandHandler("version", new Version(this)); registerCommandHandler("version", new Version(this));
registerCommandHandler("mps", new MultiPs(this)); registerCommandHandler("mps", new MultiPs(this));
registerCommandHandler("sysinfo", new SysInfo(this)); registerCommandHandler("sysinfo", new SysInfo(this));