Sample code for 30+ languages & platforms
React Native

Request a Pseudo-Terminal (PTY) over SSH

See more SSH Examples

Demonstrates the Chilkat Ssh.SendReqPty method, which requests a pseudo-terminal for a session channel. The arguments are the channel number, the terminal type, the character width and height, and the pixel width and height. It must be sent before the primary exec or shell request.

Background: A PTY makes the remote side behave as though a real terminal is attached, which matters for programs that draw screens, prompt for passwords, or colorize output — many refuse to run interactively without one. The terminal type (such as xterm) tells the remote program what escape sequences it may use. Pixel dimensions are commonly 0, meaning unspecified.

Chilkat React Native Downloads

React Native
import { Ssh } from '@chilkat/react-native'

async function chilkatExample() {
  // Demonstrates the Ssh.SendReqPty method, which requests a pseudo-terminal for a session
  // channel.  The 1st argument is the channel number, the 2nd is the terminal type, the 3rd and
  // 4th are the character width and height, and the 5th and 6th are the pixel width and height.
  // It must be sent before the primary exec or shell request.

  const ssh = new Ssh();

  const sshPort = 22;
  try {
    await ssh.connectAsync('ssh.example.com', sshPort);
  } catch {
    console.log(ssh.lastErrorText);
    return;
  }

  // Normally you would not hard-code the password in source.  You should instead obtain it
  // from an interactive prompt, environment variable, or a secrets vault.
  const password = 'mySshPassword';

  try {
    await ssh.authenticatePwAsync('mySshLogin', password);
  } catch {
    console.log(ssh.lastErrorText);
    return;
  }

  // Open a session channel.  A negative return value indicates failure.
  const channelNum = await ssh.openSessionChannelAsync();
  if (channelNum < 0) {
    console.log(ssh.lastErrorText);
    return;
  }

  // Request an 80x24 xterm.  Pixel dimensions of 0 mean unspecified.
  const termType = 'xterm';
  const widthInChars = 80;
  const heightInChars = 24;
  const widthInPixels = 0;
  const heightInPixels = 0;
  try {
    await ssh.sendReqPtyAsync(channelNum, termType, widthInChars, heightInChars, widthInPixels, heightInPixels);
  } catch {
    console.log(ssh.lastErrorText);
    return;
  }

  // Start the shell now that the PTY is allocated.
  try {
    await ssh.sendReqShellAsync(channelNum);
  } catch {
    console.log(ssh.lastErrorText);
    return;
  }

  ssh.disconnect();
}