• Configure groups of interfaces based on description regex

 
 
Print Friendly, PDF & Email

A simple python eAPI script to find groups of interfaces to configure based on a regex match to interface descriptions. The most interesting part is that the script types out the “interface <list of matching interfaces>” command in an interactive manner as if the EOS CLI user typed it. The command is even saved in the command history. Give it a try!

 

1) Configure a command alias:

alias intRegex bash sudo /mnt/flash/intRegex.py %1

 

2) Load the script in flash:

#!/usr/bin/env python
#
# intRegex.py ver 1.12

# Arista Networks, Inc.

import jsonrpclib, os, re, socket, sys, fcntl, termios

from jsonrpclib import Server

 

if __name__ == “__main__”:

    if len (sys.argv ) != 2:

        print “Usage: ” + sys.argv[ 0 ] + ” <interface description regex>”

        sys.exit( 1 )

    if len ( sys.argv ) == 2:

        regex = sys.argv[ 1 ]

        localSwitch = Server( “unix:/var/run/command-api.sock” )

 

    try:

        intDesc = localSwitch.runCmds( 1, [ “enable”,

                                              “show interfaces description” ] )[ 1 ][ “interfaceDescriptions” ]

    except Exception as e:

        print e

        sys.exit( 1 )

 

intDescList = []

 

descRegex = re.compile( regex )

 

for key1,value1 in intDesc.items():

   for key2, value2 in value1.items():

      if key2  == “description”:

         descMatch = re.findall( descRegex, value2 )

         if descMatch:

            intDescList.append( key1 )

 

ints = “interface ” + ‘,’.join( intDescList ) + “\n”

 

ghostTty = os.ttyname( sys.stdin.fileno() )

 

if len( intDescList ) > 0:

   with open( ghostTty, ‘w’) as fd:

      oldSetting = termios.tcgetattr(fd)

      newSetting = oldSetting[:]

      newSetting[ 3 ] = newSetting[ 3 ] & ~termios.ECHO

      termios.tcsetattr( fd, termios.TCSANOW, newSetting )

      for char in ints:

         fcntl.ioctl( fd, termios.TIOCSTI, char )

      termios.tcsetattr( fd, termios.TCSANOW, oldSetting )

 

sys.exit( 0 )

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: