0
votes

I am trying to achieve a certain expect script I have a txt file..

It contains data like

1 ip telnetname slot1 slot2 assoc1 assoc2 mep1 mep2
2 ip telnetname slot1 slot2 assoc1 assoc2 mep1 mep2

I want to make an expect script that for each line in the file it spawns a telnet session with variables set from the line like spawn telnet from line 1 with each 1 of those words as a variable set to be later used in commands.

This is what I tried so far

foreach ip $ips telnetname $telnetnames slot1 $slots1 slot2 $slots2  { commands here }
1
What's missing is the result of what you have tried. And hence with what problem exactly you do need assistance. Otherwise the question looks like of the sadly standard "give me the complete code" type. - kostix
Could you edit your own answer and try to elaborate on what you are trying to achieve? I'm not sure I understand what you're trying to do. Do you mean you want to use, for example, only the first line and not the other lines when you run the command? (and that it could have been only line 3 another time?) - Jerry

1 Answers

0
votes

It isn't quite clear what you're trying to do from your description, but it is probably easiest to make your code work like this:

# Slurp the data into Tcl; it's not *that* long, is it?
set f [open "inputfile.txt"]
set data [read $f]
close $f

foreach line [split $data "\n"] {
    # Tcl's [scan] is like C's sscanf(), but memory-safe!
    if {[scan $line "%d %s %s %s %s" -> ip telnetname slot1 slot2] != 5} {
        # Not what was expected; skip it!
        continue
    }
    # Now do something with $ip, $telnetname, $slot1 and $slot2 ...
}