Getting information from your network devices is really helpful, but actually change device configurations is even more helpful! This post will have a few examples of how to do just that with scripts that will shutdown a specified interface and change an interface IP address. This is where the fun begins, so strap into your chairs and get ready for some network automation!

Shutting down an Interface

This first example’s goal is to shut down an interface programmatically. In order to specify an interface, we’ll search the interfaces for a passed in IP address.

When we run this script from the command line, this is the output:

Ok, let’s break this down now. We use really similar code to an example in the previous post that will get the IP addresses for each interface. We iterate through each interface looking for the IP addresses specified, and apply the .shut_down() method to it. You can see in the full example here, if a value of 1 (shutdown) or 0 (enable) is passed into the .shut_down() method, that value is passed to the method, otherwise we just shutdown the interface with a value of 1.

Changing an Interface IP Address

If you’re going through an IP address range migration this next example could be very valuable to shave hours off the update process. Let’s look at what it takes to change and Interface IP address programmatically:

It looks like there’s a lot going on, but there are really just a few simple steps:

  • Connect to the IOS device
  • Set the IP address and prefix values (this can be done through user input as well).
  • Test the IP address to make sure it is valid.
  • Set the address on the interface we specified by name.
  • Print the output and disconnect.

The only new object here is the OnepAddressScopeType, you can read about the possible scope types here. Also, you can see more options when using the .set_address() method of the NetworkInterface object here.

To see how this example can be made into a CLI script which takes all the values in as arguments, check out the full example in my OnePK GitHub repo here.

Saving the Network Element Configuration

Being able to make these changes via script is great for bulk changes, but remember that a change is only saved in running_configuration until it’s written to the NVRAM of the IOS device. These changes above would be lost with a reload or power off. Luckily though, the OnePK library includes a VtyHelper class to add some config saving abilities to our script:

The Vty module in OnePK has plenty of other options for running commands and receiving output via the CLI if the other OnePK classes don’t offer what you need. I’ll go more in depth in a later post.

This was just an introduction to making configuration changes on an IOS device. The next post will cover more interface config changes like applying a VLAN or adding ACLs. I know, I’m just as excited as you are! Thanks for reading and check back soon.

Series Navigation<< OnePK – Pulling information from an IOS deviceOnePK – Configuring and Assigning an ACL >>

This post was originally published on

Leave a Reply

Your email address will not be published. Required fields are marked *