LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 09-01-2020, 08:44 PM   #1
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,681
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
input to a psuedoterminal


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.
 
Old 09-01-2020, 08:58 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Without fully understanding what you wrote, I'm going to ask you one question:

Have you heard of expect?
 
Old 09-02-2020, 02:36 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
if I understand well it is a simple redirection:
command > /dev/pts/<tty>
 
Old 09-02-2020, 05:17 AM   #4
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,800

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by pan64 View Post
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?
 
Old 09-02-2020, 07:31 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
ah so. in that case something need to catch that "stdin" and execute.
You can [try to] execute bash using stdin as command input and use a socket.
 
Old 09-02-2020, 05:22 PM   #6
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,681

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by dugan View Post
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.
 
Old 09-02-2020, 05:27 PM   #7
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,681

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
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.
 
Old 09-03-2020, 01:33 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
Sending a command to another terminal instead of executing it is pointless. You can execute it directly, the result will be exactly the same.
 
Old 09-03-2020, 07:31 AM   #9
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,295
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
Skaperen, can you please rephrase and expand on what you are aiming to accomplish?
 
Old 09-04-2020, 07:03 PM   #10
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,681

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by Turbocapitalist View Post
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.

does that explain it enough?
 
Old 09-05-2020, 03:53 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
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...
 
Old 09-05-2020, 04:19 AM   #12
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,295
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
Or maybe the task can be shoehorned into xterm and tmux instead:

Code:
DISPLAY=:0.0 xterm -x sh -c 'tmux new-session -s skaperen -n a_script \; \
        send-keys -t skaperen:a_script "ping -w www.google.com" C-m'
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.
 
Old 09-07-2020, 12:10 PM   #13
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,800

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by Skaperen View Post
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.

Just a thought...
 
Old 09-08-2020, 01:04 PM   #14
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,681

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by pan64 View Post
but why?
do i have to answer that?

Quote:
Originally Posted by pan64 View Post
What is the point?
or that?

Quote:
Originally Posted by pan64 View Post
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 View Post
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 View Post
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 View Post
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 View Post
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 View Post
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.
 
Old 09-08-2020, 01:07 PM   #15
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,681

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by Turbocapitalist View Post
Or maybe the task can be shoehorned into xterm and tmux instead
or maybe it has already been done.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PsuedoTerminal problem cannot read from slave max1232 Linux - Newbie 2 03-04-2009 01:08 AM
Repeated "input: AT Translated Set 2 keyboard as /class/input/input" messages AcerKev Mandriva 2 09-16-2007 08:35 AM
Ctrl+Shift Unicode input gone, after installing Japanese Input Methodes polemon Linux - Newbie 1 09-20-2005 05:17 PM
Sendmail: timeout waiting for input from local during Draining Input andrewstr Linux - Software 0 07-14-2004 01:43 PM
my mouse input is takes as keyboard input in BASH e1000 Slackware 5 12-08-2003 03:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration