Coder Social home page Coder Social logo

umurarslan / edit4config Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 33 KB

Nokia SROS, Cisco IOS style (parent/child with space indent) config edit (add/delete/replace) with regex

License: MIT License

Python 100.00%
cisco config-generator config-parser network-automation network-configuration network-configuration-parser network-programming nokia python

edit4config's Introduction

Edit4Config

Nokia SROS, Cisco IOS style (parent/child with space indentation) config edit module with add, delete, replace and search function with regex supported. Every line in config text converted to path (parent) and value (line) based on space indent. After this converting process (Config with Parents or CwP) every config line is unique with own parents and editing can be done easily.


Requirements

Python >= 3.9

For Windows, select the Add Python 3.x to PATH checkbox during installation.


Installation

pip install edit4config

CwP (Config with Parents) Example

Config Text:

configure
    ...
    card 1
        card-type iom-e
        ...
        mda 1
            mda-type me10-10gb-sfp+
            ...
            no shutdown
            ...
        mda 2
            ...
        ...
    ...
...

CwP List:

[
...
['configure', '    card 1'],
['configure,card 1', '        card-type iom-e'],
['configure,card 1', '        mda 1'],
['configure,card 1,mda 1', '            mda-type me10-10gb-sfp+'],
...
['configure,card 1,mda 1', '            no shutdown'],
...
['configure,card 1', '        mda 2'],
...
]

CwP Text:

...
configure,card 1
configure,card 1,card-type iom-e
configure,card 1,mda 1
configure,card 1,mda 1,mda-type me10-10gb-sfp+
...
configure,card 1,mda 1,no shutdown
...
configure,card 1,mda 2
...

Usage

EditConfig Simple Usage

After CwP converting EditConfig add, delete, replace, search and other methods can be used as below.

Add-Delete-Replace methods supported regex, multiple match.

Add-Delete methods supported serial CwP Text lines with newline.

# import EditConfig module
from edit4config import EditConfig

# read config file and get config_text
with open('CONFIG_FILE.txt') as file:
    config_text = file.read()

# define EditConfig object with options e.g. comments, step_space
# comments for Nokia is ('#', 'echo') and for Cisco is ('!')
# step_space for Nokia is 4 and for Cisco is 1
device_cwp = EditConfig(config_text, 4, ('#', 'echo'))

# add "sync-e" before "no shutdown" under configure,card 1,mda 1
device_cwp.add_before_lines(
                            'configure,card 1,mda 1,sync-e',
                            'configure,card 1,mda 1,no shutdown'
                        )

# delete "no shutdown" under configure,card 1,mda 1
device_cwp.delete_serial_lines('configure,card 1,mda 1,no shutdown')

# replace "no shutdown" with "shutdown" under configure,card 1,mda 1
device_cwp.replace_line(
                        'configure,card 1,mda 1,no shutdown',
                        'configure,card 1,mda 1,shutdown'
                    )

# delete "no shutdown" for all card and all mda with regex
device_cwp.delete_serial_lines(
                            'configure,card \d+,mda \d+,no shutdown',
                            regex_match=True, 
                            multiple_match=True
                            )

# after editing done, convert device_cwp object to text file
new_config_text = device_cwp.cwp_to_text()

with open('CONFIG_FILE_NEW.txt', 'w') as file:
    file.write(new_config_text)

Another example below for getting CwP and making custom jobs.

# import EditConfig module
from edit4config import EditConfig

# read config file and get config_text
with open('CONFIG_FILE.txt') as file:
    config_text = file.read()

# define EditConfig object with options e.g. comments, step_space
# comments for Nokia is ('#', 'echo') and for Cisco is ('!')
# step_space for Nokia is 4 and for Cisco is 1
device_cwp = EditConfig(config_text, 4, ('#', 'echo'))

# get full cwp-list from EditConfig object with <cwp> variable
device_cwp_list = device_cwp.cwp

# get ntp config from cwp-list and print
for line_path, line_value in device_cwp.cwp:
    if line_path.startswith('configure,system,time,ntp'):
        print(line_value.strip())

Besides simple usage check other EditConfig methods e.g. cwp_search, cwp_serial_check.

edit4config's People

Contributors

umurarslan avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.