Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

tcl/expect w/ cisco devices

212 views
Skip to first unread message

Dennis

unread,
Sep 28, 2002, 3:39:41 PM9/28/02
to

Has anyone used tcl w/ expect? I'm trying to create some scripts to
automating the configuration cisco routers and switches. I'm did some
searching around on the web but didn't really find the information I was
looking for. I want to be able to script a telnet or console session like
in CRT or SecureCRT. Basically this is what I want to do:

I need the script to initiate a telnet session then

I need a script to wait for certain prompts, for example:

router>

then when the script sees this prompt will return a command and carriage
return

router> enable

then wait another prompt:

router#

then returns another command

router# configure terminal

etc.etc.etc.

Any help would greatly be appreciated? I am completely new to tcl and
expect.

Thanks

-Dennis


Cameron Laird

unread,
Sep 28, 2002, 6:41:06 PM9/28/02
to
In article <upc1o34...@corp.supernews.com>,
.
.
.
That's exactly what Expect does.

Well, in fact there are several ways to achieve this, if I understand
your description correctly. When you write, "... initiate a telnet
session ...", is there a password authentication involved? I'll as-
sume so; if not, there's likely to be an even simpler solution than
what follows.

I had intentions of writing <URL: http://wiki.tcl.tk/4202 > for you.
I probably won't be able to make it readable until the work-week.
I'll summarize for now: if you're working from a Unix host, you're
going to have fun. Using Expect to manage a Cisco router will work
out well. You might find a lot of quick satisfaction by starting
with autoexpect (see the link from the URL above). Use autoexpect,
and see how it solves this problem for you.
--

Cameron Laird <Cam...@Lairds.com>
Business: http://www.Phaseit.net
Personal: http://phaseit.net/claird/home.html

Mike Hoegeman

unread,
Sep 29, 2002, 2:23:58 PM9/29/02
to
Dennis wrote:
>
> Has anyone used tcl w/ expect? I'm trying to create some scripts to
> automating the configuration cisco routers and switches. I'm did some
> searching around on the web but didn't really find the information I was
> looking for. I want to be able to script a telnet or console session like
> in CRT or SecureCRT. Basically this is what I want to do:

Dennis,

people manipulate cisco routers all the
time this way with expect.

your best bet is to get the expect book if you are going to do
a lot of this. it's well worth the investment.

you can get book via amazon or the o'reilly web site http://ora.com


>
> I need the script to initiate a telnet session then
>
> I need a script to wait for certain prompts, for example:
>
> router>
>
> then when the script sees this prompt will return a command and carriage
> return
>
> router> enable
>
> then wait another prompt:
>
> router#
>
> then returns another command
>
> router# configure terminal
>
> etc.etc.etc.
>
> Any help would greatly be appreciated? I am completely new to tcl and
> expect.
>
> Thanks
>
> -Dennis

--
Mike Hoegeman Habanero Technologies, LLC
http://habanerotech.net/personal-pages/mh/contact.html

Dennis

unread,
Sep 29, 2002, 11:52:21 PM9/29/02
to

Thanks for the responses, I have another question. Cisco devices have a
break sequence like ctrl-shift-6 and x. Is this possible? And if so what
about a regular break sequence like ctrl-break?

The company I work for teaches Cisco stuff and we have occasional students
and instructors lock themselves out. I was just thinking if I wrote some
scripts that would do password recovery it would make managing it a lot
easier. Also at the end of the week I could maybe make a scheduled task that
blows away the old config and restores them to default so I don't have to do
them manually 1 by 1 each week.

Thanks again for the previous responses. Sounds like I'll be taking a closer
look at expect. Probably use it from a linux box. Maybe I'll go buy the
book, I was hesitant because it was dated so long ago. Hopefully o'reilly
will come out w/ a 2nd edition. =)

-Dennis

"Dennis" <abu...@hotmail.com> wrote in message
news:upc1o34...@corp.supernews.com...

Dennis

unread,
Sep 30, 2002, 4:05:05 AM9/30/02
to

Thanks Cameron and Mike!

I managed to get tcl and expect installed on my linux box and played around
w/ autoexpect. I have a pretty good idea of how it works and it is just what
I expected. Can't wait until I get to work and start writing scripts to
automate everything...should save a lot of time.

-Dennis


Cameron Laird

unread,
Sep 30, 2002, 9:40:40 AM9/30/02
to
In article <upfijrf...@corp.supernews.com>,

Dennis <abu...@hotmail.com> wrote:
>
>Thanks for the responses, I have another question. Cisco devices have a
>break sequence like ctrl-shift-6 and x. Is this possible? And if so what
>about a regular break sequence like ctrl-break?
.
.
.
<URL: http://wiki.tcl.tk/3547 > should get you close
to the answer you're after.

Cameron Laird

unread,
Sep 30, 2002, 9:50:13 AM9/30/02
to
.
.
.

>The company I work for teaches Cisco stuff and we have occasional students
>and instructors lock themselves out. I was just thinking if I wrote some
>scripts that would do password recovery it would make managing it a lot
>easier. Also at the end of the week I could maybe make a scheduled task that
>blows away the old config and restores them to default so I don't have to do
>them manually 1 by 1 each week.
>
>Thanks again for the previous responses. Sounds like I'll be taking a closer
>look at expect. Probably use it from a linux box. Maybe I'll go buy the
>book, I was hesitant because it was dated so long ago. Hopefully o'reilly
>will come out w/ a 2nd edition. =)
.
.
.
YES. Whoops; didn't mean to frighten you. What I
mean is to applaud you as vigorously as possible
for noticing that you're encountering a recurring
problem--participants locking themselves out, end-
of-the-week resets--and you're looking for a
systematic way to resolve it. Good for you!

As it happens, many, many situations in this cate-
gory simplify enormously with Expect. Once you have
a little familiarity with it, you'll wonder how you
lived without it.

Yes, the Expect book was written a long time ago.
Sometimes that's a bad thing. In this case, it
means that it was written so well that it has
endured far beyond the computer-book lifespan to
which you're accustomed.

O'Reilly's not the constraint. In fact, there are
even other publishers willing to put out rival Ex-
pect books. All the potential authors competent
to do one know they have better prospects than
competing with Don's.

You mention "a linux box". Here's an Expect tip
that's obvious to some, and a revelation to others:
you can exploit Expect even when the host you care
about doesn't have Expect. That's what you're
doing: you want to automate operations on a
particular router, but you use Expect from your
Linux box to do so. Think for a moment about how
you can generalize this: maybe you have database
work to do under Windows or OpenVMS. You know
Expect is good for automating such things. You
don't have Expect on those machines. That needn't
stop you; if you can network in to those machines,
even from a tiny little Linux box that's booting
from a floppy, all of a sudden you have all the
power of Expect to automate to your heart's content.
Slick, eh?

Cameron Laird

unread,
Oct 2, 2002, 7:08:21 AM10/2/02
to
In article <upg1tus...@corp.supernews.com>,

Absolutely.

'Best thanks you can give is to write up your experiences,
so others can benefit from them. There are thousands of
people using Tcl to script Cisco boxes, but almost zero of
their work is visible publicly.

There's space for anything you have to say either in the
Wiki, on such pages as <URL: http://wiki.tcl.tk/cisco >,
or in more conventional magazines, if you can defer grati-
fication a bit longer.
--

Cameron Laird <Cam...@Lairds.com>
Business: http://www.Phaseit.net

Personal: http://starbase.neosoft.com/~claird/home.html

Dennis

unread,
Oct 3, 2002, 3:44:17 AM10/3/02
to

As soon as I get these scripts running the way I like them, I'll post them.
I was thinking of creating a website for that sort of stuff because I can't
find any documentation out there. I'm actually working w/ tcl/expect in
linux and tera term pro in windows. Tera term also has some really nice
scripting features and both are free. I'm going to compare the 2 of them
but will probably end up using both. One for the student end and the other
for management end. Maybe do a cron job so on monday all our equipment is
ready to go. I'm building a password recovery script right now and trying to
build some logic into it for our use. It's pretty addicting because it's
like a puzzle.

Okay anyways, I had another question. I can get expect to work from a
script, can I combine expect w/ tcl? or am I already doing so? From my
understanding expect is an extension of tcl. Also, is there any way to call
subroutines, etc. Like for example in DOS batch scripts you can have logical
pointers and have a call or goto statement. Just wondering...I will probably
answer my own question in time. =)

Anyways, it might be some time cause we busy at work, but I for sure want to
get this to work. Also I'm trying to make everything modular so I can easily
scale. When I get a chance I'll post my results. =)

Thanks for the help!

-Dennis

Cameron Laird

unread,
Oct 3, 2002, 8:46:49 AM10/3/02
to
In article <upnter6...@corp.supernews.com>,
.
.
.
Super.

I repeat: the Wiki's available, and there are several
other possibilities for Web space. I'd be happy to
host your work, for example. Also, this sort of auto-
mation is important enough to be publishable as either
magazine articles or even a book. That's saying a lot,
because publishers generally are quite wary about Tcl.
The Cisco aspect makes a big difference.

I'm going to be writing you privately; I need help with
Tera Term.

You are absolutely right to be excited by this work.
It does make for a puzzle which is both interesting and,
as you're seeing for yourself, marvelously powerful.
You should be able to raise the quality of your opera-
tions significantly with the Expect-based automation you
describe.

Can you combine Expect and Tcl? YES. This is the subject
of <URL: http://
www-106.ibm.com/developerworks/library/l-sc1/?n-l-4112 >.

Are there subroutines? YES. Do this, in Expect:
proc mysub argument {
puts "The argument is '$argument'."
}

mysub reindeer
mysub "drugs terrorists CIA covert"

0 new messages