Update juniper-config-to-set.php

This commit is contained in:
pgnuta 2018-03-26 14:09:59 +13:00 committed by GitHub
parent fa7ea36c38
commit 051f4b3002
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,22 +1,18 @@
#!/usr/bin/php #!/usr/bin/php
<?php <?php
/***************************************************************************************** /******************************************************************************************************
* *
* Written by Tim Price 24/11/2016 * Written by Tim Price 24/11/2016
* *
* The purpose of this script is to take a juniper config and convert it to set commands. * The purpose of this script is to take a juniper config and convert it to set commands.
* There was a business need to audit thbe correctness of Juniper configs and the process * There was a business need to audit thbe correctness of Juniper configs and the process of logging
* of logging into each Juniper device and dumping the configs in set format was too * into each Juniper device and dumping the configs in set format was too burdensome on both the Juniper
* burdensome on both the Juniper devices in question but also extended the execution time * devices in question but also extended the execution time of the audit scripts by some minutes.
* of the audit scripts by some minutes.
*
* The script will take a juniper config piped to it from STDIN and output the results to
* STDOUT
* *
* Usage example: `cat juniper-config.txt | juniper-config-to-set.php` * Usage example: `cat juniper-config.txt | juniper-config-to-set.php`
* *
*****************************************************************************************/ ******************************************************************************************************/
function endswith($string, $test) { function endswith($string, $test) {
$strlen = strlen($string); $strlen = strlen($string);
@ -51,17 +47,22 @@ while($line = fgets(STDIN)){
// Trim white space from the line // Trim white space from the line
$line=trim($line); $line=trim($line);
// Trim inline comments from the line
if(preg_match('/; ##/',$line)==1) {
list($line,$comment)=preg_split('/; ##/',$line);
$line .= ";";
}
// What remains is a series of tests to see what the line ends with. // What remains is a series of tests to see what the line ends with.
// This test matches a ';' character which means its a leaf and we can output the // This test matches a ';' character which means its a leaf and we can output the whole line to STDOUT
// whole line to STDOUT
if(endswith($line,';')) { if(endswith($line,';')) {
// Trim the semi-colon from the end of the line, we don't need that. // Trim the semi-colon from the end of the line, we don't need that.
$line = rtrim($line,';'); $line = rtrim($line,';');
// Test to see if the tree is empty, if it is then we'll just print the leaf and //Test to see if the tree is empty, if it is then we'll just print the leaf and not the tree
// not the tree to avoid excess white spaces //to avoid excess white spaces
if(count($tree)==0) { if(count($tree)==0) {
echo "set $line\n"; echo "set $line\n";
} else { } else {
@ -69,15 +70,13 @@ while($line = fgets(STDIN)){
} }
} }
// This test matches a '{' character on the end of the line and means we need to add a // This test matches a '{' character on the end of the line and means we need to add a branch to the tree
// branch to the tree
if(endswith($line,'{')) { if(endswith($line,'{')) {
$line = rtrim($line,' {'); $line = rtrim($line,' {');
$tree=addtotree($tree,$line); $tree=addtotree($tree,$line);
} }
// This test matches a '}' character on the end of the line and means we need to // This test matches a '}' character on the end of the line and means we need to remove the last branch that we added to the tree
// remove the last branch that we added to the tree
if(endswith($line,'}')) { if(endswith($line,'}')) {
$tree=removewordfromtree($tree); $tree=removewordfromtree($tree);
} }