private static List <Route> GetRoutes(VyattaShell Shell) { string ShowRoutes = Shell.RunCommand("sudo route -n"); string[] RouteLines = ShowRoutes.Split(new char[] { '\n' }); List <Route> Routes = new List <Route>(); foreach (string Line in RouteLines) { Match Match = GatewayRegex.Match(Line); if (Match.Success) { Route Route = new Route(); Route.Destination = Match.Groups[1].Value; Route.Gateway = Match.Groups[2].Value; Route.Genmask = Match.Groups[3].Value; Route.Interface = Match.Groups[4].Value; Routes.Add(Route); } } return(Routes); }
public static bool DeleteRoutes(VyattaShell Shell, List <Route> Routes) { foreach (Route Route in Routes) { string Result = Shell.RunCommand(string.Format("ip route del {0} via {1} dev {2}", Route.CalculatedNetmask.Value, Route.Gateway, Route.Interface)); //TODO: Check Result } return(true); }
public static List <Route> GetRoutesForInterface(VyattaShell Shell, bool DefaultOnly, string Interface = null) { var Routes = GetRoutes(Shell); List <Route> NewRoutes = new List <Route>(); foreach (Route Route in Routes) { //Only match gateways to the internet if ((Interface == null || Route.Interface == Interface) && (!DefaultOnly || Route.Gateway != "0.0.0.0")) { if (!DefaultOnly || (Route.Genmask == "0.0.0.0" || Route.Genmask == "128.0.0.0")) { NewRoutes.Add(Route); } } } return(NewRoutes); }
public static Dictionary <string, string> GetDefaultGateways(VyattaShell Shell) { var Routes = GetRoutes(Shell); Dictionary <string, string> Gateways = new Dictionary <string, string>(); foreach (Route Route in Routes) { //Only match gateways to the internet if ((Route.Destination == "default" || Route.Destination == "0.0.0.0") && Route.Gateway != "0.0.0.0") { if (Route.Genmask == "0.0.0.0" || Route.Genmask == "128.0.0.0") { Gateways[Route.Interface] = Route.Gateway; } } } return(Gateways); }