
Newbie - passing variable number of vars to expect script
Hi Peter,
First the important things:
Quote:
> spawn telnet [lindex $line 0]
> ### Problem is here!
> I log in here, but problem is how can I get interface to
> change from within telnet session OR reference [lindex $line
> i]???
I don't understand the problem. You have opened your telnet session
here. Why can't you use a variable? Can you show an example of code
that didn't work? The rest of the code doesn't do anything much yet,
so that can't be part of the problem.
Try something simple, let's drop the scaffolding for a moment and
concentrate on the problem with which you want help. Write down
exactly those things that we need to understand the problem.
Currently you have the equivalent of:
set routerid "your.router.name"
set interfaces [list "iface1" "iface2" "iface3"]
foreach interface $interfaces {
spawn telnet $routerid
# do something
# close telnet session
}
First change, to clean up resources, you probably need:
foreach interface $interfaces {
set spawn_id [spawn telnet $routerid]
# do something
close $spawn_id
}
Second change to consider: Do you really want to re-connect to the
router every time? If not, you want
set spawn_id [spawn telnet $routerid]
foreach interface $interfaces {
# do something
}
close $spawn_id
Next, what is that code that you can't get to work, and which goes
where I wrote "# do something"?
Fine print:
Quote:
> Trying to use a list. I'm reading the file in, everything is
> looking fine, but just found out I can't use a variable for an
> index.
Index into what? I don't understand, try slowly (getting impatient
yourself won't get results). Tcl lets you use a variable in any place
where you can use a literal. There may be a matter of giving the
right level of quoting.
Quote:
> Someone please tell me there is a way around this....
> #!/usr/local/bin/expect
> set datafile [open "al.tmp" "r"]
> set uid "pg0"
> set passwd "pgpass"
> set console_prompt ">$" ;# Variable for console prompt
> set enable_prompt "#$" ;# Variable for enable prompt
> set config_prompt "#$" ;# Variable for config prompt
> set config_line "#$" ;# Variable for config prompt
Are these variables part of your problem? You never use them yet.
Also you should get into the habit of escaping the $ signs, if they do
not refer to variables.
Quote:
> while {[gets $datafile line] != -1} {
> set len [llength $line]
> set routerid [lindex $line 0]
> for {set i 1} {$i <=[expr [llength $line]]} {incr i} {
> set interface [lindex $line $i]
> [...]
Indent your code correctly. You will loose time getting confused if
you don't.
Quote:
> while {[gets $datafile line] != -1} {
> set len [llength $line]
> set routerid [lindex $line 0]
> for {set i 1} {$i <=[expr [llength $line]]} {incr i} {
> set interface [lindex $line $i]
> puts $interface
> spawn telnet [lindex $line 0]
> ### Problem is here!
> I log in here, but problem is how can I get interface to change from
> within telnet session OR reference [lindex $line i]???
> }
> }
Line-by-line comments:
Quote:
> while {[gets $datafile line] != -1} {
You get a line from your datafile. Can you explain what format your
datafile has and where the data comes from? This should than go into
an explaining comment in the code.
Quote:
> set len [llength $line]
You are treating the string you read as a list. Is that safe? In
general if you can not garantee that, you may want to use something
like
set params [split $line " "]
And than use the list $params instead of the raw string.
Quote:
> set routerid [lindex $line 0]
So your first item in the list is a router address.
Quote:
> for {set i 1} {$i <=[expr [llength $line]]} {incr i} {
> set interface [lindex $line $i]
The rest is a list of interfaces. It's more Tcl-like to use foreach
if you do not want to access a single item but want to iterate over a
list, like
foreach interface [lrange $line 1 end] {
Quote:
> puts $interface
> spawn telnet [lindex $line 0]
You want to use your variable $routerid here, like
spawn telnet $routerid
benny