Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i have a psuedoterminal open in a terminal window (xfce4terminal to be exact). i want to stuff some input into it and have the shell on the slave side of the pty get it like any other typed input. everything was started under the same non-root user (there are no cross user issues). i see two possible ways, though there may be more. the 1st is that the terminal program may provide an interface to do this. the 2nd is writing directly to the master side of the psuedoterminal. i do have the tty device name from the tty command. how can i stuff type-in input into this psuedoterminal? i don't need to read the output though in the future that could be useful.
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,800
Rep:
Quote:
Originally Posted by pan64
if I understand well it is a simple redirection:
command > /dev/pts/<tty>
Unless I'm totally misunderstanding the description in #1, that doesn't meet the requirement of:
Quote:
i want to stuff some input into it and have the shell on the slave side of the pty get it like any other typed input.
(Emphasis mine)
Which I interpreted as: Send text to the pty as though it was actually typed in at that pty and executed. "echo env > /dev/pts/NN" merely displays "env" on that pty, not display the environment on it.
I suspect there's no easy way -- writing to another terminal's stdin via some command switch, etc., anyway -- to do this. Perhaps if the target pty was running some "odd" program instead of a normal shell?
Without fully understanding what you wrote, I'm going to ask you one question:
Have you heard of expect?
yes i have. and i have even used it, before. have you? have you read its man page? expect creates its own pty to provide a controlling tty to the process it is starting. my goal is to "insert" into an existing pty that is controlling an existing shell to provide some type-in.
if you know how to get expect to "connect" into an existing pty, please say where you read how to make it do that.
under the old way of doing psuedoterminals there were separate tty and pty devices with matching numbers. this kind of thing was trivial: just write to the other device and was like typed input, though more than one character at a time would be too fast and could overflow some buffers. the more at once the worse the chances.
inserting a program in front of the shell could be plausible. but i would have to set that up to run the extra process all the time. but it might be doable to handle bulk strings better, like pacing them at a configurable speed. i usually run many shell terminals that just sit idle ready to be used. at this moment the total in use and idle is 98. for me this would mean adding 98 more process, though most would be idle.
Skaperen, can you please rephrase and expand on what you are aiming to accomplish?
i have many already created, running, and used, terminal windows with a controlled shell (usually bash, but not always). at rare times, i want to run a script in the background that "types" into that window. it should appear that what the script "typed" in was typed in through the human interface by a human.
the script doing the typing will be running the same effective userid as the terminal and shell so the ability to do this fake typing must not depend on having root or sudo privileges.
at the time the terminal and shell are started, it is unknown which might need to be typed in to.
there are many different userids and instances of X (usually more than 15) running with many (usually 10) virtual screens, st up. each userid has one instance of X but different virtual screens may be doing scrpted type-ins concurrently. this means a general type-in to the X server is not specific enough ... some terminals may not be displaying because they are in an inactive virtual screen.
if i need to do mods to make this work, i would add an option to the terminal to add an interface (named unix socket on which the userid of the connecting process can be verified). this interface would provide a stream for input and a stream for output (what is to be rendered on the screen), as well as other features like access the graphical content, and controls like resizing the window, changing colors, and so on. a C program could make it easy for a script to connect.
but why? What is the point? usually you need to run a server which can listen on some port/socket/whatever to accept external requests. A general interactive shell - which is running in a terminal - is different, it is waiting for input on its tty. The only way to feed some commands to bash is to redirect its stdin, like this:
Code:
cat shellscript | bash
In this case you need to keep stdin open and that also means you have a server listening on a socket and not an interactive shell.
Additionally much easier to execute the command directly instead of sending it to another shell...
The other way could be to rewrite the X server to be able to send keyboard events to windows/terminals/whatever which are not in focus or on screen.
Or to write your own pty handler which can somehow accept events from another app, socket, whatever...
That would launch a terminal with a multiplexed session within it and within that an instance of ping. It is shown using xterm but any other terminal emulator can be used. Some terminal emulators have a lot of options, as does tmux.
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,800
Rep:
Quote:
Originally Posted by Skaperen
i have many already created, running, and used, terminal windows with a controlled shell (usually bash, but not always). at rare times, i want to run a script in the background that "types" into that window. it should appear that what the script "typed" in was typed in through the human interface by a human.
I'm thinking that cssh(1) sounds like it'd work except it creates new terminal windows. Perhaps taking a look at the source for that utility would generate ideas.
usually you need to run a server which can listen on some port/socket/whatever to accept external requests.
i used to not need to.
well, tty emulation was reading it and there used to be an individual device that could be opened (by another process) to write there.
BTDT.
Quote:
Originally Posted by pan64
A general interactive shell - which is running in a terminal - is different, it is waiting for input on its tty.
and ready to handle various signals.
Quote:
Originally Posted by pan64
The only way to feed some commands to bash is to redirect its stdin
the idea is to do this on a shell that is already running with input from the terminal and will continue to run after the stuffed input is given, reading from that same terminal.
Quote:
Originally Posted by pan64
like this:
Code:
cat shellscript | bash
that is a whole separate and new instance of a shell, not the specific existing one that is the target for this stuffed input.
Quote:
Originally Posted by pan64
In this case you need to keep stdin open and that also means you have a server listening on a socket and not an interactive shell.
Additionally much easier to execute the command directly instead of sending it to another shell...
it's not necessarily a complete command. it may be partial input of a command. or it may be input to the command it does. it may also be internal shell commands.
Quote:
Originally Posted by pan64
The other way could be to rewrite the X server to be able to send keyboard events to windows/terminals/whatever which are not in focus or on screen.
Or to write your own pty handler which can somehow accept events from another app, socket, whatever...
or rewrite the terminal emulator application to do similar. to X the terminal is just another app. or rewrite QEMU to ... well, no
who knows, maybe a means to do that has already been done.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.