TTY Stdin Writer PRO

Requires Scripting PRO

Represents a writable input stream for a TTY (teletypewriter) session opened over SSH. This class allows writing text to the remote terminal’s standard input and resizing the terminal window dynamically.

This class is typically returned from methods such as SSHClient.withPTY() and SSHClient.withTTY().


Methods

write(data: string): Promise<void>

Writes the given string to the standard input of the TTY session.

Parameters:

  • data (string): The string data to send to the remote terminal’s stdin. This can include control characters (e.g., "\r" for Enter, "\x03" for Ctrl+C).

Returns:

  • A Promise that resolves when the data has been successfully written to the TTY stream.

Example:

1const writer = await ssh.withTTY({
2  onOutput: (text) => {
3    console.log("Output:", text)
4    return true
5  }
6})
7await writer.write("ls -la\n")

changeSize(options: { cols: number; rows: number; pixelWidth: number; pixelHeight: number }): Promise<void>

Resizes the remote terminal window. This is useful for applications that rely on terminal dimensions, such as text editors or full-screen tools (e.g., vim, htop).

Parameters:

  • options (object): An object describing the new terminal dimensions:

    • cols (number): The number of character columns in the terminal (e.g., 80).

    • rows (number): The number of character rows in the terminal (e.g., 24).

    • pixelWidth (number): The pixel width of the terminal window. Use 0 if not applicable.

    • pixelHeight (number): The pixel height of the terminal window. Use 0 if not applicable.

Returns:

  • A Promise that resolves once the terminal size has been successfully updated.

Example:

1await writer.changeSize({
2  cols: 100,
3  rows: 30,
4  pixelWidth: 0,
5  pixelHeight: 0
6})

Usage Example

1const ssh = await SSHClient.connect({
2  host: "192.168.1.10",
3  authenticationMethod: SSHAuthenticationMethod.passwordBased("user", "password")
4})
5
6const writer = await ssh.withPTY({
7  term: "xterm",
8  onOutput: (text, isStderr) => {
9    console.log(text)
10    return true
11  }
12})
13
14await writer.write("top\n")
15
16// Resize the terminal after 2 seconds
17await new Promise(resolve => setTimeout(resolve, 2000))
18await writer.changeSize({
19  cols: 120,
20  rows: 40,
21  pixelWidth: 0,
22  pixelHeight: 0
23})