convert Route to shell commmand and format the output
This commit is contained in:
parent
9c20564afa
commit
924c46fdbf
|
@ -1,10 +1,36 @@
|
|||
package net.hyperic.sigar.cmd;
|
||||
|
||||
import net.hyperic.sigar.Sigar;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import net.hyperic.sigar.NetFlags;
|
||||
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) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
|
@ -17,26 +43,33 @@ public class Route {
|
|||
return sb.toString();
|
||||
}
|
||||
|
||||
public String getUsageShort() {
|
||||
return "Kernel IP routing table";
|
||||
}
|
||||
|
||||
//netstat -r
|
||||
public static void main(String[] args) throws Exception {
|
||||
Sigar sigar = new Sigar();
|
||||
public void output(String[] args) throws SigarException {
|
||||
NetRoute[] routes = this.sigar.getNetRouteList();
|
||||
|
||||
NetRoute[] routes = sigar.getNetRouteList();
|
||||
|
||||
System.out.println("Destination\tGateway\tGenmask" +
|
||||
"\tFlags\tMSS\tWindow\tirtt\tIface");
|
||||
printf(HEADER);
|
||||
|
||||
for (int i=0; i<routes.length; i++) {
|
||||
NetRoute route = routes[i];
|
||||
|
||||
System.out.println(route.getDestination() + "\t" +
|
||||
route.getGateway() + "\t" +
|
||||
route.getMask() + "\t" +
|
||||
flags(route.getFlags()) + "\t" +
|
||||
route.getMtu() + "\t" +
|
||||
route.getWindow() + "\t" +
|
||||
route.getIrtt() + "\t" +
|
||||
route.getIfname());
|
||||
ArrayList items = new ArrayList();
|
||||
items.add(route.getDestination());
|
||||
items.add(route.getGateway());
|
||||
items.add(route.getMask());
|
||||
items.add(flags(route.getFlags()));
|
||||
items.add(String.valueOf(route.getMetric()));
|
||||
items.add(String.valueOf(route.getRefcnt()));
|
||||
items.add(route.getIfname());
|
||||
|
||||
printf(items);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
new Route().processCommand(args);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -71,6 +71,7 @@ public class Shell extends ShellBase {
|
|||
registerCommandHandler("kill", new Kill(this));
|
||||
registerCommandHandler("netstat", new Netstat(this));
|
||||
registerCommandHandler("netinfo", new NetInfo(this));
|
||||
registerCommandHandler("route", new Route(this));
|
||||
registerCommandHandler("version", new Version(this));
|
||||
registerCommandHandler("mps", new MultiPs(this));
|
||||
registerCommandHandler("sysinfo", new SysInfo(this));
|
||||
|
|
Loading…
Reference in New Issue