This program replaces variables in a template.
This is primarily to automate configuration files for routers and switches.
There are better products on the market that do this, but it is still a fun exercise.
This program not only replaces strings.
It also provides a few functions and a simple grammar.
Once declared, variables may be referenced by successive variables.
Function calls are recursively evaluated from the inside out using a simple parser.
You can easily extend this program by adding additional lexemes and lambdas to the grammar
dictionary.
MASK(LENGTH)
.
Converts a classless inter-domain routing (CIDR) prefix length to subnet mask.
LENGTH
may be preceeded by a forward slash, "/".NETWORK(IP, MASK)
. Calculates the network address for an IP
address. Functions may nest, such as NETWORK(IP, MASK(LENGTH))
.WILDCARD(IP)
. Inverts the bits in a DWORD for Cisco's
"wildcard mask" silliness.INT(X)
. Returns a integer value from a dotted-decimal value.
The exact value of your integer is implementation defined but probably a
twos-complement signed 32-bit DWORD.DEC(X)
. Converts an integer from its 32-bit literal representation
to a dotted-decimal string.PLUS(X, Y)
. Performs addition on numeric inputs.
This function returns an integer, which you may need to convert to
dotted-decimal notation with DEC(X)
.Values may not contain a function name from above, parenthesis ("(" and ")"), commas (","), or the equals symbol ("="). Lines starting with "!" or "#" are skipped. Be careful with your spelling! Any string not previously defined as a variable will be treated as a literal. This program does not check for mismatched parenthesis.