React Native
React Native
SSH to a Cisco Switch - Processing "More" Responses
See more SSH Examples
Demonstrates connecting to a Cisco switch, entering privileged mode with the ena command, and running a command whose response is paged. Each page ends with --More-- and requires a SPACE character to request the next page.
Note: QuickShell allocates a PTY, which is what a network device console expects. The device presents an interactive prompt, and each command's output is read up to the next prompt.
Background: Paged output is the console equivalent of a pager like
more, and a client must reproduce the keypress a human would make. Matching a set of patterns is what makes this tractable: each read ends either at the device prompt (output complete) or at --More-- (another page waiting), and the code branches accordingly. Note the prompt pattern includes the device name rather than a bare #, because that character also appears inside configuration text and would otherwise match far too early.Chilkat React Native Downloads
import { Ssh, StringArray, StringBuilder } from '@chilkat/react-native'
async function chilkatExample() {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Demonstrates connecting to a Cisco switch, entering privileged mode, and running a command
// whose response is paged -- each page ends with "--More--" and requires a SPACE character to
// request the next page.
const ssh = new Ssh();
const port = 22;
try {
await ssh.connectAsync('172.16.16.100', port);
} 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;
}
const channelNum = await ssh.quickShellAsync();
if (channelNum < 0) {
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 = true;
// In unprivileged mode the switch prompt ends with ">".
try {
await ssh.channelReceiveUntilMatchAsync(channelNum, '>', 'utf-8', caseSensitive);
} catch {
console.log(ssh.lastErrorText);
return;
}
let received: string;
try {
received = ssh.getReceivedText(channelNum, 'utf-8');
} catch {
console.log(ssh.lastErrorText);
return;
}
console.log(received);
// Send "ena" to enter privileged mode. Cisco devices expect a bare CR to terminate a command.
try {
await ssh.channelSendStringAsync(channelNum, 'ena\r', 'utf-8');
} catch {
console.log(ssh.lastErrorText);
return;
}
try {
await ssh.channelReceiveUntilMatchAsync(channelNum, 'Password:', 'utf-8', caseSensitive);
} catch {
console.log(ssh.lastErrorText);
return;
}
try {
received = ssh.getReceivedText(channelNum, 'utf-8');
} catch {
console.log(ssh.lastErrorText);
return;
}
console.log(received);
// The enable password is separate from the login password, and likewise should come from a
// secure source rather than being hard-coded.
const enablePassword = 'myEnablePassword';
try {
await ssh.channelSendStringAsync(channelNum, enablePassword, 'utf-8');
} catch {
console.log(ssh.lastErrorText);
return;
}
try {
await ssh.channelSendStringAsync(channelNum, '\r', 'utf-8');
} catch {
console.log(ssh.lastErrorText);
return;
}
// In privileged mode the prompt ends with "#" instead of ">".
try {
await ssh.channelReceiveUntilMatchAsync(channelNum, '#', 'utf-8', caseSensitive);
} catch {
console.log(ssh.lastErrorText);
return;
}
try {
received = ssh.getReceivedText(channelNum, 'utf-8');
} catch {
console.log(ssh.lastErrorText);
return;
}
console.log(received);
// Run a command whose output is delivered a page at a time.
try {
await ssh.channelSendStringAsync(channelNum, 'show running-config\r', 'utf-8');
} catch {
console.log(ssh.lastErrorText);
return;
}
// Each read ends either at the device prompt (output complete) or at "--More--" (another page
// is waiting). Match the full prompt rather than a bare "#", because "#" also appears inside
// configuration text and would match too early.
const saMatch = new StringArray();
saMatch.append('MySwitchName#');
saMatch.append('--More--');
const sbReceived = new StringBuilder();
let moreComing = true;
while (moreComing) {
try {
await ssh.channelReceiveUntilMatchNAsync(channelNum, saMatch, 'utf-8', caseSensitive);
} catch {
console.log(ssh.lastErrorText);
return;
}
sbReceived.clear();
let pageText: string;
try {
pageText = ssh.getReceivedText(channelNum, 'utf-8');
} catch {
console.log(ssh.lastErrorText);
return;
}
sbReceived.append(pageText);
console.log(pageText);
// If this page ended with "--More--", send a SPACE to request the next page.
moreComing = sbReceived.contains('--More--', caseSensitive);
if (moreComing) {
try {
await ssh.channelSendStringAsync(channelNum, ' ', 'utf-8');
} catch {
console.log(ssh.lastErrorText);
return;
}
}
}
ssh.disconnect();
}