PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 11, 2022

[FIXED] how to spawn telnet from a proc with Tcl?

 November 11, 2022     automation, expect, proc, tcl, telnet     No comments   

Issue

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
}


Answered By - Donal Fellows
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing