-
-
Save alexisdacquay/1db1c67196deb8b54a1b to your computer and use it in GitHub Desktop.
buildVlanVni
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# Copyright (c) 2015, Arista Networks, Inc. | |
# All rights reserved. | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions are | |
# met: | |
# - Redistributions of source code must retain the above copyright notice, | |
# this list of conditions and the following disclaimer. | |
# - Redistributions in binary form must reproduce the above copyright | |
# notice, this list of conditions and the following disclaimer in the | |
# documentation and/or other materials provided with the distribution. | |
# - Neither the name of Arista Networks nor the names of its | |
# contributors may be used to endorse or promote products derived from | |
# this software without specific prior written permission. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARISTA NETWORKS | |
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR | |
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE | |
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN | |
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
# | |
# Build VLAN VNI | |
# | |
# Version 1.0 2015-02-20 | |
# Written by: | |
# Alexis Dacquay, Arista Networks | |
# | |
# Revision history: | |
# 1.0 - First released version | |
''' | |
Instructions - Example | |
Configure: | |
./buildVlanVni.py -d -v 102 -i "2.2.2.2 3.3.3.3 4.4.4.4" -n 102102 | |
Undo config: | |
./buildVlanVni.py -d -v 102 -i "2.2.2.2 3.3.3.3 4.4.4.4" -n 102102 -u | |
''' | |
from __future__ import print_function | |
from jsonrpclib import Server | |
import optparse | |
import signal | |
import copy | |
#---------------------------------------------------------------- | |
# Configuration section | |
#---------------------------------------------------------------- | |
#-------------------Configuration - MUST Set ------------------------ | |
EAPI_USERNAME = 'admin' | |
EAPI_PASSWORD = '' | |
EAPI_ENABLE_PASSWORD = '' | |
# http or https method | |
EAPI_METHOD = 'http' | |
#-------------------------------------------------------- | |
errors = {} | |
# Variable initialisation | |
debug = None | |
def main(): | |
global debug | |
signal.signal(signal.SIGINT, lambda x,y: sys.exit(0)) | |
# Create help string and parse cmd line | |
usage = 'usage: %prog [options]' | |
op = optparse.OptionParser(usage=usage) | |
op.add_option( '-d', '--debug', dest='debug', action='store_true', | |
help='print debug info' ) | |
op.add_option( '-u', '--unconf', dest='unconf', action='store_true', | |
help='unconfigure the stated arguments' ) | |
op.add_option( '-v', '--vlan', dest='vlan', action='store', | |
help='VLAN ID - numerical value [1-4094]', type='int') | |
op.add_option( '-n', '--vni', dest='vni', action='store', | |
help='VXLAN ID: VNI - numerical value', type='int') | |
op.add_option( '-i', '--ip', dest='hosts', action='store', | |
help='list of host IP addresses, where to configure the VLAN and VNI' | |
, type='string') | |
opts, _ = op.parse_args() | |
debug = opts.debug | |
unconf = opts.unconf | |
vlan = opts.vlan | |
vni = opts.vni | |
hosts = opts.hosts | |
iplist = hosts.split() | |
if unconf: | |
negate = 'no' | |
else: | |
negate = '' | |
if debug: | |
print('Review of the options selected:') | |
print('debug:\t\t' + str(debug)) | |
print('remove:\t\t' + str(unconf)) | |
print('vlan:\t\t' + str(vlan)) | |
print('vni:\t\t' + str(vni)) | |
print('IP list:\t' + str(iplist)) | |
print('') | |
for ip in iplist: | |
otherip = copy.copy(iplist) | |
otherip.remove(ip) | |
others = ' '.join(otherip) | |
switch = Server( '%s://%s:%s@%s/command-api' % | |
( EAPI_METHOD, EAPI_USERNAME, EAPI_PASSWORD, ip ) ) | |
rc = switch.runCmds( 1, [ 'enable', | |
'configure', | |
'%s vlan %d' % ( negate, vlan ), | |
'interface Vxlan1', | |
'%s vxlan vlan %d vni %d' % ( negate, vlan, vni ), | |
'%s vxlan vlan %d flood vtep %s' % ( negate, vlan, others ) ] ) | |
print( 'Host configured: %s' % ( ip ) ) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment