Fork 0
A scriptlet for easy writing commands to a virtual terminal. https://spacecruft.org/deepcrayon/wrvtcmd
Go to file
root 7293a6a17f CONFIG_LEGACY_TIOCSTI 2023-09-17 10:20:56 -06:00
.gitignore ignore tmp files 2023-09-16 12:28:57 -06:00
LICENSE MIT license 2023-09-16 12:05:24 -06:00
README.md CONFIG_LEGACY_TIOCSTI 2023-09-17 10:20:56 -06:00
wrvtcmd wrvtcmd --- write command to virtual terminal 2023-09-16 12:38:19 -06:00



wrvtcmd is a Python script to write commands to a virtual terminal.

By "virtual terminal", it is meant what is often called the "console", which is slightly different. In this case it is the "virtual terminal" controlled by commands such as chvt (e.g. sudo chvt 3).

Writing to the console can make text appear, but it won't make it execute as a command in a shell. Writing to the virtual terminal, such as /dev/tty1, commands can be run, login on console, etc.

This may be used in a situation where you have ssh access to a machine, but no X/Wayland/VNC running, but you need to run as if you are in front of the machine. This in effect simulates an operator standing in front of a machine, typing on the local keyboard.


Build thusly.

Under Debian stable (Bookworm/12). No external pip packages are required; standard Python.

git clone https://spacecruft.org/deepcrayon/wrvtcmd
cd wrvtcmd/


Help output:

$ ./wrvtcmd -h
usage: wrvtcmd [-h] [-c CMD] [-t TTY]

write command to virtual terminal

  -h, --help         show this help message and exit
  -c CMD, --cmd CMD  command to run (default echo)
  -t TTY, --tty TTY  TTY to use (default /dev/tty1)


Use examples:

sudo ./wrvtcmd
sudo ./wrvtcmd -c "date -uR"
sudo ./wrvtcmd -t /dev/tty2 --cmd "clear"




I don't know of another application that specifically does this. There are a few old snippets in perl, C, and Python in StackExchange and blogs, but just small examples.

Some linkies:

fcntl — The fcntl and ioctl system calls

Faking input with IOCTL/TIOCSTI

RichardBronosky writevt.c

Construct a command by putting a string into a tty

Unable to fake terminal input with termios.TIOCSTI

send command to another terminal

Can I send some text to the STDIN of an active process running in a screen session?

Linux Kernel Note

I see this configuration option and note in a 6.5 kernel:

Historically the kernel has allowed TIOCSTI, which will push
characters into a controlling TTY. This continues to be used
as a malicious privilege escalation mechanism, and provides no
meaningful real-world utility any more. Its use is considered
a dangerous legacy operation, and can be disabled on most

Say Y here only if you have confirmed that your system's
userspace depends on this functionality to continue operating

Processes which run with CAP_SYS_ADMIN, such as BRLTTY, can
use TIOCSTI even when this is set to N.

This functionality can be changed at runtime with the
dev.tty.legacy_tiocsti sysctl. This configuration option sets
the default value of the sysctl.

Type  : bool
Defined at drivers/tty/Kconfig:152
Prompt: Allow legacy TIOCSTI usage
Depends on: TTY [=y]
    -> Device Drivers
      -> Character devices
        -> Enable TTY (TTY [=y])
          -> Allow legacy TIOCSTI usage (LEGACY_TIOCSTI [=y])




Copyright © 2023, Jeff Moe.