Tuesday, June 23, 2009

Automation with Cisco NCM Command Scripts

One of the strongest features of Cisco NCM is the capability to create scripts to be executed against any number of devices. These scripts can be as simple as running a sequence of Cisco IOS commands or as complex as a multi-page Expect script. In this blog I'll show how easy it is to create an Expect script, within NCM.

For this example, let's assume we're trying to figure out a way to allow the Network Operations Center (NOC) to shut down interfaces on access switches, but not on distribution or core switches. Additionally, the NOC should not be able to be able to shut down the uplink ports located on ports fastethernet 0/23 and fastethernet 0/24.

NCM provides an easy way to get started with the script. This is done by creating a SSH session to the switch and executing the commands that would be executed in the command script. Then, the commands can be viewed and automatically converted to an Expect or Perl script. From there, the script can be customized to provide the logic to accomplish the task above. Let's walk through how each step is done.

1. Click on "Devices > Inventory".


2. In the resulting screen, click on the SSH button next to the device you would like to connect to


3. In the Java SSH window that appears, type in an example of the commands that would be used to shutdown an interface and exit.


4. Navigate back to the device by, once again, going to "Devices > Inventory". Click on the device name


5. In the resulting screen, click on "View > Telnet/SSH Sessions"


6. Click on "View Commands Only" to view the commands that you just entered


7. Click on "Convert to Expect Script" to automatically create a script from the commands entered.



8. At this point, an Expect script is created with the code needed to execute the commands entered previously


9. There a few places that need customization. First, the interface used in the script, fastethernet0/9 should be a variable that the NOC can define at execution time. This can be done by replacing fastethernet0/9 with a NCM variable that the NOC will be prompted to enter when they execute the script. To do this, replace fastethernet0/9 with $interface$. A string with $ at the beginning and end signifies an NCM variable.

send "interface fa0/9\r" -----------> send "interface $interface$\r"

10. When this is added, the "Pull Variables" button can be clicked to create the prompt that the NOC will see when they execute the script


11. This brings up another screen that requires information to be entered for the prompt


12. That's the basics of the script. The only thing left to do is add the restrictions for the script. First, the NOC should only be able to change access layer switches. The naming convention for the switches state that access switches start with "A". We can use this as a check to make sure an access switch is being used. Below is the corresponding Expect code snippet
if [string match "^A*" $enable_prompt] {
} else {
puts "\nThis is not an access layer switch\n"
exit 1
}
This snippet checks to see if the pre-defined $enable_prompt variable starts with an A. If so, it is an access layer switch. If not, an error message is displayed and the script is exited with error status.

13. The second check was to make sure fastethernet0/23 or fastethernet0/24 are not used. This is accomplished with the snippet below.
set protected_int {"1/6" "1/7"}
set int $interface$

set i 0
foreach i $protected_int {
if [string match "*$i" $int] {
puts "\nShutdown of uplink ports is not permitted\n"
exit 1
}
}
In this portion, the uplink interfaces are put into an array named "protect_int". The interface, that the NOC chooses, is stored in the $interface$ variable. A for loop checks to see if there is match between an uplink interface and the chosen interface. If so, an error message is sent and the scripted is exited.

14. When the script is created, the Expect command "log_user 1" is set. This means that whatever is sent in the script is also sent to the script output. In order to stop this from happening, set "log_user 0". With this set, only the "puts" output is displayed. In general, this is what you will want to see.

15. To run the script, click "Devices > Device Tools > Command Scripts"


16. Select "Run" on the script to execute.


17. Select the devices to run the script on, the interface to shutdown, and click "Save Task" to execute the script



That's all there is to creating a command script in NCM. I would highly recommend purchasing the "Exploring Expect" book written by Don Libes and published by O'Reilly. Additionally, I would recommend downloading ActiveTCL from Activestate.com. Expect is actually an extension of TCL. After installing TCL, you can load the Expect extension by entering "teacup install Expect" from a CMD prompt.

Saturday, June 20, 2009

Microsoft Swiss Army Toolkit

Every once in awhile there are instances where you need information about a Microsoft Windows XP computer that you think should be easy to find but seems impossible to uncover. The tool to find the information is a built-in command line tool call wmic.exe. This command has a slew of command line options that can uncover practically any information about your computer. Here are some examples.

Want to find out the OS version of your computer? Run "wmic os". The output shows up in columns with a ton of information. The version information show up as shown below
C:\>wmic os
Version
5.1.2600

Here's a list of all the command line parameters available
C:\> wmic /?

[global switches]

The following global switches are available:
/NAMESPACE Path for the namespace the alias operate against.
/ROLE Path for the role containing the alias definitions.
/NODE Servers the alias will operate against.
/IMPLEVEL Client impersonation level.
/AUTHLEVEL Client authentication level.
/LOCALE Language id the client should use.
/PRIVILEGES Enable or disable all privileges.
/TRACE Outputs debugging information to stderr.
/RECORD Logs all input commands and output.
/INTERACTIVE Sets or resets the interactive mode.
/FAILFAST Sets or resets the FailFast mode.
/USER User to be used during the session.
/PASSWORD Password to be used for session login.
/OUTPUT Specifies the mode for output redirection.
/APPEND Specifies the mode for output redirection.
/AGGREGATE Sets or resets aggregate mode.
/AUTHORITY Specifies the for the connection.
/?[:] Usage information.

For more information on a specific global switch, type: switch-name /?


The following alias/es are available in the current role:
ALIAS - Access to the aliases available on the local system
BASEBOARD - Base board (also known as a motherboard or system board) management.
BIOS - Basic input/output services (BIOS) management.
BOOTCONFIG - Boot configuration management.
CDROM - CD-ROM management.
COMPUTERSYSTEM - Computer system management.
CPU - CPU management.
CSPRODUCT - Computer system product information from SMBIOS.
DATAFILE - DataFile Management.
DCOMAPP - DCOM Application management.
DESKTOP - User's Desktop management.
DESKTOPMONITOR - Desktop Monitor management.
DEVICEMEMORYADDRESS - Device memory addresses management.
DISKDRIVE - Physical disk drive management.
DISKQUOTA - Disk space usage for NTFS volumes.
DMACHANNEL - Direct memory access (DMA) channel management.
ENVIRONMENT - System environment settings management.
FSDIR - Filesystem directory entry management.
GROUP - Group account management.
IDECONTROLLER - IDE Controller management.
IRQ - Interrupt request line (IRQ) management.
JOB - Provides access to the jobs scheduled using the schedule service.
LOADORDER - Management of system services that define execution dependencies.
LOGICALDISK - Local storage device management.
LOGON - LOGON Sessions.
MEMCACHE - Cache memory management.
MEMLOGICAL - System memory management (configuration layout and availability of memory).
MEMPHYSICAL - Computer system's physical memory management.
NETCLIENT - Network Client management.
NETLOGIN - Network login information (of a particular user) management.
NETPROTOCOL - Protocols (and their network characteristics) management.
NETUSE - Active network connection management.
NIC - Network Interface Controller (NIC) management.
NICCONFIG - Network adapter management.
NTDOMAIN - NT Domain management.
NTEVENT - Entries in the NT Event Log.
NTEVENTLOG - NT eventlog file management.
ONBOARDDEVICE - Management of common adapter devices built into the motherboard (system board).
OS - Installed Operating System/s management.
PAGEFILE - Virtual memory file swapping management.
PAGEFILESET - Page file settings management.
PARTITION - Management of partitioned areas of a physical disk.
PORT - I/O port management.
PORTCONNECTOR - Physical connection ports management.
PRINTER - Printer device management.
PRINTERCONFIG - Printer device configuration management.
PRINTJOB - Print job management.
PROCESS - Process management.
PRODUCT - Installation package task management.
QFE - Quick Fix Engineering.
QUOTASETTING - Setting information for disk quotas on a volume.
RECOVEROS - Information that will be gathered from memory when the operating system fails.
REGISTRY - Computer system registry management.
SCSICONTROLLER - SCSI Controller management.
SERVER - Server information management.
SERVICE - Service application management.
SHARE - Shared resource management.
SOFTWAREELEMENT - Management of the elements of a software product installed on a system.
SOFTWAREFEATURE - Management of software product subsets of SoftwareElement.
SOUNDDEV - Sound Device management.
STARTUP - Management of commands that run automatically when users log onto the computer system.
SYSACCOUNT - System account management.
SYSDRIVER - Management of the system driver for a base service.
SYSTEMENCLOSURE - Physical system enclosure management.
SYSTEMSLOT - Management of physical connection points including ports, slots and peripherals, and proprietary connections points.
TAPEDRIVE - Tape drive management.
TEMPERATURE - Data management of a temperature sensor (electronic thermometer).
TIMEZONE - Time zone data management.
UPS - Uninterruptible power supply (UPS) management.
USERACCOUNT - User account management.
VOLTAGE - Voltage sensor (electronic voltmeter) data management.
VOLUMEQUOTASETTING - Associates the disk quota setting with a specific disk volume.
WMISET - WMI service operational parameters management.

For more information on a specific alias, type: alias /?

CLASS - Escapes to full WMI schema.
PATH - Escapes to full WMI object paths.
CONTEXT - Displays the state of all the global switches.
QUIT/EXIT - Exits the program.

For more information on CLASS/PATH/CONTEXT, type: (CLASS | PATH | CONTEXT) /?