create account

Python and Network Automation HOW TO - Part 2 Configuration Commands by dcolthar

View this thread on: hive.blogpeakd.comecency.com
· @dcolthar ·
$0.07
Python and Network Automation HOW TO - Part 2 Configuration Commands
<center>![Capture.PNG](https://steemitimages.com/DQmXPFaY7d2mJrGmYwEPcvqHxokrjfV8FACDwF7UAak9ked/Capture.PNG)
<h2>Moving from simple show commands to config commands with Python</h2></center>

In the **[first post on using Python to automate network show commands](https://steemit.com/howto/@dcolthar/python-and-network-automation-how-to-part-1-basic-show-commands)** we got a base script going where we took precautions such as trying to make it agnostic to running in Python2 and Python3.  We also got to the point where we ran a show single command against a single switch to receive some basic output.  Before we get into running against multiple devices, I wanted to jump back into the steps of using Python and Netmiko to automate network tasks by doing some simple examples of configuration commands.

Once again, I'll be performing this against my lab HP2530 switch, **[but check out the first post](https://steemit.com/howto/@dcolthar/python-and-network-automation-how-to-part-1-basic-show-commands)** for a list of supported devices in the Netmiko library.

---

<h3>Recap of what we have so far</h3>

Just to recap, this is what we have in our script so far:

```
#!/usr/bin/env python

from __future__ import print_function

import netmiko
from getpass import getpass

connection = netmiko.ConnectHandler(ip='172.31.1.251',
                                    device_type='hp_procurve',
                                    username='admin',
                                    password=getpass('Enter a password for the switch:\n'))
print(connection)
result = connection.find_prompt()
result += connection.send_command('show version')
print(result)

connection.disconnect()
```
---
We're going to take what we have and modify it a bit to make it more modular and easier should we want to incorporate this into something in real life.  First before getting into the config section let's turn this setup into a Python class.  A class will let us follow an object oriented type approach where we can create an instance of the class we make and then call the methods we define to do our work.  We'll change our file to look something like the output below:

```
#!/usr/bin/env python

from __future__ import print_function

import netmiko
from getpass import getpass

class SteemitAutomate():

    def __init__(self):
        self.connection = netmiko.ConnectHandler(ip='172.31.1.251',
                                            device_type='hp_procurve',
                                            username='admin',
                                            password=getpass('Enter a password for the switch:\n'))

    def printConnection(self):
        print(self.connection)

    def showCommand(self):
        result = self.connection.find_prompt()
        result += self.connection.send_command('show version')
        print(result)

sa = SteemitAutomate()
sa.printConnection()
sa.showCommand()
sa.connection.disconnect()
```
---

Some of the sections we changed are described here
- **class SteemitAutomate()**  <- defines our class
- **def __init__** <- the constructor of the class, basically what creates the class when called.  This class also creates the connection to the switch
- **the 'self' declaration** <- this is to help define that variables you refer to belong to self, or the class itself
- **def printConnection(self)** <- a method to just print out connection info
- **def showCommand(self)** <- we moved the functionality of our show commands here, the connection object is inherited from the __init__ class
- **sa = SteemitAutomate()** <- outside the class definition, we create an instance of the class that has all the methods of the class too

We then call the methods by calling the object by name, **sa** in our case then following with a period "." then the name of the function.  Above is an example **sa.printConnection()** which will call the printConnection function of the SteemitAutomate class from the sa object.  Also at the end notice how we access the **disconnect** property of the Netmiko **connection** object through **sa**.  It's important to disconnect when complete.

So now we have a good framework down for our class, let's add a method to handle configuration commands.  We'll keep the config commands pretty static and next time we will add the ability to input a command after calling the script as well as select show or config command.

---

<h3>Adding the configuration Class Method</h3>

Add the following to the script, each field I will elaborate on below:

```
    def configCommand(self):
        changes = ['vlan 777 name Lucky7s']
        output = self.connection.send_config_set(changes)
        print(output)
```

- **def configCommand(self):**  <- defining the Method
- **changes = ['vlan 777 name Lucky7s']** <- this is a List of the changes to make, I'm adding a vlan and naming it
- **output = self.connection.send_config_set(changes)** <- we're calling the send_config_set of the connection object from Netmiko we made here, we assign it to a variable because we're going to print the output of what commands were sent too
- **print(output)** <- this will show you the commands that Netmiko ran against the switch

So now that we have that, lets use the new method and verify it adds the VLAN by changing our show command from **show version** to **show vlan** and then call the **showCommand** method before and after the change.   Our code in it's entirety is now below:

```
#!/usr/bin/env python

from __future__ import print_function

import netmiko
from getpass import getpass

class SteemitAutomate():

    def __init__(self):
        self.connection = netmiko.ConnectHandler(ip='172.31.1.251',
                                            device_type='hp_procurve',
                                            username='admin',
                                            password=getpass('Enter a password for the switch:\n'))

    def printConnection(self):
        print(self.connection)

    def showCommand(self):
        result = self.connection.find_prompt()
        result += self.connection.send_command('show vlan')
        print(result)

    def configCommand(self):
        changes = ['vlan 777 name Lucky7s']
        output = self.connection.send_config_set(changes)
        print(output)

sa = SteemitAutomate()
sa.printConnection()
sa.showCommand()
sa.configCommand()
sa.showCommand()
sa.connection.disconnect()
```
---

<h3>Moment of Truth!</h3>
Time to run this thing and see what happens!  The output of me running it is below:

---
python steemit.py
Enter a password for the switch:

<netmiko.hp.hp_procurve_ssh.HPProcurveSSH object at 0x000002A2CE8E9908>
CH-2530#
 Status and Counters - VLAN Information

  Maximum VLANs to support : 256
  Primary VLAN : DEFAULT_VLAN
  Management VLAN :

  VLAN ID Name                             | Status     Voice Jumbo
  ------- -------------------------------- + ---------- ----- -----
  1       DEFAULT_VLAN                     | Port-based No    No


config term
CH-2530(config)# vlan 777 name Lucky7s
CH-2530(config)# end
CH-2530#
CH-2530#
 Status and Counters - VLAN Information

  Maximum VLANs to support : 256
  Primary VLAN : DEFAULT_VLAN
  Management VLAN :

  VLAN ID Name                             | Status     Voice Jumbo
  ------- -------------------------------- + ---------- ----- -----
  1       DEFAULT_VLAN                     | Port-based No    No
  777     Lucky7s                          | Port-based No    No

---

Success!! Now this may not seem like much, but wait until we get to running multiple commands against multiple devices at the same time!  

<h3>Wrapping Up</h3>

So let's finish this up for now with a quick recap of what we did and accomplished today:
1. Changed our Python file over to a Python class to make it more reusable in the future
2. Added a configuration class to take a single command and apply to our device
3. Created an instance of our class and tested our methods

Next time we will expand upon this to do more and more.  If you have anything you'd like to see let me know and I appreciate you reading this and the support.

<center>![](https://steemitimages.com/DQmbpofNspiFHDnstFuKDKtrfWTuHSQ9KANxHVHmGoTUEYg/image.png)</center>
👍  , , ,
properties (23)
authordcolthar
permlinkpython-and-network-automation-how-to-part-2-configuration-commands
categoryhowto
json_metadata{"tags":["howto","blog","tech","steem"],"image":["https://steemitimages.com/DQmXPFaY7d2mJrGmYwEPcvqHxokrjfV8FACDwF7UAak9ked/Capture.PNG","https://steemitimages.com/DQmbpofNspiFHDnstFuKDKtrfWTuHSQ9KANxHVHmGoTUEYg/image.png"],"links":["https://steemit.com/howto/@dcolthar/python-and-network-automation-how-to-part-1-basic-show-commands"],"app":"steemit/0.1","format":"markdown"}
created2017-07-08 05:23:12
last_update2017-07-08 05:23:12
depth0
children0
last_payout2017-07-15 05:23:12
cashout_time1969-12-31 23:59:59
total_payout_value0.070 HBD
curator_payout_value0.004 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length8,234
author_reputation31,870,289,985
root_title"Python and Network Automation HOW TO - Part 2 Configuration Commands"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id7,720,122
net_rshares17,207,831,177
author_curate_reward""
vote details (4)