Reply to "write" messages

I wonder if anyone knew of a way to capture and recognize incoming write commands to a terminal. I tried using script -f and then using tail -f to keep track of the output in the while loop, but since the terminal trace I do not initiate write , it is not output. Unfortunately, I do not have root privileges and I can not play with glasses or screendump, I wonder if anyone knew a method for this?

Example:

 Terminal 1 $ echo hello | write Terminal 2 Terminal 2 $ Message from Terminal 1 on pts/0 at 23:48 ... hello EOF *Cause a trigger from which I can send a return message 
+7
source share
1 answer

I can't think of any obvious way to do this. That's why...

Your shell receives its input and sends its output to some terminal Device:

  +-----------+ | | | bash | | | +-----------+ ^ | | | | v +----------------------+ | some terminal device | +----------------------+ 

When write writes to the terminal, it sends data directly to the same terminal device. It does not come close to your shell:

  +-----------+ +-----------+ | | | | | bash | | write | | | | | +-----------+ +-----------+ ^ | | | | | | vv +----------------------+ | some terminal device | +----------------------+ 

So, so that you can grab what sent write , you need some hook provided by the terminal device itself, and I don't think you can use this to do this.

So, how does the script work and why does it not output write output?

script cannot connect to terminal device. He really wants to insert himself between your shell and your terminal, but there is no direct way to do this directly.

Thus, he creates a new terminal device (pseudo-terminal, also known as "pty") and launches a new shell in it. Pty consists of two sides: "master", which is just a stream of bytes, and "subordinate", which looks just like any other interactive terminal device.

The new shell joins the subordinate side, and the script controls the main side - this means that it can save a stream of bytes to a file, as well as forward them between the new shell and the original terminal:

  +-----------+ | | | bash | | | +-----------+ ^ | | | | v +-----------------+ <=== slave side of pty -- presents the interface of | pseudo-terminal | an interactive terminal +-----------------+ <=== master side of pty -- just a stream of bytes ^ | | v +-----------+ | | | script | | | +-----------+ ^ | | | | v +----------------------+ | some terminal device | +----------------------+ 

Now you can see that write to the source terminal goes around everything, as it was in the simple case above:

  +-----------+ | | | bash | | | +-----------+ ^ | | | | v +-----------------+ <=== slave side of pty -- presents the interface of | pseudo-terminal | an interactive terminal +-----------------+ <=== master side of pty -- just a stream of bytes ^ | | v +-----------+ +-----------+ | | | | | script | | write | | | | | +-----------+ +-----------+ ^ | | | | | | vv +----------------------+ | some terminal device | +----------------------+ 

If you write data to the working side of the new terminal here, you will see that the output is displayed because it will appear in the data stream on the main side that the script sees. You can find the name of the new pty using the tty command from the shell inside the script .

Unfortunately, this does not help with write , since you probably cannot write to it: your login session is connected to the original terminal and not the new one, and write - probably complain that you are not logged in. But if you, for example, echo hello >/dev/pts/NNN , you will see that it is displayed in the output of the script .

+3
source

All Articles