React Native
React Native
Start a Remote Shell with QuickShell
See more SSH Examples
Demonstrates the Chilkat Ssh.QuickShell method, which starts a remote shell using the simplified sequence OpenSessionChannel → SendReqPty → SendReqShell. It takes no arguments and returns the shell channel number, or -1 on failure. The default PTY size is 80 columns by 24 rows.
Background: Note that
QuickShell does allocate a PTY, so unlike a bare SendReqShell the remote shell runs interactively: it prints a command prompt, echoes the commands you send, and runs interactive login scripts. That is convenient for terminal-style work but means the output contains prompts and echoed input that scripted parsing must tolerate. The method returns as soon as the shell request is accepted — it does not consume the initial login text or prompt, so read and discard that before relying on command output.Chilkat React Native Downloads
import { Ssh } from '@chilkat/react-native'
async function chilkatExample() {
// Demonstrates the Ssh.QuickShell method, which starts a remote shell using the simplified
// sequence OpenSessionChannel, SendReqPty, and SendReqShell. It takes no arguments and returns
// the shell channel number, or -1 on failure. The default PTY size is 80 columns by 24 rows.
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;
}
// Start the shell. Note that QuickShell allocates a PTY, so the remote shell runs
// interactively: it prints a command prompt and echoes the commands that are sent to it.
const channelNum = await ssh.quickShellAsync();
if (channelNum < 0) {
console.log(ssh.lastErrorText);
return;
}
// QuickShell returns as soon as the shell request is accepted -- it does not wait for or
// consume the initial login text or prompt.
try {
await ssh.channelSendStringAsync(channelNum, 'uname -a; echo DONE_MARKER\n', 'utf-8');
} catch {
console.log(ssh.lastErrorText);
return;
}
// IMPORTANT: Set a read timeout before receiving until a match. ReadTimeoutMs defaults to 0,
// which means no limit -- without it, this call waits forever if the received data never
// contains a match.
ssh.readTimeoutMs = 15000;
const caseSensitive = false;
try {
await ssh.channelReceiveUntilMatchAsync(channelNum, 'DONE_MARKER', 'utf-8', caseSensitive);
} catch {
console.log(ssh.lastErrorText);
return;
}
let output: string;
try {
output = ssh.getReceivedText(channelNum, 'utf-8');
} catch {
console.log(ssh.lastErrorText);
return;
}
console.log(output);
ssh.disconnect();
}