tags:

views:

254

answers:

1

My router supports telnet sessions to configure my router, I want to make an application in c# that parses the console output into something useful.

edit: the lines are seperated with "\n\r" and there are no \t characters used, everything is spaced out

Example:

 bridge configuration for "bridge" :
OBC       : dest : Internal
            Connection State: connected   Retry: 10
            Priority Tagging: Disabled
            Port: OBC            PortNr: 0          PortState: forwarding  Interface: up
            Multicast filter: disabled              Dynamic VLAN    : disabled
            IGMP snooping   : enabled
            VLAN: Default VLAN: default  Ingressfiltering: disabled  Acceptvlanonly: disabled
            VLAN: Priority: disabled  IP Prec: disabled  Priority: 0  Regeneration table: 0 1 2 3 4 5 6 7
            RX bytes: 1978767922 frames: 21288686
            TX bytes: 481256491  frames: 16224065     dropframes: 13671

ethport1  : dest : ethif1
            Connection State: connected   Retry: 10
            Priority Tagging: NA (destination switch interface)
            Port: ethport1       PortNr: 1          PortState: forwarding  Interface: up
            Multicast filter: disabled              Dynamic VLAN    : disabled
            IGMP snooping   : enabled
            VLAN: Default VLAN: default  Ingressfiltering: disabled  Acceptvlanonly: disabled
            VLAN: Priority: disabled  IP Prec: disabled  Priority: 0  Regeneration table: 0 1 2 3 4 5 6 7
            RX bytes: 44045      frames: 0
            TX bytes: 12618      frames: 0            dropframes: 0

Can somebody give me some hints? My first thought was a regex, but I don't know how to do that on this scale.

+2  A: 

"Into something useful" means to me that your first step is to create classes, structs, and enums that represent your data:

public class RouterEntry
{
    public ConnectionState ConnectionState { get; }
    public int Retry { get; }
    ...
    public long BytesTX { get; }
}

Then, start thinking about how to parse the result strings. I would probably write a Deserialize(StringReader) method (or something along those lines) so that your object parses the data line by line.

You can also use string.Split with the SplitOptions overload to help you ignore any blank spaces.

So for example, if you use

Port: OBC            PortNr: 0        PortState: forwarding  Interface: up

as your input line, and

char[] delims = new char[] { ':', ' ' };

as your delimiters, then calling it like this

string[] tokens = line.Split(delims, StringSplitOptions.RemoveEmptyEntries);

would result in tokens containing

[Port, OBC, PortNr, 0, PortState, forwarding, Interface, up]

For the lines that have multiple "values" for a key (such as Regeneration table: 0 1 2 3... you'll just to take into account the fact that each of those values will be a separate string in the tokens array).

The key is that if you break your problem down one bit a time, it should become more manageable to solve.

Erich Mirabal