Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automating Cisco 4006 switch configuration

Status
Not open for further replies.

sasj0e

IS-IT--Management
Nov 6, 2002
82
Hi,
Is it possible to automate the process of configuring the Cisco catalyst 4006 with sup II?
For example...
writing a batch file in a windows system to perform a set of tasks.
Say
Telnet to Cisco catalyst.
1.entering loging password
2.entering "enable"
3.entering password
4. sh port
5.sh vlan
6.set vlan 3
.....etc one after the other.
Cant we do this by writing a batch file or similar..to perform the tasks one after the other..?
Pls let me know on this.
Regards,
Sas
 
It's very easy. You instruct the script to send a string then capture the results until a certain string. I have written a Python script that opens a list of cisco devices, issues a command and records the results in a file. There's no limit to what you can do as long as you follow the send/expect sequence.

-Jeff

#!/bin/python
# File: netconfig.py
# Author: Jeff Gercken
# Date: 1/28/2003
# Discription: Connects to network devices listed in devicefile. Outputs to a
# individual files results of 'show tech'. Folder=device, file is weekday
# devicefile format is name,ip,OS eg: s60-a-1 148.129.170.22 cls
#######################################################################################
# Changeable variables
command='show tech'
user = 'JG-Script' #change this to your username
password = 'I35kC23m' #change this to your password
directory='c:\\temp\\configs\\' #change this to where the devicefile is
errorlog=directory+'errors.txt'
devices=directory+'devicefile.txt' #change "filename" to match devicefile filename
#devices=directory+'test.txt'
#######################################################################################
import sys
import telnetlib
import string
import time
import os
cdate=time.strftime("%m-%d-%y", time.localtime()) #Get date in mm-dd-yy format
ctime=time.strftime("%X", time.localtime()) #Get time in hh:mm:ss format
day=time.strftime("%A", time.localtime()) #Get name of day Monday, Tuesday
#
devices=open(devices,'r')
for cdevice in devices.readlines():
cdevice=cdevice.split()
if cdevice==[] or cdevice==['\n']:continue # Quit if line is empty
print cdevice[0]
# Check for existance of subdirectory, create if necessary
if os.path.isdir(directory+cdevice[0]):pass
else:eek:s.mkdir(directory+cdevice[0])
fileout=open(directory+cdevice[0]+'//'+day+'.txt','w')
# Initialize output file with device name, ip, date, and time
fileout.write(cdevice[0]+' '+cdevice[1]+' '+cdate+' '+ctime+'\n\n')
try:tn = telnetlib.Telnet(cdevice[1]) #connect to device
except: #if error record in errorfile
print 'Error, Device '+cdevice[0]+' unreachable'
error=open(errorlog,'a',0)
error.write('\n'+cdate+' '+ctime+' Error, Device %s unreachable %s' %(cdevice[0],sys.exc_info()[0]))
error.close()
break
if cdevice[2]=='cls': #if device is switch use these commands
print "Device is a switch"
print "Downloading data...."
tn.read_until("Username: ")
tn.write(user+"\n")
tn.read_until("Password:")
tn.write(password+"\n")
time.sleep(2) #give the device time to authenticate
tn.write("enable\n")
tn.read_until("Password: ")
tn.write(password+"\n")
tn.read_until("(enable)",10)
# prevent pause in output
tn.write("set length 0\n")
tn.read_until("(enable)",10)
# prevent console messages in output
tn.write("set logging session disable\n")
tn.read_until("(enable)",5)
# Send 'sh tech' command to switch
tn.write(command+"\n")
output=tn.read_until("(enable)",10)
string.strip(output)
fileout.write(output)
fileout.close()
else: #assume anything else is a router or ios switch and use ios commands
print 'Device is a router or switch running IOS'
print "Downloading data...."
tn.read_until("name: ",5)
tn.write(user+"\n")
time.sleep(2)
tn.read_until("Password:",5)
tn.write(password+"\n")
time.sleep(2) #give the device time to authenticate
tn.write("enable\n")
tn.read_until("Password: ",5)
tn.write(password+"\n")
tn.read_until("#",5)
# prevent pause in output
tn.write("terminal length 0\n")
tn.read_until("#",5)
# console messages not sent by default
# Send 'sh tech' command to switch
tn.write("show tech\n")
output=tn.read_until(cdevice[2]+'#',10)
string.strip(output)
fileout.write(output)
fileout.close()
print 'Done, getting next device \n'
devices.close()
print 'All done' ----------------------------------------
Wasabi Pop Tarts! Write Kellogs today!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top