The Answer Gang 
By Jim Dennis, Karl-Heinz Herrmann, Breen, Chris, and...
(meet the Gang) ...
the Editors of Linux Gazette... and
You!
Sending a keystroke to remote application
From Nick Urbanik
Answered By: Thomas Adam, Neil Youngman, Kapil Hari Paranjape
Dear Folks,
I am backing up my machine at work with Mondoarchive
http://www.mondorescue.org A great program! I ran the program at
work, so the terminal interface to the program is there, not here. My
friend put in a new DVD into the DVD writer, and now, after everyone
has gone home, I am looking at the log /var/log/mondo-archive.log (via
ssh), which says:
I am about to burn DVD #11 of the backup set. Please insert DVD and
press Enter.
My question:
How can I send the "\r" to the remote application?
[Neil]
I don't think you can, unless you've arranged a suitable mechanism in advance.
I can determine the process ID, I have full administrative control of
the machine, but I realise that I don't know how to do this simple
task! It seems like I should, and would be most interested if any of
you Answer Gang people could offer any suggestions.
[Neil]
If you could it would be a security hole. I believe it might have been
possible in older versions of X11 with the security turned off. If you find
it is possible then you really need to do something about it.
If you want to be able to do this remotely then you can run the program in
something like VNC or GNU screens, that will allow to detach from an X11
display or terminal session and reattach from somewhere else. If it wasn't
started it in such an environment it's way to late to do anything about it
now.
[Thomas]
Use 'expect'. It is why it was written. As an example:
#!/usr/bin/expect -f
# Created by Thomas Adam
# First startup the application in question
spawn <program_name> $arguments
# When we reach this prompt....
expect -re "Please insert DVD and press Enter"
# Send a carriage return
send "\r"
# Allow control of the program back to the user
interact
Clearly you will have to play about with it, but just save the file,
chmod +x it, change the values in it. The only thing I am concerned
about is the "inteact" statement, as it might not be needed. I am
unfamiliar with the application, so you'll have to tinker with it.
You will need to install 'expect'.
Yes, I have used expect briefly, but I do not understand how to apply
it in this case. The application is already running, and I don't know
how to send the keystroke to it.
If I restarted it, then ran it over the ssh session, then, well, I
would be able to press enter here easily, but would then face the same
problem at work; being unable to send a keypress to the application to
the console from which it is being controlled.
[Thomas]
Tough. It cannot be done -- you need a program to talk to it.
I am
sure there is a way you can resume from a certain position... My best
advice is to stop it, and use expect.
[Kapil]
In case the remote application is running in an X-window, you could
- install "rfb" or "x11vnc" on the remote machine.
- start x11vnc on the remote machine.
- install a vnc viewer on the local machine.
- start the vnc viewer on the local machine.
- make the appropriate changes to the remote application.
This is admittedly a very brief summary. You must also insert the step
(a+b)/2 which consists of reading the documentation for vnc.
If you do not have the application running in an X-window but on the
console then there is also a vnc server for the console
somewhere...(mumbles and runs "apt-cache search vnc")...ah, there it is
Package: linuxvnc
Description: A VNC server to monitor a tty
With linuxvnc you can export your currently running text
sessions to any VNC client. So it can be useful, if you want to
move to another computer without having to log out and if
you've forgotten to attach a 'screen' session to it, or to help
a distant colleague to solve a problem.
If fd0 has been closed then you are truly without any option except kill
<proc-number>. You are also stuck with an application that is making
the mistake of waiting for a read without having an open input source!
That looks like the solution! I thought it may be very difficult to
do this without starting modoarchive in a screen session. I will
investigate how to use this. Thank you.
This page edited and maintained by the Editors of Linux Gazette
HTML script maintained by
Heather Stern of
Starshine Technical Services,
http://www.starshine.org/
Each TAG thread Copyright © its authors, 2004
Published in issue 105 of Linux Gazette August 2004