|
| 1 | +#! /usr/bin/env python |
| 2 | +"""Sample use of the netmiko library for CLI interfacing |
| 3 | +
|
| 4 | +This script will create new configuration on a device. |
| 5 | +
|
| 6 | +Copyright (c) 2018 Cisco and/or its affiliates. |
| 7 | +
|
| 8 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +of this software and associated documentation files (the "Software"), to deal |
| 10 | +in the Software without restriction, including without limitation the rights |
| 11 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +copies of the Software, and to permit persons to whom the Software is |
| 13 | +furnished to do so, subject to the following conditions: |
| 14 | +
|
| 15 | +The above copyright notice and this permission notice shall be included in all |
| 16 | +copies or substantial portions of the Software. |
| 17 | +
|
| 18 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | +SOFTWARE. |
| 25 | +""" |
| 26 | + |
| 27 | +# Import libraries |
| 28 | +from netmiko import ConnectHandler |
| 29 | + |
| 30 | +from device_info import ios_xe1 as device # noqa |
| 31 | + |
| 32 | +# New Loopback Details |
| 33 | +loopback = {"int_name": "Loopback103", |
| 34 | + "description": "Demo interface by CLI and netmiko", |
| 35 | + "ip": "192.168.103.1", |
| 36 | + "netmask": "255.255.255.0"} |
| 37 | + |
| 38 | +# Create a CLI configuration |
| 39 | +interface_config = [ |
| 40 | + "interface {}".format(loopback["int_name"]), |
| 41 | + "description {}".format(loopback["description"]), |
| 42 | + "ip address {} {}".format(loopback["ip"], loopback["netmask"]), |
| 43 | + "no shut" |
| 44 | +] |
| 45 | + |
| 46 | +# Open CLI connection to device |
| 47 | +with ConnectHandler(ip = device["address"], |
| 48 | + port = device["ssh_port"], |
| 49 | + username = device["username"], |
| 50 | + password = device["password"], |
| 51 | + device_type = device["device_type"]) as ch: |
| 52 | + |
| 53 | + # Send configuration to device |
| 54 | + output = ch.send_config_set(interface_config) |
| 55 | + |
| 56 | + # Print the raw command output to the screen |
| 57 | + print("The following configuration was sent: ") |
| 58 | + print(output) |
0 commit comments