Documentation
¶
Overview ¶
The package confparser is used to extract element and logical configuration state from network device configuration files.
It was written for a small personal project, but feel free to use or fork as required.
At the time of writing, only Cisco IOS configurations are supported. But the library was written with the intention of being extensible at a later date.
Creating a Client ¶
The eaiest way to use the package is to create a client for the make of networking device configuration you want to parse.
For example:
package main import ( "fmt" ".../parser" ) func main() { // Create the Parser Client client, err := parser.NewClient(parser.PARSER_CISCO) if err != nil { // Handle the error } // Check the Parser Client make fmt.Printf("This Parser Client is for \"%v\" Configuration Files\n", client.Make) }
You then have the choice to parse either:
- The whole configuration file - A single block of vlan configuration - A single block of Interface configuration
Parsing an Interface Configuration Block ¶
The most common use-case for me was the need to parse Interface configuration blocks and extract the relavent elements.
For example:
package main import ( "fmt" ".../confparser" ) const ( myInterfaceConfig = `interface GigabitEthernet1/0/1 description STANDARD ACCESS INTERFACE switchport access vlan 101 switchport mode access power inline never` ) func main() { // Create the Conf Parser Client client, err := confparser.NewClient(confparser.PARSER_CISCO) if err != nil { // Handle the error } // Create a Config Interface Parser and Set the Interface Configuration: confIntParser := client.NewConfigIntParser() Set(myInterfaceConfig) // Evaluate aspects of the Interface Configuration fmt.Printf("Full Name for Interface: %v\n", confIntParser.InterfaceNameFull()) fmt.Printf("Short Name for Interface: %v\n", confIntParser.InterfaceNameShort()) fmt.Printf("Access Vlan for Interface: %v\n", confIntParser.VlanAccess()) }
See the code and testing packages for me details.
Index ¶
- Constants
- func GetBlocksByPrefixString(s, p string) []string
- func GetBlocksByRegexString(s, r string) []string
- func GetCiscoConfIntAccessVlan(s string) string
- func GetCiscoConfIntDescription(s string) string
- func GetCiscoConfIntIpV4Address(s string) string
- func GetCiscoConfIntIpV4AddressAndMask(s string) string
- func GetCiscoConfIntIpV4Mask(s string) string
- func GetCiscoConfIntNameFull(s string) string
- func GetCiscoConfIntNameShort(s string) string
- func GetCiscoConfIntPowerInlineConsumption(s string) string
- func GetCiscoConfIntTrunkAllowedVlans(s string) string
- func GetCiscoConfIntTrunkAllowedVlansSlice(s string) []string
- func GetCiscoConfIntTrunkNativeVlan(s string) string
- func GetCiscoConfIntVoiceVlan(s string) string
- func GetCiscoConfMainHostname(s string) string
- func GetCiscoConfVlanName(s string) string
- func GetCiscoConfVlanNumber(s string) string
- func GetInterfaceBlocks(s string) []string
- func GetInterfaceBlocksByType(s, t string) []string
- func GetInterfaceBlocksEthernet(s string) []string
- func GetInterfaceBlocksVlan(s string) []string
- func GetVlanBlocks(s string) []string
- func IsCiscoConfIntEthernet(s string) bool
- func IsCiscoConfIntEthernetFast(s string) bool
- func IsCiscoConfIntEthernetGigabit(s string) bool
- func IsCiscoConfIntEthernetTenGigabit(s string) bool
- func IsCiscoConfIntIpV4Int(s string) bool
- func IsCiscoConfIntPowerInlineConsumption(s string) bool
- func IsCiscoConfIntPowerInlineNever(s string) bool
- func IsCiscoConfIntShutdown(s string) bool
- func IsCiscoConfIntSwitchPortModeAccess(s string) bool
- func IsCiscoConfIntSwitchPortModeTrunk(s string) bool
- func IsCiscoConfIntVlan(s string) bool
- type CiscoConf
- type CiscoConfInt
- func (c CiscoConfInt) Description() string
- func (c CiscoConfInt) InterfaceNameFull() string
- func (c CiscoConfInt) InterfaceNameShort() string
- func (c CiscoConfInt) IpAddress() string
- func (c CiscoConfInt) IpAddressAndMask() string
- func (c CiscoConfInt) IpMask() string
- func (c CiscoConfInt) IpV4Address() string
- func (c CiscoConfInt) IpV4AddressAndMask() string
- func (c CiscoConfInt) IpV4Mask() string
- func (c CiscoConfInt) IsEthernet() bool
- func (c CiscoConfInt) IsEthernetFast() bool
- func (c CiscoConfInt) IsEthernetGigabit() bool
- func (c CiscoConfInt) IsEthernetTenGigabit() bool
- func (c CiscoConfInt) IsIpInt() bool
- func (c CiscoConfInt) IsIpV4Int() bool
- func (c CiscoConfInt) IsPoeDisabled() bool
- func (c CiscoConfInt) IsPoeStatic() bool
- func (c CiscoConfInt) IsShutdown() bool
- func (c CiscoConfInt) IsSwAccess() bool
- func (c CiscoConfInt) IsSwTrunk() bool
- func (c CiscoConfInt) IsVlan() bool
- func (c CiscoConfInt) PoeConsumption() string
- func (c *CiscoConfInt) Set(s string)
- func (c CiscoConfInt) String() string
- func (c CiscoConfInt) VlanAccess() string
- func (c CiscoConfInt) VlanTrunkNative() string
- func (c CiscoConfInt) VlanVoice() string
- func (c CiscoConfInt) VlansTrunkAllowed() string
- func (c CiscoConfInt) VlansTrunkAllowedSlice() []string
- type CiscoConfVlan
- type ConfIntParser
- type ConfParser
- type ConfVlanParser
- type ParserClient
- type ShowCommands
Constants ¶
const ( CommandsCiscoShowConfig = "show run,show running-config" CommandsCiscoShowInterfaces = "show interfaces" CommandsCiscoShowVersion = "show version" )
const ( PrefixCiscoConfInterface = "interface" PrefixCiscoConfVlan = "vlan" )
const ( RegexCiscoConfMainHostname = `^\s*hostname (.+)$` RegexCiscoConfVlan = `^\s*vlan \d+$` RegexCiscoConfVlanNumber = `^\s*vlan (\d+)$` RegexCiscoConfVlanName = `^\s*name (.+)$` RegexCiscoConfInterface = `^\s*interface .+$` RegexCiscoConfIntNameFull = `^\s*interface (.+)$` RegexCiscoConfIntNameShort = `^\s*interface (.{2})[^\d]+(.+)$` RegexCiscoConfIntDescription = `^\s*description\s(.+)$` RegexCiscoConfIntShutdown = `^\s*shutdown\s*$` RegexCiscoConfIntNoShutdown = `^\s*no\sshutdown\s*$` RegexCiscoConfIntSwitchPortModeAccess = `^\s*switchport mode access\s*$` RegexCiscoConfIntSwitchPortModeTrunk = `^\s*switchport mode trunk\s*$` RegexCiscoConfIntAccessVlan = `^\s*switchport access vlan (\d+)\s*$` RegexCiscoConfIntVoiceVlan = `^\s*switchport voice vlan (\d+)\s*$` RegexCiscoConfIntTrunkNativeVlan = `^\s*switchport trunk native vlan (\d+)\s*$` RegexCiscoConfIntTrunkAllowedVlans = `^\s*switchport trunk allowed vlan ([\d,]+)\s*$` RegexCiscoConfIntPowerInlineNever = `^\s*power inline never\s*$` RegexCiscoConfIntNoIpV4Address = `^\s*no ip address\s*$` RegexCiscoConfIntIpV4AddressAndMask = `^\s*ip address (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*$` RegexCiscoConfIntPowerInlineConsumption = `^\s*power inline consumption (\d+)\s*$` RegexCiscoConfIntTypeEthernet = `^\s*interface .+Ethernet\d+` RegexCiscoConfIntTypeVlan = `^\s*interface Vlan\d+` RegexCiscoConfIntTypeEthernetFast = `^\s*interface FastEthernet\d+` RegexCiscoConfIntTypeEthernetGigabit = `^\s*interface GigabitEthernet\d+` RegexCiscoConfIntTypeEthernetTenGigabit = `^\s*interface TenGigabitEthernet\d+` )
const ( // Supprted Parser Makes PARSER_CISCO = "cisco" PARSER_HP = "hp" )
Variables ¶
This section is empty.
Functions ¶
func GetBlocksByPrefixString ¶ added in v0.2.0
GetBlocksByPrefixString can be used to extract any block from the configuration by using a Prefix string as a delimiter.
It also works just as well on single lines which have a defined prefix, such as "aaa" or "snmp-server".
TODO: Needs Test
func GetBlocksByRegexString ¶ added in v0.2.0
GetBlocksByRegexString can be used to extract any block from the configuration by using a Prefix regex string as a delimiter. The regex should be passed as a string, so that the calling module does not need to import the regexp package. If the regex string fails to compile, a nil slice will be returned.
It also works just as well on single lines which have a defined prefix, such as "aaa" or "snmp-server".
TODO: Needs Test
func GetCiscoConfIntIpV4Mask ¶
func GetCiscoConfIntNameFull ¶
func GetCiscoConfVlanName ¶
func GetCiscoConfVlanNumber ¶
func GetInterfaceBlocks ¶
func GetInterfaceBlocksVlan ¶
func GetVlanBlocks ¶
func IsCiscoConfIntEthernet ¶ added in v0.2.0
func IsCiscoConfIntEthernetFast ¶ added in v0.2.0
func IsCiscoConfIntEthernetGigabit ¶ added in v0.2.0
func IsCiscoConfIntEthernetTenGigabit ¶ added in v0.2.0
func IsCiscoConfIntIpV4Int ¶
func IsCiscoConfIntPowerInlineNever ¶
IsCiscoConfIntPowerInlineNever will return true if the interface configuration contains "power inline never". NOTE: This indicates that PoE is disabled. I.e. `true` indicates NO PoE on the interface.
func IsCiscoConfIntShutdown ¶
func IsCiscoConfIntVlan ¶ added in v0.2.0
Types ¶
type CiscoConf ¶
type CiscoConf string
CiscoConf is the full configuration for a Cisco device from the `show running-config` command output.
func (CiscoConf) InterfaceBlocks ¶
InterfaceBlocks returns all of the "interface xxx" sections from the configuration, as a slice of string. Any errors in the extraction will not be reported and a nil slice will be returned.
func (CiscoConf) InterfaceBlocksEthernet ¶
InterfaceBlocksEthernet returns all of the "interface xxx" sections from the configuration, where the interface is an "Ethernet" type, as a slice of string. Any errors in the extraction will not be reported and a nil slice will be returned.
func (CiscoConf) InterfaceBlocksVlan ¶
InterfaceBlocksEthernet returns all of the "interface xxx" sections from the configuration, where the interface is a "Vlan" type, as a slice of string. Any errors in the extraction will not be reported and a nil slice will be returned.
func (CiscoConf) String ¶
String is used to output the CiscoConf type as a string. It fulfills the Stringer interface.
func (CiscoConf) VlanBlocks ¶
InterfaceBlocks returns all of the "interface xxx" sections from the configuration, as a slice of string. Any errors in the extraction will not be reported and a nil slice will be returned.
type CiscoConfInt ¶
type CiscoConfInt string
CiscoConfInt is the interface configuration block for a Cisco device from the `show running-config` command output. For example:
interface GigabitEthernet1/0/22 description [A-001] switchport access vlan 101 switchport mode access power inline never
A block can be extracted from the main configuration using the `CiscoConf.IntrfacBlocks()` method.
func (CiscoConfInt) Description ¶
func (c CiscoConfInt) Description() string
Description returns the `description xxx` from the interface configuration block, as a string.
func (CiscoConfInt) InterfaceNameFull ¶
func (c CiscoConfInt) InterfaceNameFull() string
InterfaceNameFull returns the full intrface name from the interface configuration block, as a string.
func (CiscoConfInt) InterfaceNameShort ¶
func (c CiscoConfInt) InterfaceNameShort() string
InterfaceNameShort returns the shortened interface name from the interface configuration block, as a string. For example, `GigabitEthernet1/0/22`, would be shortened to, `Gi1/0/22`.
func (CiscoConfInt) IpAddress ¶
func (c CiscoConfInt) IpAddress() string
IpAddress - To Be Documented
func (CiscoConfInt) IpAddressAndMask ¶
func (c CiscoConfInt) IpAddressAndMask() string
IpAddressAndMask - To Be Documented
func (CiscoConfInt) IpV4Address ¶
func (c CiscoConfInt) IpV4Address() string
IpV4Address - To Be Documented
func (CiscoConfInt) IpV4AddressAndMask ¶
func (c CiscoConfInt) IpV4AddressAndMask() string
IpV4AddressAndMask - To Be Documented
func (CiscoConfInt) IsEthernet ¶ added in v0.2.0
func (c CiscoConfInt) IsEthernet() bool
IsEthernetInt - To Be Documented
func (CiscoConfInt) IsEthernetFast ¶ added in v0.2.0
func (c CiscoConfInt) IsEthernetFast() bool
IsEthernetInt - To Be Documented
func (CiscoConfInt) IsEthernetGigabit ¶ added in v0.2.0
func (c CiscoConfInt) IsEthernetGigabit() bool
IsEthernetInt - To Be Documented
func (CiscoConfInt) IsEthernetTenGigabit ¶ added in v0.2.0
func (c CiscoConfInt) IsEthernetTenGigabit() bool
IsEthernetInt - To Be Documented
func (CiscoConfInt) IsPoeDisabled ¶
func (c CiscoConfInt) IsPoeDisabled() bool
IsPoeDisabled tests to see if PoE is disabled on the interface. By default, PoE is enabled in auto mode, but there would be no configuration line for this.
So it's better to test for the negative (I.e. Is PoE disabled?), as this is an explicit configuration entry.
func (CiscoConfInt) IsPoeStatic ¶
func (c CiscoConfInt) IsPoeStatic() bool
IsPoeStatic - To Be Documented
func (CiscoConfInt) IsShutdown ¶
func (c CiscoConfInt) IsShutdown() bool
IsShutdown returns the configured interface shutdown state from the interface configuration block, as a bool. If the interface is shutdown/disabled, then IsShutdown will return true.
func (CiscoConfInt) IsSwAccess ¶
func (c CiscoConfInt) IsSwAccess() bool
IsSwAccess returns the configured switchport access state from the interface configuration block, as a bool. If the interface as the config, `switchport mode access`, then IsSwAccess will return true.
func (CiscoConfInt) IsSwTrunk ¶
func (c CiscoConfInt) IsSwTrunk() bool
IsSwTrunk returns the configured switchport trunk state from the interface configuration block, as a bool. If the interface as the config, `switchport mode trunk`, then IsSwTrunk will return true.
func (CiscoConfInt) IsVlan ¶ added in v0.2.0
func (c CiscoConfInt) IsVlan() bool
IsVlanInt - To Be Documented
func (CiscoConfInt) PoeConsumption ¶
func (c CiscoConfInt) PoeConsumption() string
PoeConsumption - To Be Documented
func (*CiscoConfInt) Set ¶
func (c *CiscoConfInt) Set(s string)
Set is used to pass an interfacee configuration block to the CiscoConfInt type.
func (CiscoConfInt) String ¶
func (c CiscoConfInt) String() string
String is used to output the CiscoConfInt type as a string. It fulfills the Stringer interface.
func (CiscoConfInt) VlanAccess ¶
func (c CiscoConfInt) VlanAccess() string
VlanAccess returns the `switchport access vlan xxx` number from the interface configuration block, as a string.
func (CiscoConfInt) VlanTrunkNative ¶
func (c CiscoConfInt) VlanTrunkNative() string
VlanTrunkNative returns the `switchport trunk native vlan xxx` number from the interface configuration block, as a string.
func (CiscoConfInt) VlanVoice ¶
func (c CiscoConfInt) VlanVoice() string
VlanVoice returns the `switchport voice vlan xxx` number from the interface configuration block, as a string.
func (CiscoConfInt) VlansTrunkAllowed ¶
func (c CiscoConfInt) VlansTrunkAllowed() string
VlansTrunkAllowed returns the `switchport trunk allowed vlan xxx` list from the interface configuration block, as a comma separated string. For example, `switchport trunk allowed vlan 100,200,300`, would return the string, `100,200,300`.
func (CiscoConfInt) VlansTrunkAllowedSlice ¶
func (c CiscoConfInt) VlansTrunkAllowedSlice() []string
VlansTrunkAllowedSlice returns the `switchport trunk allowed vlan xxx` list from the interface configuration block, as a slice of strings.
type CiscoConfVlan ¶
type CiscoConfVlan string
CiscoConfVlan is a single vlan configuration block for a Cisco device from the `show running-config` command output. For example:
vlan 101 name Internet
A block can be extracted from the main configuration using the `CiscoConf.VlanBlocks()` method.
func (*CiscoConfVlan) Set ¶
func (c *CiscoConfVlan) Set(s string)
Set is used to pass a vlan configuration block to the CiscoConfVlan type.
func (CiscoConfVlan) String ¶
func (c CiscoConfVlan) String() string
String is used to output the CiscoConfVlan type as a string. It fulfills the Stringer interface.
func (CiscoConfVlan) VlanName ¶
func (c CiscoConfVlan) VlanName() string
VlanName returns the vlan name from the vlan configuration block, as a string.
func (CiscoConfVlan) VlanNumber ¶
func (c CiscoConfVlan) VlanNumber() string
VlanNumber returns the vlan number from the vlan configuration block, as a string.
type ConfIntParser ¶
type ConfIntParser interface { Set(s string) String() string InterfaceNameFull() string InterfaceNameShort() string Description() string VlanAccess() string VlanVoice() string VlanTrunkNative() string VlansTrunkAllowed() string // VlansTrunkAllowedSlice() []string // Not implemented yet IpAddressAndMask() string IpAddress() string IpMask() string IsShutdown() bool IsSwAccess() bool IsSwTrunk() bool IsPoeDisabled() bool IsEthernet() bool IsEthernetFast() bool IsEthernetGigabit() bool IsEthernetTenGigabit() bool IsVlan() bool }
ConfIntParser is an interface which all device interface configuration parsers should fulfill.
type ConfParser ¶
type ConfParser interface { Set(s string) String() string Hostname() string InterfaceBlocks() []string InterfaceBlocksEthernet() []string InterfaceBlocksVlan() []string VlanBlocks() []string }
ConfParser is an interface which all device configuration parsers should fulfill.
type ConfVlanParser ¶
type ConfVlanParser interface { Set(s string) String() string VlanNumber() string VlanName() string }
VlanParser is an interface which all device vlan configuration parsers should fulfill.
type ParserClient ¶
type ParserClient struct { Make string `json:"make"` ConfigParser confParserFunc ConfigVlanParser confVlanParserFunc ConfigIntParser confIntParserFunc }
TBD
func (*ParserClient) NewConfigIntParser ¶
func (p *ParserClient) NewConfigIntParser() ConfIntParser
TBD
func (*ParserClient) NewConfigVlanParser ¶
func (p *ParserClient) NewConfigVlanParser() ConfVlanParser
TBD
type ShowCommands ¶
TBD