Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send `eof` signal, over a socket, to a command running in remote shell?

How to send eof signal, over a socket, to a command running in remote shell?

I've programmed in Python, using sockets, a remote shell application, where I send commands to be executed on another PC.

Everything works fine (for most commands), except a command like cat > file is causing me problems.

Normally, I would terminate the command above with CTRL + D (eof signal), but pressing CTRL + D in my client, doesn't send the signal to the remote shell. Therefore I have no means of terminating the command and I'm stuck.

Anyone have suggestions ?

like image 507
Shuzheng Avatar asked Aug 15 '15 09:08

Shuzheng


People also ask

How do I pass EOF in terminal?

the “end-of-file” (EOF) key combination can be used to quickly log out of any terminal. CTRL-D is also used in programs such as “at” to signal that you have finished typing your commands (the EOF command). key combination is used to stop a process. It can be used to put something in the background temporarily.

How do you send EOF to pipe?

You cannot “send EOF”. There is simply no “EOF character” that would go through the pipe. Typically, linux programs read from a file descriptor using read (2) in blocking mode, filling a buffer with the received data, and returning the amount of characters read.

What is the use of EOF in shell scripting?

This operator stands for the end of the file. This means that wherever a compiler or an interpreter encounters this operator, it will receive an indication that the file it was reading has ended. Similarly, in bash, the EOF operator is used to specify the end of the file.


1 Answers

eof is not a signal but is implemented by the tty driver as a read of length 0 when you type ctrl-d.

If your remote is not running in a tty then you cannot generate an eof as you cannot send a packet that reads as of length 0. However if you run a program like script /dev/null as the first command in your remote shell, then this will envelop your shell inside a pseudo-tty, and you will be able to send a real ctrl-d character (hex 0x04) and the pty will convert this to eof and end a cat, for example. Send a stty -a to the remote to check that eol is enabled in your pty.


stty -a on my terminal says lnext = ^V (literal-next char) so I can type ctrl-vctrl-d to input a real hex 0x04 char.

I chose script as I know it effectively interposes a pseudo-tty in the communication, and does nothing to the data stream. This is not its original purpose (see its man page), but that doesn't matter.

like image 158
meuh Avatar answered Nov 14 '22 22:11

meuh