[vox-tech] writing to a shell instance

Bill Broadley bill at broadley.org
Wed Oct 26 18:57:59 PDT 2011


On 10/26/2011 03:34 PM, Norm Matloff wrote:
> 
> Here's what I'd like to do.  I'm running code, in this case Python, in
> xterm A (replace by your favorite terminal emulator), and want that code
> to write to xterm B, just as if I had typed directly into xterm B.
> 
> Say for example I want to run the ls command in xterm B, but do so via
> some action in A.  Say the latter is /dev/pts/8.  I could run the Python
> code
> 
> import os
> os.system('echo "ls" > /dev/pts/8')
> 
> I have 2 questions:
> 
> 1.  How do I get the end-of-line character in there, so that the ls
> command actually runs?  I've tried "ls\n", "ls \r\n" and lots of
> variants, e.g.

Er, getting in there should be easy.  But your writing to the same
device that the shells OUTPUT is writing to.  Not to the shells input
which is passed through X when the window manage decides that the focus
is in that window.

Sounds like you want something like:
nc -l 9999 | bash

That way anything you write to port 9999 gets executed in the target
window because bash's stdin is listening to the port.

> echocmd = 'echo "ls'+chr(14)+chr(10)+'" > /dev/pts/13'
> os.system(echocmd)
> 
> But no matter what I try, it doesn't work.  The "ls" does appear in
> xterm B, and the newlines, but it's still expecting more input from me.
> If I manually hit Enter in xterm B, then it works.

Woah, really?  You sure?  I just tried it, I send a ls to /dev/pts/7 and
I see it in the target window, if I hit return in the target window I
just get a new prompt.  That fits with my idea of how this works since
you aren't writing to bash's input.

> I know this must be simple ridiculously simple, but I don't see it.
> 
> Yes, I know I could use a pipe here, but I want to retain the ability to
> manually type in xterm B, i.e. I want to be able to input there either
> by physically typing there or by having the program in xterm B do it.

Ah, that makes it more complicated, seems like you want the equivalent
of tee, but for input instead of output.  A tiny bit of perl/python/c
code should be able to listen to say a socket and interactive input and
exec both.  I often do similar with screen -x to let two people share a
shell.  Not surprised that doesn't work under windows.

> Maybe I can launch xterm A via a pipe in the first place?  I've tried
> that a bit, but don't have enough experience with pipes to see how to
> make that work either.

I'd be a little surprised if you could get an xterm to accept input from
a pipe AND what you type.  Especially since it's bash listening/running
commands and not the terminal.

> One solution is to use "screen," which is what I'm doing currently,
> but some people would like to use my program from Windows.
> 
> 2.  Which brings me to my next question:  How can I do this in Windows?
> (Not Cygwin.)

I don't run windows, but seems like there's likely a python or perl port
to windows (not cygwin) and portability for something so simple seems
very likely.


More information about the vox-tech mailing list