expect user -> socket 
Author Message
 expect user -> socket

I'm hoping somebody might already have a script tidbit that
addresses this. I've got Tcl (+ expect) listening on a socket
(simple server). A user connects to the socket and gets to
execute a limited set of commands (e.g. using telnet). The
results of the commands get passed back to the user on the
socket.

Now, I want to execute a long running program. I'd like to
use interact, I think. Interact seems to want work with spawn_ids,
but i have a file channel. There's probably a better way that
I haven't thought of yet.. (investigating the possibility of
setting up a fifo so that the socket communicates with the fifo
using fileevents and then the spawned process communicates to the
socket via the fifo as well. Seems kind of complicated though)

        DOug



Sat, 24 Jul 2004 08:10:03 GMT  
 expect user -> socket

Quote:

> I'm hoping somebody might already have a script tidbit that
> addresses this. I've got Tcl (+ expect) listening on a socket
> (simple server). A user connects to the socket and gets to
> execute a limited set of commands (e.g. using telnet). The
> results of the commands get passed back to the user on the
> socket.

> Now, I want to execute a long running program. I'd like to
> use interact, I think. Interact seems to want work with spawn_ids,
> but i have a file channel. There's probably a better way that
> I haven't thought of yet.. (investigating the possibility of
> setting up a fifo so that the socket communicates with the fifo
> using fileevents and then the spawned process communicates to the
> socket via the fifo as well. Seems kind of complicated though)

You can convert a file channel to a spawn id by using spawn -open.
For example:

  spawn -open [open "|date"]
  interact

See the spawn documentation for more info.

Don



Sun, 25 Jul 2004 01:57:09 GMT  
 expect user -> socket

Quote:


> > I'm hoping somebody might already have a script tidbit that
> > addresses this. I've got Tcl (+ expect) listening on a socket
> > (simple server). A user connects to the socket and gets to
> > execute a limited set of commands (e.g. using telnet). The
> > results of the commands get passed back to the user on the
> > socket.

> > Now, I want to execute a long running program. I'd like to
> > use interact, I think. Interact seems to want work with spawn_ids,
> > but i have a file channel. There's probably a better way that
> > I haven't thought of yet.. (investigating the possibility of
> > setting up a fifo so that the socket communicates with the fifo
> > using fileevents and then the spawned process communicates to the
> > socket via the fifo as well. Seems kind of complicated though)

> You can convert a file channel to a spawn id by using spawn -open.
> For example:

>   spawn -open [open "|date"]
>   interact

> See the spawn documentation for more info.

> Don

Mr Libes,
Sorry to bug you on this issue, but I am curious.
Since the server socket returns the parent id (as I understand it) and
the current connection is referenced by sock element returned, what
spawn_id would you collect with:
set sock [spawn -open [socket -server [conn_handler 1235]]],
and what could you do with this with expect? Wouldn't you be
talking to the "parent" and not the handled connection?

That is, if I am reading the post correctly, what the gentleman wants
to do. I, obviously have not tried it myself(scared):)
Thanks,
MMD



Sun, 25 Jul 2004 12:21:03 GMT  
 expect user -> socket

Quote:



> > > I'm hoping somebody might already have a script tidbit that
> > > addresses this. I've got Tcl (+ expect) listening on a socket
> > > (simple server). A user connects to the socket and gets to
> > > execute a limited set of commands (e.g. using telnet). The
> > > results of the commands get passed back to the user on the
> > > socket.

> > > Now, I want to execute a long running program. I'd like to
> > > use interact, I think. Interact seems to want work with spawn_ids,
> > > but i have a file channel. There's probably a better way that
> > > I haven't thought of yet.. (investigating the possibility of
> > > setting up a fifo so that the socket communicates with the fifo
> > > using fileevents and then the spawned process communicates to the
> > > socket via the fifo as well. Seems kind of complicated though)

> > You can convert a file channel to a spawn id by using spawn -open.
> > For example:

> >   spawn -open [open "|date"]
> >   interact

> > See the spawn documentation for more info.

> > Don

> Mr Libes,
> Sorry to bug you on this issue, but I am curious.
> Since the server socket returns the parent id (as I understand it) and
> the current connection is referenced by sock element returned, what
> spawn_id would you collect with:
> set sock [spawn -open [socket -server [conn_handler 1235]]],
> and what could you do with this with expect? Wouldn't you be
> talking to the "parent" and not the handled connection?

NO, don't try to convert the server socket to a spawn_id.
as a connection is made the conn_handler is called with the new socket
that communication occurs - that one you can turn into a spawn_id
if you wanted to.

Bruce



Sun, 25 Jul 2004 19:01:08 GMT  
 expect user -> socket

Quote:




> > > > I'm hoping somebody might already have a script tidbit that
> > > > addresses this. I've got Tcl (+ expect) listening on a socket
> > > > (simple server). A user connects to the socket and gets to
> > > > execute a limited set of commands (e.g. using telnet). The
> > > > results of the commands get passed back to the user on the
> > > > socket.

> > > > Now, I want to execute a long running program. I'd like to
> > > > use interact, I think. Interact seems to want work with spawn_ids,
> > > > but i have a file channel. There's probably a better way that
> > > > I haven't thought of yet.. (investigating the possibility of
> > > > setting up a fifo so that the socket communicates with the fifo
> > > > using fileevents and then the spawned process communicates to the
> > > > socket via the fifo as well. Seems kind of complicated though)

> > > You can convert a file channel to a spawn id by using spawn -open.
> > > For example:

> > >   spawn -open [open "|date"]
> > >   interact

> > > See the spawn documentation for more info.

> > > Don

> > Mr Libes,
> > Sorry to bug you on this issue, but I am curious.
> > Since the server socket returns the parent id (as I understand it) and
> > the current connection is referenced by sock element returned, what
> > spawn_id would you collect with:
> > set sock [spawn -open [socket -server [conn_handler 1235]]],
> > and what could you do with this with expect? Wouldn't you be
> > talking to the "parent" and not the handled connection?

> NO, don't try to convert the server socket to a spawn_id.
> as a connection is made the conn_handler is called with the new socket
> that communication occurs - that one you can turn into a spawn_id
> if you wanted to.

> Bruce

Mr.Hartweg,
Could you give an example of how to do this? BTW:That was what I was
referring to,(I am not a programmer),that the original handle references
parent and the connects are handed off to children.
How are you going to do it?? :: spawn -open $sock?

Thanks
MMD



Mon, 26 Jul 2004 06:59:41 GMT  
 expect user -> socket

Quote:





> > > > > I'm hoping somebody might already have a script tidbit that
> > > > > addresses this. I've got Tcl (+ expect) listening on a socket
> > > > > (simple server). A user connects to the socket and gets to
> > > > > execute a limited set of commands (e.g. using telnet). The
> > > > > results of the commands get passed back to the user on the
> > > > > socket.

> > > > > Now, I want to execute a long running program. I'd like to
> > > > > use interact, I think. Interact seems to want work with spawn_ids,
> > > > > but i have a file channel. There's probably a better way that
> > > > > I haven't thought of yet.. (investigating the possibility of
> > > > > setting up a fifo so that the socket communicates with the fifo
> > > > > using fileevents and then the spawned process communicates to the
> > > > > socket via the fifo as well. Seems kind of complicated though)

> > > > You can convert a file channel to a spawn id by using spawn -open.
> > > > For example:

> > > >   spawn -open [open "|date"]
> > > >   interact

> > > > See the spawn documentation for more info.

> > > > Don

> > > Mr Libes,
> > > Sorry to bug you on this issue, but I am curious.
> > > Since the server socket returns the parent id (as I understand it) and
> > > the current connection is referenced by sock element returned, what
> > > spawn_id would you collect with:
> > > set sock [spawn -open [socket -server [conn_handler 1235]]],
> > > and what could you do with this with expect? Wouldn't you be
> > > talking to the "parent" and not the handled connection?

> > NO, don't try to convert the server socket to a spawn_id.
> > as a connection is made the conn_handler is called with the new socket
> > that communication occurs - that one you can turn into a spawn_id
> > if you wanted to.

> > Bruce

> Mr.Hartweg,
> Could you give an example of how to do this? BTW:That was what I was
> referring to,(I am not a programmer),that the original handle references
> parent and the connects are handed off to children.
> How are you going to do it?? :: spawn -open $sock?

set sock [socket -server [list conn_handler 1235]]
proc conn_handler {sock addr port} {
    puts "Socket $sock from ${addr}:$port"
    set connection [spawn -open $sock]

Quote:
}

will probably be what you are looking for (obviously this is a minimal
example). I don't use expect, but I do use Tcl sockets - I'm assuming
[spawn -open] will work this way.

- Show quoted text -

Quote:

> Thanks
> MMD



Mon, 26 Jul 2004 07:25:33 GMT  
 expect user -> socket
Quote:


>> Now, I want to execute a long running program. I'd like to
>> use interact, I think. Interact seems to want work with spawn_ids,
>> but i have a file channel. There's probably a better way that
>> I haven't thought of yet.. (investigating the possibility of
>> setting up a fifo so that the socket communicates with the fifo
>> using fileevents and then the spawned process communicates to the
>> socket via the fifo as well. Seems kind of complicated though)

>You can convert a file channel to a spawn id by using spawn -open.
>For example:

>  spawn -open [open "|date"]
>  interact

>See the spawn documentation for more info.

Yeah, that's exactly what I ended up doing after looking around a bit
more. At first I couldn't get it to work. User error. :)

(Got a little remote control sort of restricted shell daemon. a customer
connects over a socket, has a limited set of commands they can run
in a safe interpreter, gets back results. Worked great. )
        thanks!
    Doug



Mon, 26 Jul 2004 12:51:48 GMT  
 expect user -> socket

Quote:




>> > NO, don't try to convert the server socket to a spawn_id.
>> > as a connection is made the conn_handler is called with the new socket
>> > that communication occurs - that one you can turn into a spawn_id
>> > if you wanted to.

>> > Bruce

>> Mr.Hartweg,
>> Could you give an example of how to do this? BTW:That was what I was
>> referring to,(I am not a programmer),that the original handle references
>> parent and the connects are handed off to children.
>> How are you going to do it?? :: spawn -open $sock?

>set sock [socket -server [list conn_handler 1235]]
>proc conn_handler {sock addr port} {
>    puts "Socket $sock from ${addr}:$port"
>    set connection [spawn -open $sock]
>}

>will probably be what you are looking for (obviously this is a minimal
>example). I don't use expect, but I do use Tcl sockets - I'm assuming
>[spawn -open] will work this way.

Here are some excerpts from what I did..

interp create -safe safei

safei expose pwd

proc bgerror {args} {
        global sock errorInfo
#        puts "hmm, handled error '$args'"
        puts $sock "Disabled command: $errorInfo"
        flush $sock
        return 0

Quote:
}

proc sadd {chan addr port} {
        global errorInfo spawn_id sock

        puts "connect from $addr at [exec date]"
        fconfigure $chan -buffering none
        set sock $chan
        puts -nonewline $chan "% "
        while {[gets $chan x] >= 0} {
                puts "got $x"
                switch [lindex $x 0] {
                        lping   {
                                spawn -open $sock
                                set user $spawn_id
                                spawn ./lping -i hosts -o output
                                interact {
                                        -nobuffer
                                        -input $spawn_id -output $user
                                        -input $user -output $spawn_id
                                }
                        }
                                                ...
                        default {
                                if {[catch {set m [safei eval $x]} zoo]} {
                                        regexp -line {^.*$} $errorInfo errhead
                                        puts $chan "$x: $errhead"
                                        puts -nonewline $chan "% "
                                        continue
                                }
                                puts $chan $m
                        }
                }
                puts -nonewline $chan "% "
        }
        close $chan

Quote:
}

socket -server sadd 21735

---
        Doug



Mon, 26 Jul 2004 12:57:34 GMT  
 expect user -> socket

Quote:





> >> > NO, don't try to convert the server socket to a spawn_id.
> >> > as a connection is made the conn_handler is called with the new socket
> >> > that communication occurs - that one you can turn into a spawn_id
> >> > if you wanted to.

> >> > Bruce

> >> Mr.Hartweg,
> >> Could you give an example of how to do this? BTW:That was what I was
> >> referring to,(I am not a programmer),that the original handle references
> >> parent and the connects are handed off to children.
> >> How are you going to do it?? :: spawn -open $sock?

> >set sock [socket -server [list conn_handler 1235]]
> >proc conn_handler {sock addr port} {
> >    puts "Socket $sock from ${addr}:$port"
> >    set connection [spawn -open $sock]
> >}

> >will probably be what you are looking for (obviously this is a minimal
> >example). I don't use expect, but I do use Tcl sockets - I'm assuming
> >[spawn -open] will work this way.

> Here are some excerpts from what I did..

> interp create -safe safei

> safei expose pwd

> proc bgerror {args} {
>         global sock errorInfo
> #        puts "hmm, handled error '$args'"
>         puts $sock "Disabled command: $errorInfo"
>         flush $sock
>         return 0
> }

> proc sadd {chan addr port} {
>         global errorInfo spawn_id sock

>         puts "connect from $addr at [exec date]"
>         fconfigure $chan -buffering none
>         set sock $chan
>         puts -nonewline $chan "% "
>         while {[gets $chan x] >= 0} {
>                 puts "got $x"
>                 switch [lindex $x 0] {
>                         lping   {
>                                 spawn -open $sock

I think this line needs to be:
spawn -open $chan
as "chan" holds the value of the currently used socket. You are still
using the server socket here.
BTW - [exec date] can be replaced with [clock format [clock seconds]]
for portability and efficiency.

<snip>

- Show quoted text -

Quote:
> ---
>         Doug



Mon, 26 Jul 2004 20:46:02 GMT  
 expect user -> socket

Quote:

> >         puts "connect from $addr at [exec date]"
> >         fconfigure $chan -buffering none
> >         set sock $chan
> >         puts -nonewline $chan "% "
> >         while {[gets $chan x] >= 0} {
> >                 puts "got $x"
> >                 switch [lindex $x 0] {
> >                         lping   {
> >                                 spawn -open $sock

> I think this line needs to be:
> spawn -open $chan
> as "chan" holds the value of the currently used socket. You are still
> using the server socket here.

look up a few lines .... set sock $chan
don't really need it - could just use $chan directly - but it isn't the server sock

Bruce



Mon, 26 Jul 2004 21:59:30 GMT  
 expect user -> socket

Quote:


> > >         puts "connect from $addr at [exec date]"
> > >         fconfigure $chan -buffering none
> > >         set sock $chan
> > >         puts -nonewline $chan "% "
> > >         while {[gets $chan x] >= 0} {
> > >                 puts "got $x"
> > >                 switch [lindex $x 0] {
> > >                         lping   {
> > >                                 spawn -open $sock

> > I think this line needs to be:
> > spawn -open $chan
> > as "chan" holds the value of the currently used socket. You are still
> > using the server socket here.

> look up a few lines .... set sock $chan
> don't really need it - could just use $chan directly - but it isn't the server sock

> Bruce

Ah ok - missed that. I notice there is also a [global sock] at the top
too. Bit confusing, especially when it comes time to close the server!


Mon, 26 Jul 2004 22:19:36 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Expect: fails in expect { {->} whatever } :-[

2. Socket<->Modem<->Null-modem?

3. HELP>>>>>>>Fortran-Pascal Linking

4. >>>>>>>FROM SMTK TO C++

5. HELP>..for listing selection of user...HELP>>>>

6. ><><><><>Heeeeeeeeeeeeeeelp on INT 14!><><><><><>

7. Expected behavior in the socket library

8. Expect: binary data over socket

9. Sockets: Wish & Expect Scripts

10. Socket/Expect problem help.

11. Sockets & Expect

12. Expect: spawn open socket fails

 

 
Powered by phpBB® Forum Software