From: Salau,Olayemi (Olayemi.Salau@southampton.gov.uk)
Date: Tue Mar 20 2007 - 15:32:20 ART
Hello Hossam,
Don't know if you're conversant with shell scripts, but this is no
different at all, just basic programming logic.
The Cisco IOS on routers have got an inbuilt shell called tclshell,
based on the "tool control language". This is just like any other
programming language, with the aid of a shell prompt called "tclshell",
you can interact with the router using the tcl programming language.
Just like using command.exe to interact with DOS based systems, or
sh/ksh/csh/ash/bash to interact with unix-based OSes.
Also, internetworkexpert provided a good intro into this: have a look:
http://www.internetworkexpert.com/resources/tclshrsh.htm
I guess the above gives a good intro about shells, now lets move on to
the command basics:-
In any programming language, there are so many things to talk about, but
you only need a few of them in CCIE Lab:
- Variables
- Procedures
- Expressions
- Loop
- Strings
- A few other bits
You can find a link to a good tutorial here:
http://www.tcl.tk/man/tcl8.5/tutorial/tcltutorial.html
But I will try to explain the commands you sent across:
Tclsh <<<<<< This activates or take you to the shell where you can start
your tcl programming
foreach i { <<<<<<<< The foreach statement starts a loop where you
need to do some things with some objects, this
will keep reoccurring until the process jumps out of a loop.
Analogy:- You've got 5 kids and need to buy them all sweets, so you'll
need to do the buying 5 times i.e kid A, kid B, kid C, kid D and Kid E
Also, when you do a loop, you don't just do if for fun, you'll
have to specify an action to follow.
The "i" on the line above could be seen as a variable, something that
changes within the loop, a widely used analogy in the programming world
would be a cup, which can be filled with different kind of liquids,
water, orange juice, fanta, or even BEER :-). But to be consistent with
the analogy I used about the kids, the content of this "Cup" would be
the Kids i.e. A, B, C, D and E.
So the next line is where you declare your values, i.e. the value that
the cup contains within a turn of the loop, in your example, the loop
will go round 3 times. In my own analogy about the kids, it will go
round 5 times. So for me, what I would do here is put in my five values
... Kid A, Kid B etc.
136.1.136.1
136.1.15.1
150.1.1.1
} { puts [ exec "ping $i" ] } <<<<<< this line is the tricky bit I
guess, but let's keep things simple, let's say this
is the line where you declare what you want to output to the screen"
Now in the Cisco world, to ping, you need to execute a command e.g.
"ping 1.1.1.1" on the Router's prompt. In the real world, you type with
your keyboard to "input" these xters into your execution prompts, In TCL
programming, what does this is "exec".
The command you want to execute is "ping something", Hence in double
qoutes.
Now remember what is important to us is the content of the cup but not
the cup itself, so what's important to us will be $i and not "i" itself.
The content of variable "i" is the something we want to ping
I'm not sure if you've got the grips you need on this, but the link I
provided explains what the difference is between using "",[], and () in
tcl programming. My own aim is just to throw a sort of introductory
light on tcl programming.
"" - These are used to output characters with option to substitute
contents of variable
{} - These are used to output characters without option to substitute
contents of variables, ie. What you enclose here is exactly what is
printed on the screen. So if I do puts {$i}; you will get $i on the
screen as opposed to the content of i
[] - These are used to execute commands, see it as a
subprocedure/subroutine kind of thing where the output of a command in
fed as input to another command
Also remember that all of your commands can go in one line, but we tend
to use the enter key after each variable for best practice:
foreach i { 136.1.136.1 136.1.15.1 150.1.1.1 } { puts [exec "ping $i"] }
The above will achieve the same result.
Hope this helps, I'm closing for the day at work now. Don't forget this
link for futher details:
http://www.tcl.tk/man/tcl8.5/tutorial/tcltutorial.html
Many Thanks
This archive was generated by hypermail 2.1.4 : Sun Apr 01 2007 - 06:35:52 ART