March 04, 2014

Yet Another Day of Scripting with Python

These are two script that produce Juniper router MPLS LSP and path configuration.
The following CSV structures are eaten by those script:
LSP Definitions
Label switched path definition format is *LSP_NAME*,*TO_ADDR*,*PATH1 IP_ADDR1 IP_ADDR2...*,*PATH2 IP_ADDR3 IP_ADDR4*,...
DC1-DC3-1,3.3.37.38,DC1-DC3-cable2 3.3.71.209,DC1-DC4-DC3-sub1 3.3.88.185 3.3.71.81,DC1-DC4-DC3-cable1 3.3.37.241 3.3.71.81,DC1-DC3-3-sub2 3.3.88.96,DC1-DC3-sub 3.3.88.89,DC1-DC3-standby Standby
DC1-DC3-2,3.3.37.38,DC1-DC4-DC3-sub1 3.3.88.185 3.3.71.81,DC1-DC4-DC3-cable1 3.3.37.241 3.3.71.81,DC1-DC3-3-sub2 3.3.88.96,DC1-DC3-sub 3.3.88.89,DC1-DC4-DC3-sub4 3.3.88.100 3.3.71.81,DC1-DC3-standby Standby
The data will be eaten by below script…
#!/usr/bin/env python

import re

f = open('../lsp.csv', 'r')
lspFile = f.read()
lines = re.split('\n', lspFile)

for line in lines:
    """ LSP Name """
    entries = re.split(',', line)
    lspName = entries[0]
    toAddress = entries[1]

    print 'set protocols mpls label-switched-path',lspName,'to',toAddress
    print 'set protocols mpls label-switched-path',lspName,'node-link-protection'

    for i in range(2, len(entries)):
        pathInfo = re.split(' ', entries[i])

        if i == 2:
            print 'set protocols mpls label-switched-path',lspName,'primary',pathInfo[0]
        else:
            print 'set protocols mpls label-switched-path',lspName,'secondary',pathInfo[0],'standby'
    print ''    
The result is…
set protocols mpls label-switched-path DC1-DC3-1 to 3.3.37.38
set protocols mpls label-switched-path DC1-DC3-1 node-link-protection
set protocols mpls label-switched-path DC1-DC3-1 primary DC1-DC3-cable2
set protocols mpls label-switched-path DC1-DC3-1 secondary DC1-DC4-DC3-sub1 standby
set protocols mpls label-switched-path DC1-DC3-1 secondary DC1-DC4-DC3-cable1 standby
set protocols mpls label-switched-path DC1-DC3-1 secondary DC1-DC3-3-sub2 standby
set protocols mpls label-switched-path DC1-DC3-1 secondary DC1-DC3-sub standby
set protocols mpls label-switched-path DC1-DC3-1 secondary DC1-DC3-standby standby

set protocols mpls label-switched-path DC1-DC3-2 to 3.3.37.38
set protocols mpls label-switched-path DC1-DC3-2 node-link-protection
set protocols mpls label-switched-path DC1-DC3-2 primary DC1-DC4-DC3-sub1
set protocols mpls label-switched-path DC1-DC3-2 secondary DC1-DC4-DC3-cable1 standby
set protocols mpls label-switched-path DC1-DC3-2 secondary DC1-DC3-3-sub2 standby
set protocols mpls label-switched-path DC1-DC3-2 secondary DC1-DC3-sub standby
set protocols mpls label-switched-path DC1-DC3-2 secondary DC1-DC4-DC3-sub4 standby
set protocols mpls label-switched-path DC1-DC3-2 secondary DC1-DC3-standby standby
Path Definitions
LSP consist of paths, including primary and secondary paths. The format is PATH1 IP_ADDR1 IP_ADDR2...,PATH2 IP_ADDR3 IP_ADDR4,...
DC3-DC1-cable2 3.3.71.210,DC3-DC1-DC2-cable2 3.3.71.210 3.3.88.66,DC4-DC3-DC1-cable2 3.3.71.81 3.3.71.210,DC4-DC3-DC1-DC2-cable2 3.3.71.81 3.3.71.210 3.3.88.66
DC3-DC4-DC1-cable1 3.3.71.82 3.3.37.242,DC3-DC4-DC1-DC2-cable1 3.3.71.82 3.3.37.242 3.3.88.66,DC4-DC1-cable1 3.3.37.242,DC4-DC1-DC2-cable1 3.3.37.242 3.3.88.66
The data will be eaten by below script…
#!/usr/bin/env python

import re

f = open('../path.csv', 'r')
lspFile = f.read()
lines = re.split('\n', lspFile)

for line in lines:
    entries = re.split(',', line)

    for i in range(len(entries)):
        pathInfo = re.split(' ', entries[i])

        for j in range(1, len(pathInfo)):
            if pathInfo[j] != 'standby':
                print 'set protocols mpls path',pathInfo[0],pathInfo[j],'strict'
            else:
                print 'set protocols mpls path',pathInfo[0]

        print ''
The result is…
set protocols mpls path DC3-DC1-cable2 3.3.71.210 strict

set protocols mpls path DC3-DC1-DC2-cable2 3.3.71.210 strict
set protocols mpls path DC3-DC1-DC2-cable2 3.3.88.66 strict

set protocols mpls path DC4-DC3-DC1-cable2 3.3.71.81 strict
set protocols mpls path DC4-DC3-DC1-cable2 3.3.71.210 strict

set protocols mpls path DC4-DC3-DC1-DC2-cable2 3.3.71.81 strict
set protocols mpls path DC4-DC3-DC1-DC2-cable2 3.3.71.210 strict
set protocols mpls path DC4-DC3-DC1-DC2-cable2 3.3.88.66 strict

set protocols mpls path DC3-DC4-DC1-cable1 3.3.71.82 strict
set protocols mpls path DC3-DC4-DC1-cable1 3.3.37.242 strict

set protocols mpls path DC3-DC4-DC1-DC2-cable1 3.3.71.82 strict
set protocols mpls path DC3-DC4-DC1-DC2-cable1 3.3.37.242 strict
set protocols mpls path DC3-DC4-DC1-DC2-cable1 3.3.88.66 strict

set protocols mpls path DC4-DC1-cable1 3.3.37.242 strict

set protocols mpls path DC4-DC1-DC2-cable1 3.3.37.242 strict
set protocols mpls path DC4-DC1-DC2-cable1 3.3.88.66 strict
Feeling dizzy reading this post? You should take a break :D