Using Expect Scripting for Cisco Devices

////// Code was written by: Adeolu Owokade

#!/bin/sh
# \
exec tclsh “$0″ ${1+”$@”}
package require Expect
# ###########################################################
# This simple Expect script will login into Cisco IOS devices
# and execute SNMP commands
# ###########################################################

# Define variables
set username “cisco”
set password “cisco”

# Define all the devices to be configured (separated by spaces)
set devices “192.168.56.100 192.168.12.2”

# Main loop
foreach device $devices {
puts “Processing device: $device”;

# Open a telnet session to device. I use Plink.exe to telnet
# because I find that normal telnet may not work or in case
# you don’t have telnet enabled on your system. Plink is the
# command line client of Putty. You can download plink from:
# http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
spawn plink -telnet $device

# Perform authentication to login into device
# My device configuration does not require enable password
# so I will be placed in privilege EXEC mode (#) not USER mode (>)
expect “Username:”
send “$username\r”

expect “Password:”
send “$password\r”
expect “#”

# Enter global configuration mode
send “conf t\r”
expect “(config)#”

# Send SNMP configuration
send “snmp-server community SNMP_COMM\r”
expect “(config)#”
send “snmp-server host 192.168.56.2 SNMP_COMM\r”
expect “(config)#”

# Return to privilege EXEC mode
send “exit\r”
expect “#”

# Exit Telnet session
send “exit\r”
expect eof

}

source: http://resources.intenseschool.com/using-expect-scripting-language-to-automate-cisco-configuration-tasks/

This entry was posted in Cisco R&S. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s