Tuesday, June 25, 2013

Quickly Change Cisco Nexus Config via Run-Script and Sed

This is a tip that I picked up from my friend Shahin. The Cisco Nexus 3K line of products (Nexus 3064, 3048) includes the Python interpretor built-in, which is great. But the rest of the 7k and 5k product line do not (not sure about 2k and 1k). The following tip is a way to create scripts that live in bootflash, then use the 'run-script' command to execute them in a quick and easy fashion. If you feel adventurous, you can cron the job or do it via EEM while you have a beer at the bar. :)

Note that the below example I am using the infamous Titanium image, but it works on real production NX-OS as well for sure.

1. Eth2/1 has an IP address 192.168.1.1/24 that we will change to 192.168.200.1/24.



2. We make a script by using the 'echo' statement and write to the file in bootflash:


3. We can view the file via 'show file':


4. We use 'run-script' to execute:



5. Verify:



That was kind of fun, but what if we want to remove ALL ip address on ALL interfaces in one shot? We can also use the *Nix sed to modify the text stream:

1. Back to the 192.168.200.1/24 IP:








2. We can use sed to change string, note that it does not change the original string:






3. Now we can use the Cisco include command to include the string we want, then use sed to substitute the string. In this example I just include '192.168.200.1' but you can use 'include 'ip addresses' for example:










4. Now you see the interface:












5. Run the script:















6. Now you dont:












How cool is that? This is an absolute time saver in large scale environment. I wish Cisco includs *Nix tools in all of their platforms.

Happy coding!




13 comments:

  1. "This is an absolute time saver " - absolutely agree, thanks .

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete