A tool to convert Juniper config

Overview

On Juniper routers, we have released a tool that converts the config file output by the "show configration" command into the "set / edit" command format that can be actually submitted to the router. https://github.com/taijiji/junos_config_converter

Motivation made

With a Cisco router, you can set it by simply copying the result output by the "show running-config" command, but with a Juniper router, you can input the result output by the "show configuration" command to the router. It was annoying because I had to manually modify the "set / edit" format conversion. So I made it with great momentum, including studying python.

--Supplement 20150412 Juniper routers accept a function that outputs the config being set in set / edit format when you enter "show configuration | display set", and a setting config in "show configuration" format when you enter "load merge terminal". It has a function. The purpose of this tool is to easily create a setting config without using a router in the situation where setting commands and procedure manuals are created in advance.

code

I was able to make it quickly without doing anything difficult. Here, the program that outputs in the format including "set / edit" that can be set in the router from the config of the format output by the "show configuration" command on the Juniper router is implemented. However, on the contrary, the program that converts the "set / edit" format config to the "show configuration" format has not been implemented.

junos_config_converter/convert_config.py


#! /usr/bin/env python
# -*- coding: utf-8 -*-

def convert_from_show_to_set(input_text):
    output_text = ''
    indent = ''
    for line in input_text.splitlines():
        if line == '':
            output_text += '\n'
        elif line[-1] == '{':
            output_text += indent + 'edit ' + line[:-1].strip() + '\n'
            indent += ' ' * 4
        elif line[-1] == ';':
            output_text += indent + 'set  ' + line[:-1].strip() + '\n'
        elif line[-1] == '}':
            indent = indent[:-4]
            output_text += indent + 'up\n'
        elif '; ## SECRET-DATA' in line:
            # ignore sentence of "## SECRET-DATA"
            output_text += indent + 'set ' + line.strip('; ## SECRET-DATA') + '\n'
        else:
            output_text += line + '\n'
    return output_text

How to use

It's easy to use, just call the convert_from_show_to_set () function.

--Input example (show configuration format)

junos_config_converter/sample/sample_config_show.txt


system {
    host-name R1;
    time-zone Asia/Tokyo;
    root-authentication {
        encrypted-password "$1$9kcwd00g$YDqr8sBMaAh8SOCjQ2f4b0"; ## SECRET-DATA
    }
}
interfaces {
    fe-0/0/0 {
        unit 0 {
            family inet {
                address 192.168.1.1/24;
            }
        }
    }
}

--Tool usage example

junos_config_converter/sample/sample_from_show_to_set.py



#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os, sys
import datetime
from convert_config import convert_from_show_to_set

sys.path.append(os.getcwd())

file_input = open('sample_config_show.txt', 'r')
input_text = file_input.read()
file_input.close()

output_text = convert_from_show_to_set( input_text )

# example: 20150405_2151
current_date_str = datetime.datetime.today().strftime( '%Y%m%d_%H%M' )

file_output = open ('output' + current_date_str + '.txt', 'w')
file_output.write( output_text )
file_output.close

--Output example (set / edit format)

junos_config_converter/sample/output20150405_2151.txt


edit system
    set  host-name R1
    set  time-zone Asia/Tokyo
    edit root-authentication
        set encrypted-password "$1$9kcwd00g$YDqr8sBMaAh8SOCjQ2f4b0"
    up
up
edit interfaces
    edit fe-0/0/0
        edit unit 0
            edit family inet
                set  address 192.168.1.1/24
            up
        up
    up
up

Developmental status

Currently only the conversion of "show configration"-> "set / edit" is implemented. (convert_from_show_to_set function) I'm in the process of implementing the conversion of "set / edit"-> "show configration" (convert_from_set_to_show function), but I'm having a hard time because I need to analyze the structure of JUNOS config to implement it.

For example, if the input of set / edit has the following config, it is necessary to determine which word is a JUNOS-specific fixed word or a variable defined by the user. (It can be implemented by defining JUNOS fixed words in solid writing, but it is a bit of a pain.)

--Input

set routing-options rib inet.0 static route 192.168.2.1/24  next-hop 192.168.1.1

--Output

routing-options {
            rib inet.0 {
                static {
                    route 192.168.2.1/24 next-hop 192.168.1.1;
                }
            }
}

Personally, I often use "show configration"-> "set / edit" conversion mainly for creating procedure manuals, but "set / edit"-> "show configration" conversion is a network operation job. There are few opportunities to use it, and the motivation for implementation itself is low. If you have a need to implement it, please comment.

Recommended Posts

A tool to convert Juniper config
Convert A4 PDF to A3 every 2 pages
Convert a string to an image
How to create a config file
Create a command line tool to convert dollars to yen using Python
Convert to HSV
I made a CLI tool to convert images in each directory to PDF
I made a tool to convert Jupyter py to ipynb with VS Code
I made a router config collection tool Config Collecor
Convert a multidimensional list (array) to one dimension
I made a tool to compile Hy natively
Write a script to convert a MySQL dump to TSV
I made a tool to get new articles
How to convert / restore a string with [] in python
Convert a slice object to a list of index numbers
Convert 202003 to 2020-03 with pandas
Try to make a command standby tool with python
Convert kanji to kana
Convert a text file with hexadecimal values to a binary file
How to convert a class object to a dictionary with SQLAlchemy
Convert jupyter to py
Convert keras-yolo3 to onnx
I made a code to convert illustration2vec to keras model
Creating a scraping tool
Convert dict to array
How to convert a mel spectrogram back to a wav file
[Python] Created a method to convert radix in 1 second
Convert json to excel
How to convert an array to a dictionary with Python [Application]
Tool to centrally acquire the configuration of Cisco / Juniper router
I made a tool to create a word cloud from wikipedia
Create a tool to check scraping rules (robots.txt) in Python
Convert the image data (png) at hand to a .pbm image
[Titan Craft] I made a tool to summon a giant to Minecraft
A quick introduction to pytest-mock
Convert hexadecimal string to binary
[python] Convert date to string
Convert numpy int64 to python int
[Python] Convert list to Pandas [Pandas]
A road to intermediate Python
Convert HTML to text file
Add a dictionary to MeCab
How to call a function
Tool to check code style
Upload a file to Dropbox
Send a signal to subprocess
How to hack a terminal
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
Convert IP address to decimal
Convert SDF to CSV quickly
Try to select a language
Convert genbank file to gff file
Convert python 3.x code to python 2.x
Convert to a string while outputting standard output with Python subprocess
I made a library to easily read config files with Python
I tried to convert a Python file to EXE (Recursion error supported)
I wanted to convert my face photo into a Yuyushiki style.
[3rd] I tried to make a certain authenticator-like tool with python
Convert a large number of PDF files to text files using pdfminer
[4th] I tried to make a certain authenticator-like tool with python