Digging deep into Unified Communications from Spain.
Header

Executing commands automatically into CUCM CLI

June 10th, 2012 | Posted by Jaime Diez in Cisco | Unified Communications

Nowadays any way to save some time it is always welcome, indeed if it is a repetitive task. Because of that, I have written a script based on Expect, which “talks” with other interactive programs through a script. In my case I have used it with a SSH connection against the CUCM CLI.

The Expect package, is available just for Linux. I am using a Windows machine where I have other admin tools, so I installed it together with Cygwin.

The script that I want to share with you, connect us to the CUCM CLI and it executes the commands that we have implemented. For instance, with the script below I am able to restart one node just typing a user, a password and an IP. You can do whatever you can imagine.

Run day-to-day tasks into CUCM in a few seconds

#!/usr/bin/expect -f
#Expect script to execute commands into CUCM CLI.
#------------------------------------------------------
#How to use it: ./nameofscript.exp youruser yourpassword youripaddress
#Example: ./restartCUCM.exp admin 12345 127.0.0.1
#------------------------------------------------------
#This script is provided without warranty of any kind.
#------------------------------------------------------
#Author: Jaime Diez, http://jaime-diez.net

#set variables
set user [lindex $argv 0]
set password [lindex $argv 1]
set ipaddress [lindex $argv 2]
set timeout 10

#it connects to the server
spawn ssh -o StrictHostKeyChecking=no $user@$ipaddress
match_max 100000

#prompt for the password
expect "password:"
send -- "$password\r"

#it executes the command required
expect "admin:"
send -- "utils system restart\r"

#it confirms the restart
expect "(yes/no)?"
send -- "yes"

#it allows you to see what happens after the script is executed
interact

It will save you time of course, but what happen if we are working in a cluster? Execute this script one by one will be also a tedious task so I have written another script to read the hosts from a file. Just fill the file with the hosts you need to work with. I am assuming that the user and password is the same for each node.

Shutdown multiple CUCM nodes

#!/usr/bin/expect -f
#Expect script to execute commands via SSH to multiple servers.
#------------------------------------------------------
#Create a file called host.list with one host per line. Save it in the same directory.
#Run the script ./nameofscript.exp youruser yourpassword
#Example: ./shutdownCUCMcluster.exp admin 12345
#------------------------------------------------------
#This script is provided without warranty of any kind.
#------------------------------------------------------
#Author: Jaime Diez, http://jaime-diez.net

#set variables
set user [lindex $argv 0]
set password [lindex $argv 1]
set timeout 30
set serverlist [open "./host.list" r]
set readhost [read -nonewline $serverlist]
close $serverlist

foreach host [split $readhost "\n"] {

	#it connects to the server
	spawn ssh -o StrictHostKeyChecking=no $user@$host
	match_max 100000

	#prompt for the password
	expect "password:"
	send -- "$password\r"

	#it executes the command required
	expect "admin:"
	send -- "utils system shutdown\r"

	#it confirms the shutdown
	expect "(yes/no)?"
	send -- "yes\r"
}

With a few changes, you will be able to execute any other commands required or connect to any other type of servers through SSH, Telnet, SFTP… Any type of repetitive or heavy tasks can be accomplished in a few seconds.

One of the most useful for my day-to-day tasks, is the script which allows me to shutdown everything in my lab, VMs, ESXi server, pfSense… The limit is your mind!

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

Leave a Reply

Your email address will not be published. Required fields are marked *


*