Nikaido
Nikaido

Reputation: 13

Need assitance with expect script

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 }

Upvotes: 0

Views: 127

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137567

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 ...
}

Upvotes: 0

Related Questions