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
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.
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
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
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