Search code examples
automationtclexpecttelnetproc

how to spawn telnet from a proc with Tcl?


Where's the telnet output?

thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ tclsh main.tcl 
spawn telnet rainmaker.wunderground.com
getting weather for nyc
^C
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 

main:

lappend auto_path /home/thufir/NetBeansProjects/spawnTelnet/telnet/api

package require weather 1.0


tutstack::connect "nyc"

code:

package provide weather  1.0
package require Tcl      8.5
package require Expect

namespace eval ::tutstack {
}

proc ::tutstack::parse {city} {
puts "getting weather for $city"
expect -nocase "Press Return to continue:"
#interact \004 return
interact \004 return
}

proc ::tutstack::connect {city} {
spawn telnet rainmaker.wunderground.com
set telnet $spawn_id
#interact
parse $city
}

this works:

thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ tclsh chainedProcs.tcl 
hello Alice from first
hello Alice from second
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ cat chainedProcs.tcl 
lappend auto_path /home/thufir/NetBeansProjects/spawnTelnet/telnet/chained

package require chained 1.0

example::first "Alice"
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ cat chained/chained.tcl 
package provide chained  1.0

namespace eval ::example {
}

proc ::example::first {foo} {
puts "hello $foo from first"
second $foo
}

proc ::example::second {bar} {
puts "hello $bar from second"
}
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 

but...not using telnet there. I'm looking to "chain" (?) a sequence but with telnet, expect, interact, etc.


Solution

  • Whenever you are using the Expect package's commands in a procedure, you need to take some care because of the way it accesses variables. In particular, you probably need to say at least:

    global spawn_id
    

    in each of those procedures. Perhaps like this:

    proc ::tutstack::parse {city} {
        global spawn_id
        puts "getting weather for $city"
        expect -nocase "Press Return to continue:"
        # You *might* need inter_return instead of return; the documentation isn't clear
        interact "\004" return
    }
    
    proc ::tutstack::connect {city} {
        global spawn_id
        spawn telnet rainmaker.wunderground.com
        set telnet $spawn_id
        parse $city
    }
    

    However, you're probably better off keeping the spawn ID (i.e., the result of calling spawn) in a namespace variable and passing it explicitly into the relevant commands via the -i flag, like this:

    proc ::tutstack::connect {city} {
        variable telnet [spawn telnet rainmaker.wunderground.com]
        parse $city
    }
    
    proc ::tutstack::parse {city} {
        variable telnet
        puts "getting weather for $city"
        expect -i $telnet -nocase "Press Return to continue:"
        # You *might* need inter_return instead of return; the documentation isn't clear
        interact -i $telnet "\004" return
    }