React Native
React Native
Set a TTY Mode for an SSH PTY Request
See more SSH Examples
Demonstrates the Chilkat Ssh.SetTtyMode method, which adds or updates one TTY mode to be included in a later SendReqPty request. The first argument is the mode name and the second is its integer value. Call it once per mode, before requesting the PTY.
Background: TTY modes are the terminal settings a PTY is created with — the same knobs
stty exposes. The classic use is SetTtyMode("ECHO", 0), which stops the terminal from echoing input; that is how a password prompt keeps typed characters off the screen. Modes accumulate on the object and are sent with the next PTY request.Chilkat React Native Downloads
import { Ssh } from '@chilkat/react-native'
async function chilkatExample() {
// Demonstrates the Ssh.SetTtyMode method, which adds or updates one TTY mode to be included in
// a later SendReqPty request. The 1st argument is the mode name and the 2nd is its integer
// value. Call it once per mode, before SendReqPty.
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 that the allocated terminal not echo input.
try {
ssh.setTtyMode('ECHO', 0);
} catch {
console.log(ssh.lastErrorText);
return;
}
// The modes set above are included in the PTY request.
try {
await ssh.sendReqPtyAsync(channelNum, 'xterm', 80, 24, 0, 0);
} catch {
console.log(ssh.lastErrorText);
return;
}
try {
await ssh.sendReqShellAsync(channelNum);
} catch {
console.log(ssh.lastErrorText);
return;
}
ssh.disconnect();
}