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
<?php
/*****************************************************************************************
/******************************************************************************************************
*
* Written by Tim Price 24/11/2016
*
* 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
* of logging into each Juniper device and dumping the configs in set format was too
* burdensome on both the Juniper devices in question but also extended the execution time
* of the audit scripts by some minutes.
* There was a business need to audit thbe correctness of Juniper configs and the process of logging
* into each Juniper device and dumping the configs in set format was too burdensome on both the Juniper
* devices in question but also extended the execution time 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`
*
*****************************************************************************************/
******************************************************************************************************/
function endswith($string, $test) {
$strlen = strlen($string);
@ -51,17 +47,22 @@ while($line = fgets(STDIN)){
// Trim white space from the 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.
// This test matches a ';' character which means its a leaf and we can output the
// whole line to STDOUT
// This test matches a ';' character which means its a leaf and we can output the whole line to STDOUT
if(endswith($line,';')) {
// Trim the semi-colon from the end of the line, we don't need that.
$line = rtrim($line,';');
// Test to see if the tree is empty, if it is then we'll just print the leaf and
// not the tree to avoid excess white spaces
//Test to see if the tree is empty, if it is then we'll just print the leaf and not the tree
//to avoid excess white spaces
if(count($tree)==0) {
echo "set $line\n";
} 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
// branch to the tree
// This test matches a '{' character on the end of the line and means we need to add a branch to the tree
if(endswith($line,'{')) {
$line = rtrim($line,' {');
$tree=addtotree($tree,$line);
}
// 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
// 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
if(endswith($line,'}')) {
$tree=removewordfromtree($tree);
}