Cisco UCS Scripts – Tips Tools and Hexadecimal Math

October 24, 2012 - 1 Comment

As a homeowner I found that I am regularly involved in some sort of home project.  Typically that project will require a tool that I do not own, cannot borrow and usually wouldn’t rent because the rental would cost more than the entry level version of the tool that I may only use once.

Years ago my friend gave me this advice “Never buy cheap tools; it is almost always more frustration than the money you save.” However a while back I wrote a blog that was all about free tools that I use for developing code that interacts with Cisco UCS.

With software, there are some really great free tools out there and a little time spent checking out free stuff can yield goodness. This blog is a few tidbits of free tools and suggestions for your development efforts.

First up, autohotkey, what is it you ask? Let’s examine a scenario that comes up pretty regularly in UCS Server management, KVM access, where’s the Cut-n-Paste? You cannot paste into the KVM but AutoHotKey will emulate keystrokes and mouse clicks. Additionally AutoHotKey will locate windows by title and bring them to the foreground, has programmatic logic capabilities, and can be compiled into distributable scripts as execuatables.

For example, suppose you need to login to your ESX servers to get VMFS information, take a screen and save that screenshot in a file. You’re going to login, cd to /vmfs/volumes, ls -l , send a print screen, switch to your word document and paste the screen shot. Your ESX servers first need to be sent an Alt-F1 to get to the login screen and you want to put them back the way you found them so you’ll have to send an Alt-F11 when you’re done.

This AutoHotKey script is kicked off when you press Ctrl-n, it will Activate the Window titled Blade-1-7,  send an Alt-F1, login, change to the /vmfs/volumes directory , issue an ls -l command, take a screen shot, exit from ESX, send an Alt-F11, switch to the document titled VMFS-ScreenShots.docx – Microsoft Word and finally paste the screen shot.

WinActivate, Blade-1-7
Send {Alt down}{F1}{Alt up}root{enter}Cisco{!}123{enter}
Sleep 2000
Send cd /vmfs/volumes{enter}ls -l{enter}
Sleep 2000
Send {Alt down}{PrintScreen}{Alt up}
sleep 2000
Send exit{enter}
Sleep 2000
Send {Alt down}{F11}{F11}{Alt up}
WinActivate, VMFS-ScreenShots.docx – Microsoft Word
Send {Control down}v{Control up}

I used the UCS PowerTool “Start-UcsKvmSession -Blade 1/7 -FrameTitle Blade-1-7” to start the KVM session and give the window a title AutoHotKey could find. Since AutoHotKey supports variables, loops, delays and all kinds of scripting stuff, it is quite possible to have AutoHotKey start the KVM session wait until the window is active, process the rest of the script, shutdown the KVM session and then move on the next server.

Next up, TCL, you’ve read my blogs about expect. If you have not then you’re missing a whole “API” ish interface into your Fabric Interconnects, as well as your Nexus, MDS, and any other ssh/telnet host interfaces that are in your infrastructure. expect is usually packed as a component of TCL on your Linux hosts, but on Windows you’ll need to get something like Active TCL or Cygwin.

Now for something from me, Provisioning UCS from a Spreadsheet using UCS PowerTool (new realse .9.10.1 just came out, major improvements and additional functionality).  The spreadsheet has tabs for each tab in UCS Manager and I devised a system where the first column is the component/thing that I want to configure and the next 50 columns are places for options for the cmdlets that I’ll be running to configure that component.

The script takes as one of the arguments a configuration spreadsheet name that it will read in and create CSV files for each tab in the spreadsheet. If a sheets flag is present on the command line then that sheet’s CSV file will be processed. There is also a -Clean option to remove a bunch of UCS configuration items. I would recommend trying this out on the latest release of the UCS Emulator (free by the way) to see how it works.

The script processes the sheets in this order “Admin”, “Equipment”, “LAN”, “SAN”, “Server”. I don’t have a VM tab in there I’ll leave that to you. Logically Orgs need to exist for almost all the other items so I put Org creation in the “Admin” tab. By the time you get to the “Servers” tab you’ll have a lot of the things you need for your UCS and certainly enough to create Service Profile Templates.  In fact the last thing the script does for the “Server” tab is instantiate the Service Profiles from the templates and in my example since the Service Profile templates are associated to Pools, association happens right away. It’s pretty awesome.  I’m happy to answer any questions about the script.


Last thing I’ll leave you with is some character based hexadecimal math, good for finding the size of UCS pools. Let’s look at a two MAC addresses returned from a call to Get-UcsMacMemberBlock, the MAC member block objects that are returned will have a From attribute and a To attribute. Subtract the From attribute from the To attribute add 1 and you have the decimal size of the block.

Here’s a MacMemberBlock object

From         : 00:25:B5:2B:00:00
To           : 00:25:B5:2B:00:1F
Dn           : org-root/org-Test/mac-pool-Test-MACPool_B/block-00:25:B5:2B:00:00-00:25:B5:2B:00:1F
Rn           : block-00:25:B5:2B:00:00-00:25:B5:2B:00:1F
Status       :
XtraProperty : {}
Ucs          : ucspe-172-16-206-152

Notice no size attribute, we all know it is 32 because I do HEX math in my head. Let’s try some MAC math

PS C:\> $macFrom = “00:25:B5:2B:00:00”
PS C:\> $macTo = “00:25:B5:2B:00:1F”
PS C:\> ($macTo – $macFrom) + 1

Bad numeric constant: 00:25:B5:2B:00:1F.
At line:1 char:10
+ ($macTo – <<<<  $macFrom) + 1
+ CategoryInfo          : InvalidOperation: (00:25:B5:2B:00:1F:String) [], RuntimeException
+ FullyQualifiedErrorId : BadNumericConstant

Ouch no good, but let’s do this, put a “0x” at the beginning of the string and replace all the “:” (colons) with “” (nothing) subtract them and add 1

PS C:\> $macFrom = “0x” + “00:25:B5:2B:00:00”.Replace(“:”,””)
PS C:\> $macTo = “0x” + “00:25:B5:2B:00:1F”.Replace(“:”,””)
PS C:\> ($macTo – $macFrom) + 1

Want to know the size of your pools, there you go, hope that helps.

What other free tools are useful? You tell me, how do you automate your UCS deployments and your day-to-day activities?  Is there a keyboard emulator for Linux that you use?


In an effort to keep conversations fresh, Cisco Blogs closes comments after 60 days. Please visit the Cisco Blogs hub page for the latest content.


  1. Great!
    Autokey is great suggestion. UCS Spreadsheet is will go out of sync, till need to find a better way of source control or versioning.