private void ParseWithChildren(CiscoCommand command, CiscoCommand prevCommand) { if (command.Children == null || !command.Children.Any()) { command.Parse(command, prevCommand, _ciscoIds, _ciscoAliases); if (!string.IsNullOrEmpty(command.CiscoId) && !_ciscoIds.ContainsKey(command.CiscoId)) { _ciscoIds.Add(command.CiscoId, command); } return; } CiscoCommand prevChild = null; foreach (CiscoCommand child in command.Children) { ParseWithChildren(child, prevChild); prevChild = child; } command.Parse(command, prevCommand, _ciscoIds, _ciscoAliases); if (!string.IsNullOrEmpty(command.CiscoId) && !_ciscoIds.ContainsKey(command.CiscoId)) { _ciscoIds.Add(command.CiscoId, command); } }
private CiscoCommand FindCommand(CiscoCommand command) { string[] irrelevantCommands = { "!", ":", "speed", "dns-guard", "domain-name", "duplex", "passwd", "banner", "boot", "dns", "failover", "asdm", "arp", "clock", "mtu", "timeout" }; if (irrelevantCommands.Contains(command.FirstWord)) { command.NotAnInterestingCommand = true; } var ciscoCommandTypes = Assembly.GetExecutingAssembly().GetTypes().Where(commandType => commandType.GetInterfaces().Contains(typeof(ICiscoCommand))); foreach (Type commandType in ciscoCommandTypes) { object knownCommand = Activator.CreateInstance(commandType); string knownCommandName = (string)knownCommand.GetType().GetMethod("Name").Invoke(knownCommand, null); if (knownCommandName == command.FirstWord) { ((CiscoCommand)knownCommand).CiscoId = command.CiscoId; ((CiscoCommand)knownCommand).Id = command.Id; ((CiscoCommand)knownCommand).Text = command.Text; ((CiscoCommand)knownCommand).ParentId = command.ParentId; ((CiscoCommand)knownCommand).KnownCommand = true; ((CiscoCommand)knownCommand).NotAnInterestingCommand = false; return((CiscoCommand)knownCommand); } } command.KnownCommand = false; return(command); }
private static void AddChildren(CiscoCommand node, IDictionary <int, List <CiscoCommand> > source) { if (source.ContainsKey(node.Id)) { node.Children = source[node.Id]; for (int i = 0; i < node.Children.Count; i++) { AddChildren(node.Children[i], source); } } else { node.Children = new List <CiscoCommand>(); } }
private void ParseCommands(string filename) { string[] lines = File.ReadAllLines(filename); ParsedLines = lines.Count(); var parents = new Stack <Indentation>(); var flatList = new List <CiscoCommand>(); parents.Push(new Indentation(null, 0)); int prevIndentationLevel = 0; int lineId = 0; foreach (string line in lines) { lineId++; // Check for an empty line or line with just spaces. if (line.Trim().Length == 0) { continue; } // Check for weird stuff if (line.StartsWith("#") || line.StartsWith("<-")) { continue; } var command = new CiscoCommand { Id = lineId, Text = line }; int indentationChange = command.IndentationLevel - prevIndentationLevel; if (indentationChange > 0) { parents.Push(new Indentation(flatList.Last().Id, flatList.Last().IndentationLevel)); } else if (indentationChange < 0 && parents.Count > 0) { parents.Pop(); while ((parents.Count > 0) && (parents.Peek().Spaces > command.IndentationLevel)) { parents.Pop(); } } command.ParentId = (parents.Count > 0) ? parents.Peek().Id : null; prevIndentationLevel = command.IndentationLevel; flatList.Add(FindCommand(command)); } _ciscoCommands = flatList.BuildTree(); CiscoCommand prevCommand = null; foreach (CiscoCommand command in _ciscoCommands) { ParseWithChildren(command, prevCommand); prevCommand = command; } // Remove duplicates foreach (var ciscoId in _ciscoIds) { if (_ciscoAliases.ContainsKey(ciscoId.Key)) { _ciscoAliases.Remove(ciscoId.Key); } } }
public void Parse(string filename) { string[] lines = File.ReadAllLines(filename); _lineCount = lines.Count(); var parents = new Stack <Indentation>(); var flatList = new List <CiscoCommand>(); parents.Push(new Indentation(null, 0)); int prevIndentationLevel = 0; int lineId = 0; foreach (string line in lines) { lineId++; // Check for an empty line or line with just spaces. if (line.Trim().Length == 0) { continue; } // Check for weird stuff if (line.StartsWith("#") || line.StartsWith("<-")) { continue; } var command = new CiscoCommand { Id = lineId, Text = line }; int indentationChange = command.IndentationLevel - prevIndentationLevel; if (indentationChange > 0) { parents.Push(new Indentation(flatList.Last().Id, flatList.Last().IndentationLevel)); } else if (indentationChange < 0 && parents.Count > 0) { parents.Pop(); while ((parents.Count > 0) && (parents.Peek().Spaces > command.IndentationLevel)) { parents.Pop(); } } command.ParentId = (parents.Count > 0) ? parents.Peek().Id : null; prevIndentationLevel = command.IndentationLevel; flatList.Add(findCommand(command)); } _ciscoCommands = flatList.BuildTree(); CiscoCommand prevCommand = null; foreach (CiscoCommand command in _ciscoCommands) { ParseWithChildren(command, prevCommand); prevCommand = command; } // Remove duplicates foreach (var ciscoId in _ciscoIds) { if (_ciscoAliases.ContainsKey(ciscoId.Key)) { _ciscoAliases.Remove(ciscoId.Key); } } // Add related routing information to interface topology IEnumerable <CiscoCommand> ciscoInterfaceCommands = Filter("interface"); IEnumerable <CiscoCommand> ciscoRouteCommands = Filter("route"); foreach (Cisco_Interface ciscoInterface in ciscoInterfaceCommands) { if (!string.IsNullOrEmpty(ciscoInterface.CiscoId)) { foreach (Cisco_Route route in ciscoRouteCommands) { string routeInterfaceName = CiscoCommand.InterfacePrefix + route.InterfaceName; if (routeInterfaceName == ciscoInterface.CiscoId) { ciscoInterface.Topology.Add(new Cisco_Interface.Subnet(route.DestinationIp, route.DestinationNetmask)); if (route.DefaultRoute) { ciscoInterface.LeadsToInternet = true; } if (route.ConversionIncidentType != ConversionIncidentType.None) { ciscoInterface.ConversionIncidentType = route.ConversionIncidentType; ciscoInterface.ConversionIncidentMessage = route.ConversionIncidentMessage; } } } } } // Add version foreach (Cisco_ASA asa in Filter("ASA")) { _version = asa.Version; } }
private void ParseCommands(string filename) { string[] lines = File.ReadAllLines(filename, Encoding.GetEncoding("us-ascii", new EncoderReplacementFallback(""), new DecoderReplacementFallback(""))); ParsedLines = lines.Count(); var parents = new Stack <Indentation>(); var flatList = new List <CiscoCommand>(); parents.Push(new Indentation(null, 0)); int prevIndentationLevel = 0; int lineId = 0; foreach (string line in lines) { lineId++; // Check for an empty line or line with just spaces. if (line.Trim().Length == 0) { continue; } // Check for weird stuff if (line.StartsWith("#") || line.StartsWith("<-")) { continue; } var text = line; if ((!text.Contains("no nameif") && text.Contains("nameif")) || text.Contains("ip verify reverse-path interface") || text.Contains("mtu")) { text = System.Text.RegularExpressions.Regex.Replace(text, @"[()#/@;:<>{}`+=~|!?,]", ""); } var command = new CiscoCommand { Id = lineId, Text = text }; int indentationChange = command.IndentationLevel - prevIndentationLevel; if (indentationChange > 0) { parents.Push(new Indentation(flatList.Last().Id, flatList.Last().IndentationLevel)); } else if (indentationChange < 0 && parents.Count > 0) { parents.Pop(); while ((parents.Count > 0) && (parents.Peek().Spaces > command.IndentationLevel)) { parents.Pop(); } } command.ParentId = (parents.Count > 0) ? parents.Peek().Id : null; prevIndentationLevel = command.IndentationLevel; flatList.Add(FindCommand(command)); } _ciscoCommands = flatList.BuildTree(); CiscoCommand prevCommand = null; foreach (CiscoCommand command in _ciscoCommands) { ParseWithChildren(command, prevCommand); prevCommand = command; } // Remove duplicates foreach (var ciscoId in _ciscoIds) { if (_ciscoAliases.ContainsKey(ciscoId.Key)) { _ciscoAliases.Remove(ciscoId.Key); } } }