Python to Manage Network Devices (1): Linux lab setup

Introduction

I introduced how to use PowerShell/PowerTool to automate Cisco UCS servers via UCS Manager in July 2016. Time to play with something new in the new month, using Python to manage network devices.

Both Cisco and Juniper has Python library, Cisco confparse and Juniper PyEZ. We don’t have to install python on Linux. I used to install PyEZ on Windows with PyCharm as IDE and install SSH plugin. The beauty of Linux is…it is free and light in resource usage. In addition, Cisco appliances and OS such as Prime and IOS-XE are running on Linux platform so does JUNOS; though those are considered as ‘closed system’ by vendors, which means no patching on the underline Linux is required. Anyway…what I am trying to say is Linux is useful.

Continue reading

Advertisements

PowerShell to Automate Cisco UCSM (4): PS with .Net to develop UI and provision service profile

Introduction

PowerShell to Automate UCS (1): Lab Setup introduced the UCSM and PowerTool lab setup.

PowerShell to Automate UCS (2): PowerGUI Editor introduced how to use PowerGUI as PowerShell script editor to simplify script development test; and also script example.

PowerShell to Automate UCS (3): Convert UCSM GUI to Script introduced how to start from 0 and write PowerShell script to manage UCSM by converting UCSM GUI to script.

This blog demonstrates how to integrate PowerShell and .Net to develop a user interface to check whether a server is in use, then select unused server and provision service profile from the established service profile template.

Continue reading

PowerShell to Automate Cisco UCSM (3): Convert UCSM GUI to Script

Introduction

PowerShell to Automate UCS (1): Lab Setup introduced the UCSM and PowerTool lab setup.

PowerShell to Automate UCS (2): PowerGUI Editor introduced how to use PowerGUI as PowerShell script editor to simplify script development test; and also script example.

This blog is to demonstrate how to develop PowerShell script to manage UCSM if you are new to PowerShell and have UCSM configuration experience.

Cisco PowerTool provides the function to convert UCSM GUI to PowerShell commands. Basically we just need to configure an example from UCSM GUI interface, the actions will be captured and output as PowerShell commands in ‘Cisco UCS Manager PowerTool’ application window.

The auto script generation works fine most of time, though it may provide too many attributes than necessary. After gaining some experience with PowerShell/PowerTool, it should be pretty easy to tailor the generated script. It seems Cisco finally provides some kinda of user-friendly product…I think Cisco try to make things difficult to protect its profitable certificate system ;p

Continue reading

PowerShell to Automate Cisco UCSM (2): PowerGUI Editor

Introduction

I introduced how to set up the UCSM automation lab and used PowerTool, the Cisco PowerShell module, to connect and manage UCSM (see PowerShell to Automate UCS (1): Lab Setup).

This blog is to introduce my favourite PowerShell editor, PowerGUI, which provides an intuitive graphic interface, simplifies scripting work and allows easy test and troubleshooting. Most importantly it is free!

Download PowerGUI from DELL (It is now owned by DELL): http://software.dell.com/products/powergui-freeware/

PowerGUI 3.8 is used in this lab. The lab steps are as below:

Continue reading

PowerShell to Automate Cisco UCSM (1): Lab Setup

Introduction

Powershell is not a tool limited to Windows world any more. Cisco offers its own PowerShell module, PowerTool, to manage UCS. In this lab, PowerTool is to be installed on a Windows machine, which has accessibility to UCS manager. UCS Platform Emulator (UCSPE) is installed to simulate UCSM including a Fabric Interconnect pair (FI-A and FI-B) and UCS-B servers.

PowerTool can also be integrated with UCS Director (UCSD), the Cisco automation and orchestration tool. UCSD can call PowerShell Agent (PSA) server remotely and provide a GUI service catalog to users. In addition, VMWare also has its own PowerShell module. AWS serves another example. I may write blogs on those usage in future, but they are out of scope of the current series.

Continue reading

Home-made Web GUI Configuration Generator

A friend asked me about configuration generator last week, which reminded me to review the topic of home-made configuration generator 🙂

I used Excel and Powershell to generate configuration before. I tried to make a Web GUI Configuration Generator this time, using PHP index and trigger pre-defined XML templates. index.php also handles output. Example and steps are as below:

1) Build a web server

I am using macbook and simply enable Apache and PHP support.

How to build local web server on mac: https://discussions.apple.com/docs/DOC-3083

Another good reference: http://osxdaily.com/2012/09/10/enable-php-apache-mac-os-x/

(Your mac may support nano editor by native. ‘nano’ is much more convenient than ‘vi’ to edit files)

2) Save index.php and XML templates to web server root folder

File can be downloaded from (I found template from GitHub and modified. Please read original author declaration before use): 

https://dl.dropboxusercontent.com/u/36482628/WordPress/Web_Config_Generator.zip

Test access to index.php file. It supports the selection of multiple templates.

web_gui1

3) Modify templetes

tpl is xml template file, it can be open by any text editor. All variables are defined and consumed in the template file in consistent to xml syntax. The example template uses the following variable types.

  • ‘simple’ question type: simple text variable
  • ‘switch’ question type: dropdown list variable
  • ‘checkbox’ question type: checkbox variable

I made an example template for SVI HSRP configuration generation. It can be downloaded from  the previous dropbox link or content at the end of the blog. It is a simple use case and doesn’t include variable manipulation such as calculation.

Upon selecting the template to use, the following page shows:

web_gui2

Upon clicking submit, the following output result shows:

web_gui3.png

1_VLAN_CREATION.tpl

<xml>
<!–

Simple question type, will create a text input, and replace the match with what was entered

shortname = value that will be used in “NAME” attribut of form element to identify this question
description = Text shown on form to describe desired input.
–>
<question type=”simple” shortname=”vlan_id” description=”VLAN ID”>
<!– Whereever this string is found in template (Surrounded by [% and %]) we will
replace it with the text entered in the form –>
<match>VLANID</match>
</question>
<question type=”simple” shortname=”vlan_ip1″ description=”Primary SVI IP”>
<!– Whereever this string is found in template (Surrounded by [% and %]) we will
replace it with the text entered in the form –>
<match>SVIIP1</match>
</question>

<question type=”simple” shortname=”vlan_ip2″ description=”Secondary SVI IP”>
<!– Whereever this string is found in template (Surrounded by [% and %]) we will
replace it with the text entered in the form –>
<match>SVIIP2</match>
</question>

<question type=”simple” shortname=”vlan_vip” description=”VLAN VIP”>
<!– Whereever this string is found in template (Surrounded by [% and %]) we will
replace it with the text entered in the form –>
<match>VLANVIP</match>
</question>

<question type=”simple” shortname=”IP_mask” description=”IP MASK”>
<!– Whereever this string is found in template (Surrounded by [% and %]) we will
replace it with the text entered in the form –>
<match>IPMASK</match>
</question>

<!–

Question type switch, will create a drop down, and let you choose between several options

shortname = value that will be used in “NAME” attribut of form element to identify this question
description = Text shown on form to describe desired input.
–>
<question type=”switch” shortname=”vrf” description=”SELECT VRF”>
<!–
Define’s an availible option in the dropdown, shortname and description like for questions.
–>
<option shortname=”red” description=”RED VRF”>
<!–
If this option is selected in the form, we will go through each block, and replace the string matched
in the match element (surrounded by [% and %], with the contents of the replace element.
–>
<block>
<match>VRF</match>
<replace>RED_VRF</replace>
</block>
<block>
</block>
</option>
<option shortname=”green” description=”GREEN VRF”>
<block>
<match>VRF</match>
<replace>GREEN_VRF</replace>
</block>
<block>
</block>
</option>
</question>

<!–
Question type Checkbox, if checked will replace string in match element (Surrounded by [% and %]) with
contents of replace element. If not checked, will replace with nothing (will remove string in match element
from template
–>
<question type=”checkbox” shortname=”preempt” description=”HSRP PREEMPT”>
<match>PREEMPT</match>
<replace>preempt</replace>
</question>
</xml>
–BEGIN-TEMPLATE–

Core 1
interface vlan [% VLANID %]
ip vrf forwarding [% VRF %]
ip address [% SVIIP1 %] [% IPMASK %]
standby [% VLANID %] ip [% VLANVIP %]
standby [% VLANID %] priority 150
standby [% VLANID %] [% PREEMPT %]

*************************************************************

Core 2
interface vlan [% VLANID %]
ip vrf forwarding [% VRF %]
ip address [% SVIIP2 %] [% IPMASK %]
standby [% VLANID %] ip [% VLANVIP %]
standby [% VLANID %] [% PREEMPT %]