1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright 2013 Cisco Systems, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# @author: Hareesh Puthalath, Cisco Systems, Inc.
import sys
import logging
from ncclient import manager
log = logging.getLogger(__name__)
# Various IOS Snippets
CREATE_VRF = """
<config>
<cli-config-data>
<cmd>ip routing</cmd>
<cmd>ip vrf %s</cmd>
</cli-config-data>
</config>
"""
REMOVE_VRF = """
<config>
<cli-config-data>
<cmd>ip routing</cmd>
<cmd>no ip vrf %s</cmd>
</cli-config-data>
</config>
"""
CREATE_SUBINTERFACE = """
<config>
<cli-config-data>
<cmd>interface %s</cmd>
<cmd>encapsulation dot1Q %s</cmd>
<cmd>ip vrf forwarding %s</cmd>
<cmd>ip address %s %s</cmd>
</cli-config-data>
</config>
"""
REMOVE_SUBINTERFACE = """
<config>
<cli-config-data>
<cmd>no interface %s</cmd>
</cli-config-data>
</config>
"""
def csr_connect(host, port, user, password):
return manager.connect(host=host,
port=port,
username=user,
password=password,
device_params={'name': "csr"},
timeout=30
)
def create_vrf(conn, vrf_name):
try:
confstr = CREATE_VRF % vrf_name
rpc_obj = conn.edit_config(target='running', config=confstr)
_check_response(rpc_obj, "CREATE_VRF")
except Exception:
log.exception("Exception in creating VRF %s" % vrf_name)
def create_subinterface(conn, subinterface, vlan_id, vrf_name, ip, mask):
try:
confstr = CREATE_SUBINTERFACE % (subinterface, vlan_id,
vrf_name, ip, mask)
rpc_obj = conn.edit_config(target='running', config=confstr)
_check_response(rpc_obj, 'CREATE_SUBINTERFACE')
except Exception:
log.exception("Exception in creating subinterface %s" % subinterface)
def remove_vrf(conn, vrf_name):
try:
confstr = REMOVE_VRF % vrf_name
rpc_obj = conn.edit_config(target='running', config=confstr)
_check_response(rpc_obj, "REMOVE_VRF")
except Exception:
log.exception("Exception in removing VRF %s" % vrf_name)
def remove_subinterface(conn, subinterface):
try:
confstr = REMOVE_SUBINTERFACE % (subinterface)
rpc_obj = conn.edit_config(target='running', config=confstr)
_check_response(rpc_obj, 'REMOVE_SUBINTERFACE')
except Exception:
log.exception("Exception in removing subinterface %s" % subinterface)
def _check_response(rpc_obj, snippet_name):
log.debug("RPCReply for %s is %s" % (snippet_name, rpc_obj.xml))
xml_str = rpc_obj.xml
if "<ok />" in xml_str:
log.info("%s successful" % snippet_name)
else:
log.error("Cannot successfully execute: %s" % snippet_name)
def test_csr(host, user, password):
with csr_connect(host, port=22, user=user, password=password) as m:
create_vrf(m, "test_vrf")
create_subinterface(m, "GigabitEthernet 1.500",
'500', 'test_vrf',
'192.168.0.1', '255.255.255.0')
#Optional
remove_vrf(m, "test_vrf")
remove_subinterface(m, "GigabitEthernet 1.500")
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
test_csr(sys.argv[1], sys.argv[2], sys.argv[3])
|