piping plink output in real time into text widget - tcl

I'm trying to connect with a server using plink, execute a command and 'pipe' output into my text widget:
set myCommand "echo command | plink.exe -ssh server -pw lucas"
catch {eval exec cmd.exe $myCommand } res
.text insert end $res
# remote command is not working that's why I'm sending command using echo and I'm executing it in cmd.exe because tcl not recognize echo command
Unfortunatelly catch is not working here. Command is executed in the background and nothing happens. No piping into my text widget. It would be great if catched output would be transmited in real time.
That's why I've tried this:
http://wiki.tcl.tk/3543
package require Tk
set shell cmd.exe
proc log {text {tags {}}} {
.output configure -state normal
.output insert end $text $tags
.output configure -state disabled
.output see end
}
proc transmit {} {
global chan
log "\$ [.input get]\n" input
puts $chan [.input get]
.input delete 0 end
}
proc receive {} {
global chan
log [read $chan]
}
entry .input
scrollbar .scroll -orient vertical -command {.output yview}
text .output -state disabled -yscrollcommand {.scroll set}
.output tag configure input -background gray
pack .input -fill x -side bottom
pack .scroll -fill y -side right
pack .output -fill both -expand 1
focus .input
set chan [open |$shell a+]
fconfigure $chan -buffering line -blocking 0
fileevent $chan readable receive
bind .input <Return> transmit
and it's working under cygwin, but after wrapped to .exe when I'm trying execute a command, plink is opening me new black cmd window (why???) where the command is executing and output appears. From this wndow I cannot anymore pipe the output.

Many issues here. Hopefully these notes will help you figure out what to do.
It seems a bit over-complicated to run things through cmd.exe just to make echo piped into plink.exe work. I'd write that like this:
catch { exec plink.exe -ssh server -pw lucas << "command" } res
Note that like this, we don't need to use eval at all.
Failing that, if that command is something coming from the user and you need to support command shell syntax with it, you can do this:
set myCommand "echo command | plink.exe -ssh server -pw lucas"
catch { exec cmd.exe /C $myCommand } res
Otherwise you get into the mess of stuff related to the parsing of options to cmd.exe and that's probably not what you want! (That /C is important; it tells cmd.exe “here comes a command”.)
Note that we're still avoiding eval here. That eval is (almost) certainly a bad idea with what you're trying to do.
When working with wrapped code, the problem is a different one. The issue there is that Windows processes each use a particular subsystem (it's effectively a compilation option when building the executable if I remember right) and the wrapped wish.exe and cmd.exe use different subsystems. Going across the subsystem boundaries is very messy, as the OS tries to be helpful and does things like allocating a terminal for you. Which you didn't want. (I don't remember if you have the problem with direct use of plink.exe, and I can't check from here due to being on entirely the wrong platform and not having a convenient VM set up.)
To run the plink.exe in the background, assemble the pipeline like this:
set myCmd [list plink.exe -ssh server -pw lucas]
set chan [open |$myCmd a+]; # r+ or w+ would work just as well for read/write pipe
fconfigure $chan -buffering none -blocking 0
puts $chan "command"
Also, be aware that when using fileevent you need to take care to detect an EOF condition and close the pipe when that happens:
proc receive {} {
global chan
log [read $chan]
if {[eof $chan]} {
close $chan
}
}
Otherwise when the pipe is closed you'll get an infinite sequence of events on the pipe channel (and the read will always produce a zero-length result since there's provably nothing there).

Related

How does one get the results of 'exec' to show in the invoking shell?

I'm using a tcl gui only to assist in creating valid command lines which are otherwise too irritating to type. I simply want what is executed to appear in the shell in which I launch the gui, and all results as well, exactly as though I typed in the command (which below, is only 'ls -l').
#!/usr/bin/wish
wm title . "Console wishing thingy"
wm minsize . 400 400
# -------------------------------------------------------------
set buttonFrame [labelframe .button_frame -borderwidth 1 -text "Buttons to Click"]
set quit_button [button $buttonFrame.quit_button -text "Quit" -command "exit"]
set run_it_button [button $buttonFrame.run_it_button -text "execute ls -l" -command {do_run}]
pack $buttonFrame.quit_button -side left
pack $buttonFrame.run_it_button -side right
grid $buttonFrame -sticky w -pady 5
proc do_run {} {
exec ls -l
}
No results. No errors. But, no console output either.
I simply want what is executed to appear in the shell in which I launch the gui, and all results as well
It depends, you have several options:
(1) proc do_run {} { exec 1>#stdout 2>#stderr ls -la }
With these directives, the Tcl process running exec will show the executed command's stdout and stderr as it were its own.
(2) proc do_run {} { puts [exec ls -la] }
Without directives, as above, exec returns the captured stdout of the executed command. Using puts will print the returned stdout to the Tcl process' stdout. stderr will cause exec to return a Tcl error, which will be handled in the wish, Tk way.
(3) proc do_run {} { exec >&#stdout ls -la }
If you don't want to handle stderr specifically, this will mold both into the Tcl process's stdout.
See also https://wiki.tcl-lang.org/page/exec.

How to do 'svn checkout URL' using exec?

I'm trying to checkout Joe English's tile-extras from Github using svn using a Tcl script.
The required command is
svn checkout https://github.com/jenglish/tile-extras.git path
I have some code that boils down to
exec C:/cygwin64/bin/svn.exe checkout \
https://github.com/jenglish/tile-extras.git C:/cygwin64/tmp/TCL61416]
which fails with the message
couldn't execute "C:\cygwin64\bin\svn.exe checkout
https:\github.com\jenglish\tile-extras.git
C:\cygwin64\tmp\TCL61416": No error
Pasting the command quoted in the error message into a Windows Command Prompt window, I see
svn: E125002: 'https:\github.com\jenglish\tile-extras.git' does not appear to be a URL
So, the problem seems to be that exec converts Tcl-style paths to Unix-style a little over-enthusiastically. Is there any way I can prevent it from converting https://github.com/jenglish... to https:\github.com\jenglish...?
For information, I'm running on Windows 10, with cygwin (setup version 2.889 (64 bit)), svn 1.9.7 and tcl version 8.6.7 (via ActiveTcl 8.6.7.0).
UPDATE
Here is my actual code, which I'm only slightly embarrassed by:
# svn wrapper proposed by Donal Fellows at
# http://stackoverflow/questions/49224268
proc svn {args} {
exec {*}[auto_execok svn] {*}$args <#stdin >#stdout }
# Checkout from github to a temporary repository
set repository https://github.com/jenglish/tile-extras.git set
svnImage [auto_execok svn]
set fil [file tempfile tempfnm] close $fil file delete $tempfnm
set tempRepo [file rootname $tempfnm] puts stdout tempRepo:\ $tempRepo
file mkdir $tempRepo
set svnCmd [list svn checkout $repository [file nativename $tempRepo]]
puts stdout svnCmd:\ $svnCmd eval $svnCmd
# Determine the tile-extras sources
set sourceFiles {keynav.tcl icons.tcl}
set targets [file nativename [file join $tempRepo trunk *.tcl]]
foreach filnam [split [svn ls $targets] \n] {
if {[string match *.tcl $filnam] && [lsearch $sourceFiles $filnam] < 0} {
lappend sourceFiles $filnam
}
}
And here is the result
$ tclsh foo.tcl
tempRepo: C:/cygwin64/tmp/TCL61838
svnCmd: svn checkout
https://github.com/jenglish/tile-extras.git {C:\cygwin64\tmp\TCL61838}
A C:\cygwin64\tmp\TCL61838/branches
A C:\cygwin64\tmp\TCL61838/trunk
A C:\cygwin64\tmp\TCL61838/trunk/README.md
A C:\cygwin64\tmp\TCL61838/trunk/dialog.tcl
A C:\cygwin64\tmp\TCL61838/trunk/doc
A C:\cygwin64\tmp\TCL61838/trunk/doc/dialog.n
A C:\cygwin64\tmp\TCL61838/trunk/doc/keynav.n
A C:\cygwin64\tmp\TCL61838/trunk/icons.tcl
A C:\cygwin64\tmp\TCL61838/trunk/keynav.tcl
A C:\cygwin64\tmp\TCL61838/trunk/license.terms
A C:\cygwin64\tmp\TCL61838/trunk/pkgIndex.tcl
Checked out revision 7.
svn: E155007: '/home/alan/C:\cygwin64\tmp\TCL61838\trunk\*.tcl' is not a working copy
while executing "exec {*}[auto_execok svn] {*}$args <#stdin >#stdout"
(procedure "svn" line 2)
invoked from within "svn ls $targets"
invoked from within "split [svn ls $targets] \n"
invoked from within "foreach filnam [split [svn ls $targets] \n] {
if {[string match *.tcl $filnam] && [lsearch $sourceFiles $filnam] < 0} {
lappend sourceFiles $filn..."
(file "foo.tcl" line 30)
$ ls /tmp/TCL61838/
$
The directory /tmp/TCL61838 is empty, so it seems the svn checkout command didn't complete completely happily. I also see an unpleasant mixture of forward slashes and backslashes being reported by svn.
Thanks in advance for any more help.
Given the error message, it looks like you're getting word boundaries wrong in the code that you've not shown us; while you might believe the code “boils down to” to that exec, it's not actually done that. Also, you've flipped the slashes in the URL which won't work, but that's probably a side-effect of something else.
Alas, I can't quite guess how to fix things for you. There's just too many options. I provide a suggestion below, but there's no telling for sure whether it will work out.
Diagnosis Methodology
The evidence for why I believe that the problem is what I say? This interactive session log (on OSX, but the generic behaviour should be the same):
% exec cat asdkfajh
cat: asdkfajh: No such file or directory
% exec "cat akjsdhfdkj"
couldn't execute "cat akjsdhfdkj": no such file or directory
% exec "cat aksdjhfkdf" skdjfghd
couldn't execute "cat aksdjhfkdf": no such file or directory
The first case shows an error from an external program. The second case shows an error due to no-such-program. The third case shows that arguments are not reported when erroring due to to no-such-program.
This lets me conclude that both C:\cygwin64\bin\svn.exe and its arguments (checkout, https:\github.com\jenglish\tile-extras.git and C:\cygwin64\tmp\TCL61416) were actually passed as a single argument to exec, a fairly common error, and that the problems lie in the preparatory code. You don't show us the preparatory code, so we can't truly fix things but we can make suggestions that address the common problems.
Suggested Approach
A good way to reduce these errors is to write a small wrapper procedure:
proc svn {args} {
# I add in the I/O redirections so svn can ask for a password
exec {*}[auto_execok svn] {*}$args <#stdin >#stdout
}
This would let you write your call to svn as:
svn checkout $theURL [file nativename $theDirectory]
and it would probably Just Work™. Also note that only the directory name goes through file nativename; the URL does not. (We could embed the call to file nativename in the procedure if we were making a specialised procedure to do checkouts, but there's too much variation in the full svn program to let us do that. The caller — you — has to deal with it.)

Communication between Tcl scripts

I was successfully able to redirect the standard output of a script called by my GUI (tcl/tk) using:
exec [info nameofexecutable] jtag.tcl >#$file_id
Here's a description of my system.
Now I want to be able to tell jtag.tcl to stop data acquisition (which is in an infinite loop) when I click "stop" button. Is it possible through exec or should I use open instead?
The exec command waits until the subprocess finishes before returning control to you at all (unless you run totally disconnected in the background). To maintain control you need to open a pipeline:
# Not a read pipe since output is redirected
set pipe [open |[list [info nameofexecutable] jtag.tcl >#$file_id] "w"]
You also need to ensure that the other process listens for when the pipe is closed or have some other protocol for telling the other end to finish. The easiest mechanism to do that is for the remote end to put the pipe (which is its stdin) into non-blocking mode and to check periodically for a quit message.
# Putting the pipe into nonblocking mode
fconfigure stdin -blocking 0
# Testing for a quit message; put this in somewhere it can be called periodically
if {[gets stdin] eq "quit"} {
exit
}
Then the shutdown protocol for the subprocess becomes this in the parent process:
puts $pipe "quit"
close $pipe
Alternatively, kill the subprocess and pick up the results:
exec kill [pid $pipe]
# Need the catch; this will throw an error otherwise because of the signal
catch {close $pipe}

Tcl open channel

how does one open a channel that is not a filename in tcl? I've read the docs but I'm not a programmer so I must not understand the open and chan commands because when I try to open a new custom channel
open customchannel1 RDWR
I get errors such as
couldn't execute "customchannel1": no such file or directory
And I'm fully aware that I don't do this correctly:
chan create read customchannel1
invalid command name "customchannel1" ...and... invalid command name "initialize"
All I want is two tcl scripts to be able to talk to each other. I thought I could use channels to do this.
I have, however, successfully created a socket test version of what I want:
proc accept {chan addr port} {
puts "$addr:$port says [gets $chan]"
puts $chan goodbye
close $chan
}
puts -nonewline "master or slave? "
flush stdout
set name [gets stdin]
if {$name eq "master"} {
puts -nonewline "Whats the port? "
flush stdout
set port [gets stdin]
socket -server accept $port
vwait forever
} else {
puts "slave then."
puts -nonewline "Whats the id? "
flush stdout
set myid [gets stdin]
set chan [socket 127.0.0.1 $myid]
puts $chan hello
flush $chan
puts "127.0.0.1:$myid says [gets $chan]"
close $chan
}
In the above example I can run 3 instances of the program: 2 'masters' with different port numbers, and a 'slave' that can talk to either one depending on the port/'id' it chooses.
If I knew how to open a channel with the open command instead of the socket command I could implement the above code without using sockets, or jimmy-rigging the ports to be used as uniq ids, but every example I can find opens files and writes out to files or standard out which you don't have to create in the first place.
Thanks for helping me understand these concepts and how to implement them better!
A channel is simply a high level method for working with already open files or sockets.
From the manual page:
This command provides several operations for reading from, writing to and otherwise manipulating open channels (such as have been created with the open and socket commands, or the default named channels stdin, stdout or stderr which correspond to the process's standard input, output and error streams respectively).
So what you are doing with sockets is correct. You can use the chan command to configure the open socket.
When connecting two scripts together, you might think in terms of using a pipeline. For example, you could run one script as a subordinate process of the other. The master does this:
set pipe [open |[list [info nameofexecutable] $thescriptfile] "r+"]
to get a bidirectional (because r+) pipeline to talk to the child, which can in turn just use stdout and stdin as normal.
Within a process, chan pipe is available, which returns a pair of channels that are connected by an OS anonymous pipe.
When working with these, it really helps if you remember to use fconfigure to turn -buffering to none. Otherwise you can get deadlocks while output to a pipe sits in a buffer somewhere, which you don't want. The ultimate answer to that is to use Expect, which uses Unix ptys instead of pipes, but you can be quite productive provided you remember to tune the buffering.

How to invoke windows 'move' CLI from TCL program

I am using TCL on a Windows 7 machine. And I need to invoke the Windows move command via exec. However I cannot get it to work.
I am aware that TCL has the file rename ability, but for reasons I cannot get into I'm being asked to use the Windows move CLI.
When I use the auto_execok with move, that command returns an empty string. I've also tried with the {*} but it never works.
% info tclversion
8.6
%
% move src dest
invalid command name "move"
%
% [auto_execok move] src dest
ambiguous command name "": after append apply array auto_execok auto_import auto _load auto_load_index auto_qualify binary break case catch cd chan clock close c oncat continue coroutine dict encoding eof error eval exec exit expr fblocked fc onfigure fcopy file fileevent flush for foreach format gets glob global history if incr info interp join lappend lassign lindex linsert list llength lmap load l range lrepeat lreplace lreverse lsearch lset lsort namespace open package pid pr oc puts pwd read regexp regsub rename return scan seek set socket source split s tring subst switch tailcall tclLog tell throw time trace try unknown unload unse t update uplevel upvar variable vwait while yield yieldto zlib
%
I've also looked at the contents of the auto_execok command using the info body auto_execok and it almost looks like they didn't add 'move' to the list of suported commands....
Any suggestions on how to interface with the Windows move command from a TCL program?
Is move built into cmd? You might try:
exec {*}[auto_execok cmd] /c move src dest
I don't have a windows box to test with right now.