cancel
Showing results for 
Search instead for 
Did you mean: 
cancel

Automate the UCS CLI with expect

2353
Views
0
Helpful
0
Comments
Advisor
This blog was generated from CDN blog

Created by: John McDonough on 11-05-2011 10:05:50 AM
Sometimes an API is not able to do everything that is needed by administrators. Occasionally there may be a task that needs to be performed on a regular basis or an as needed basis.  While the CLI is not the API there are ways to automate the CLI.

The UCS Fabric Interconnects have the ability to run a script using the run-script command, however there are no script creation capabilities. Scripts need to be edited then copied on to the Fabric Interconnect to be run.  Additionally the script has no logic or flow capabilities, it is strictly command after command.

There is a better way (not as good as the API but good when the API isn't available), it is called "expect". expect is a component of TCL, and it allows you to automate CLI tasks by simulating keyboard input.  The results from a command can be captured and a decision on what to do next can be based on if the results are what was expected or something else.

Typically when an issue is encountered with the UCS and a call is made to Cisco tech support, the first request from tech support is that the customer runs a "show tech-support" command and send the resulting file to Cisco. There are several options for a "show tech-support"

  • show tech-support ucsm
  • show tech-support chassis
  • show tech-support fex
  • show tech-support server

Additionally those options may have sub-options.  If the command completes successfully a file will have been generated and placed in the techsupport sub-directory of the workspace file system. The next step in the process is to copy the file from the Fabric Interconnect using one of the four file transfer protocols.

  • ftp
  • scp
  • sftp
  • tftp

After the file has been transfered it would be a good idea to remove it from the workspace:/techsupport directory. Wouldn't it be great to have a script that would do this all for you?

Using expect this operation can be scripted, but not just a sequence of commands, but commands wrapped in logic, error detection and validation. The script presented below does these things, not exhaustively, but with a bit of each.

  1
  2#!/usr/bin/expect --
  3#
  4# John McDonough (jomcdono)
  5#
  6# This script is presneted as an example, Cisco assumes no liability
  7# use at your own risk
  8#
  9
 10set timeout -1
 11set ucsORch [lindex $argv 0]
 12set ucsUser [lindex $argv 1]
 13set ucsPass [lindex $argv 2]
 14set ucsHost [lindex $argv 3]
 15set xfrUser [lindex $argv 4]
 16set xfrPass [lindex $argv 5]
 17set xfrHost [lindex $argv 6]
 18set xfrPath [lindex $argv 7]
 19set xfrProtocol [lindex $argv 8]
 20set delFile [lindex $argv 9]
 21
 22#check if all were provided
 23if { $ucsUser == "" || $ucsPass == "" || $ucsHost == "" || $xfrUser == "" || $xfrPass == "" || $xfrHost == "" || $xfrPath == "" || $xfrProtocol == ""}  {
 24  puts "\n   Usage: $argv0 <ucsm, chassis-#, fex-#, or server-# > <UCS User> <UCS Pass> <UCS Host> <Dest User> <Dest Pass> <Dest Host> <Dest Path> <Dest Protocol> \[clean\]\n"
 25  puts "   Where:\n"
 26  puts "         ucsm, chassis-#, fex-# or server-#"
 27  puts "              ucsm       means - show tech-support ucsm detail"
 28  puts "              chassis-4  means - show tech-support chassis 4 all detail\n\n"
 29  puts "              fex-5      means - show tech-support fex 5 detail\n\n"
 30  puts "              server-10  means - show tech-support server 10 detail\n\n"
 31  puts "         UCS User - UCS user name"
 32  puts "         UCS Pass - UCS user password"
 33  puts "         UCS Host - UCS host\n"
 34  puts "         Dest User     - File transfer protocol user name"
 35  puts "         Dest Pass     - File transfer protocol user password"
 36  puts "         Dest Host     - File transfer protocol destination host"
 37  puts "         Dest Path     - File transfer protocol destination path, fully qualified\n"
 38  puts "         Dest Protocol - File transfer protocol <ftp|scp|sftp>\n"
 39  puts "         clean - If the last argument is the word clean then delete the file\n"
 40
 41  exit 1
 42}
 43
 44# Open and ssh connection to UCS Manager
 45spawn ssh $ucsUser@$ucsHost
 46expect {
 47   "Are you sure you want to continue connecting*" {
 48      send "yes\r"
 49      expect "assword:"
 50      send "$ucsPass\r"
 51   }
 52   "Password:" {
 53      send "$ucsPass\r"
 54   }
 55}
 56expect "# "
 57
 58# Connect to the local-mgmt context
 59send "connect local-mgmt\r"
 60expect "(local-mgmt)#"
 61
 62# Execute show tech-support detail for either ucsm or a specific chassis
 63if {$ucsORch == "ucsm"} {
 64  send "show tech-support ucsm detail | no-more\r"
 65} else {
 66  set tmpComponent [lindex [split $ucsORch "-"] 0]
 67  set tmpComponentNumber [lindex [split $ucsORch "-"] 1]
 68
 69  set tmpComponentCoverage ""
 70  if {$tmpComponent == "chassis"} {
 71    set tmpComponentCoverage "all"
 72  }
 73  send "show tech-support $tmpComponent $tmpComponentNumber $tmpComponentCoverage detail | no-more\r"
 74}
 75
 76# Wait for the string below that indicates the command is complete and extract the filename
 77# from the string to be used in the file transfer.  Because the string may wrap to the next
 78# line join the first two lines of the output buffer before using the split command.
 79expect "The detailed tech-support information is located at workspace*tar"
 80set tmpStr1 [lindex [split $expect_out(0,string) "/"] 4]
 81regsub -all {\s} [join $tmpStr1] {} tmpStr2
 82set xfrFile [string trim $tmpStr2 "\n"]
 83expect "(local-mgmt)#"
 84
 85# Execute the UCSM copy command to copy the techsupport file to a destination
 86send "copy workspace:techsupport/$xfrFile $xfrProtocol://$xfrUser@$xfrHost/$xfrPath\r"
 87
 88# Wait for the password prompt, the RSA fingerprint might need to be added to
 89# the ssh host keys
 90expect {
 91   "Are you sure you want to continue connecting*" {
 92      send "yes\r"
 93      expect "assword:"
 94      send "$xfrPass\r"
 95   }
 96   "Password:" {
 97      send "$xfrPass\r"
 98   }
 99}
100
101expect "(local-mgmt)#"
102
103# Remove the tech-support file if requested
104if {$delFile == "clean"} {
105    send "delete file workspace:techsupport/$xfrFile\r"
106    expect "(local-mgmt)#"
107}
108
109# output status message
110puts "\nshow tech-support operation complete check this script output for errors\n"
111
112# Finished
113exit 0



Line 1 indicates expect should be the interpreter and the double dash means the following lines are the input
Line 9 sets the timeout for expect to be indefinite
Lines 10-19 puts the command line positional parameters into variables
Line 22 checks if all the required parameters have been supplied
Lines 23-40 print out the usage message and exit, if the required parameters are not present
Line 44 create ans ssh session
Lines 45-54 "expect" one of two responses either the ssh unknown host warning or the password prompt
You'll noticed that I dropped the "P" in password that's because some systems respond with a capital P and some with a lowercase p. This code is not strictly for UCS Fabric Interconnects, so even though I know the response would be password: I expect a little less then password that way I know this piece of code could work with other systems.

Did you also notice that I have an expect statement embedded in my expect statement?

Line 55 expect that "# " prompt which means the script has connected to the Fabric Interconnect
Line 58 send the command to connect to the local-mgmt context
Lines 62-71 determine which show tech-support command to issue based on the input parameters
Line 72 send the command to run the show tech-support
Line 78 wait for the show tech-support to complete
Lines 79-81 extract the filename of the show tech-support command completion
Line 85 send the copy command to transfer the file, this script only supports ftp, scp and sftp
Lines 87-98 the [P|p]assword prompt again
Line 100 expect the Fabric Interconnect prompt
Lines 103-106 send a delete command if the clean parameter was specified on the command line
Line 109 print a status message
Line 112 exit


Is there room for improvement? Better validation? Send an email? Etc... sure. Below is an example of running the script for fex 3


jomcdono-mac:~ jomcdono$ ./ucs-tech-support.sh fex-3 admin password 10.10.10.10 jomcdono mypassword 192.168.1.100 /Users/jomcdono/Desktop sftp clean
spawn ssh admin@10.10.10.10
Cisco UCS 6100 Series Fabric Interconnect
Password:
Cisco Nexus Operating System (NX-OS) Software
TAC support: http://www.cisco.com/tac
Copyright (c) 2002-2010, Cisco Systems, Inc. All rights reserved.
The copyrights to certain works contained in this software are
owned by other third parties and used and distributed under
license. Certain components of this software are licensed under
the GNU General Public License (GPL) version 2.0 or the GNU
Lesser General Public License (LGPL) Version 2.1. A copy of each
such license is available at
http://www.opensource.org/licenses/gpl-2.0.php and
http://www.opensource.org/licenses/lgpl-2.1.php

g05-sw-A# connect local-mgmt
Cisco Nexus Operating System (NX-OS) Software
TAC support: http://www.cisco.com/tac
Copyright (c) 2002-2010, Cisco Systems, Inc. All rights reserved.
The copyrights to certain works contained in this software are
owned by other third parties and used and distributed under
license. Certain components of this software are licensed under
the GNU General Public License (GPL) version 2.0 or the GNU
Lesser General Public License (LGPL) Version 2.1. A copy of each
such license is available at
http://www.opensource.org/licenses/gpl-2.0.php and
http://www.opensource.org/licenses/lgpl-2.1.php

g05-sw-A(local-mgmt)# show tech-support fex 3 detail | no-more
Initiating tech-support information task on Fabric Extender 3 from A ...
Completed initiating tech-support subsystem tasks (Total: 1)
All tech-support subsystem tasks are completed (Total: 1)

The detailed tech-support information is located at workspace:///techsupport/20110511084825_g05-sw_BC_IOCard03.tar
g05-sw-A(local-mgmt)# copy workspace:techsupport/20110511084825_g05-sw_BC_IOCard03.tar sftp://jomcdono@192.168.1.100//Users/jomcdono/Desktop
WARNING!!! READ THIS BEFORE ATTEMPTING TO LOGON

This System is for the use of authorized users only. Individuals using this computer without authority, or in excess of their authority, are subject to having all of their activities on this system monitored and recorded by system personnel.

In the course of monitoring individuals improperly using this system, or in the course of system maintenance, the activities of authorized users may also be monitored. Anyone using this system expressly consents to such monitoring and is advised that if such monitoring reveals possible criminal activity, system personnel may provide the evidence of such monitoring to law enforcement officials.

Cisco Acceptable Use Policy:
http://wwwin.cisco.com/infosec/policies/acceptable_use.shtml
Password:
Connected to 192.168.1.100.
sftp> put /workspace/techsupport/20110511084825_g05-sw_BC_IOCard03.tar "//Users/jomcdono/Desktop"
Uploading /workspace/techsupport/20110511084825_g05-sw_BC_IOCard03.tar to //Users/jomcdono/Desktop/20110511084825_g05-sw_BC_IOCard03.tar
/workspace/techsupport/20110511084825_g05-sw_ 100% 290KB 145.0KB/s 00:02
sftp> quit
g05-sw-A(local-mgmt)# delete file workspace:techsupport/20110511084825_g05-sw_BC_IOCard03.tar
g05-sw-A(local-mgmt)#
show tech-support operation complete check this script output for errors


UCS Manager GUI 1.4 has added the ability to generate and download a Tech Support to your local file system, so this is achievable through the UCS XML API

John McDonough
Cisco Advanced Services
CreatePlease to create content
This widget could not be displayed.