Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

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 Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.StringArray,
  Chilkat.StringBuilder,
  Chilkat.Ssh;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  ssh: TSsh;
  port: Integer;
  password: string;
  channelNum: Integer;
  caseSensitive: Boolean;
  received: string;
  enablePassword: string;
  saMatch: TStringArray;
  sbReceived: TStringBuilder;
  moreComing: Boolean;
  pageText: string;

begin
  success := False;

  //  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.

  ssh := TSsh.Create;

  port := 22;
  success := ssh.Connect('172.16.16.100',port);
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  //  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.
  password := 'mySshPassword';

  success := ssh.AuthenticatePw('mySshLogin',password);
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  channelNum := ssh.QuickShell();
  if (channelNum < 0) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  //  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;

  caseSensitive := True;

  //  In unprivileged mode the switch prompt ends with ">".
  success := ssh.ChannelReceiveUntilMatch(channelNum,'>','utf-8',caseSensitive);
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  received := ssh.GetReceivedText(channelNum,'utf-8');
  if (ssh.LastMethodSuccess = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;
  WriteLn(received);

  //  Send "ena" to enter privileged mode.  Cisco devices expect a bare CR to terminate a command.
  success := ssh.ChannelSendString(channelNum,'ena' + #13,'utf-8');
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  success := ssh.ChannelReceiveUntilMatch(channelNum,'Password:','utf-8',caseSensitive);
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  received := ssh.GetReceivedText(channelNum,'utf-8');
  if (ssh.LastMethodSuccess = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;
  WriteLn(received);

  //  The enable password is separate from the login password, and likewise should come from a
  //  secure source rather than being hard-coded.
  enablePassword := 'myEnablePassword';
  success := ssh.ChannelSendString(channelNum,enablePassword,'utf-8');
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;
  success := ssh.ChannelSendString(channelNum,#13,'utf-8');
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  //  In privileged mode the prompt ends with "#" instead of ">".
  success := ssh.ChannelReceiveUntilMatch(channelNum,'#','utf-8',caseSensitive);
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  received := ssh.GetReceivedText(channelNum,'utf-8');
  if (ssh.LastMethodSuccess = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;
  WriteLn(received);

  //  Run a command whose output is delivered a page at a time.
  success := ssh.ChannelSendString(channelNum,'show running-config' + #13,'utf-8');
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  //  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.
  saMatch := TStringArray.Create;
  saMatch.Append('MySwitchName#');
  saMatch.Append('--More--');

  sbReceived := TStringBuilder.Create;
  moreComing := True;

  while moreComing do
    begin
      success := ssh.ChannelReceiveUntilMatchN(channelNum,saMatch,'utf-8',caseSensitive);
      if (success = False) then
        begin
          WriteLn(ssh.LastErrorText);
          Exit;
        end;

      sbReceived.Clear();
      pageText := ssh.GetReceivedText(channelNum,'utf-8');
      if (ssh.LastMethodSuccess = False) then
        begin
          WriteLn(ssh.LastErrorText);
          Exit;
        end;
      sbReceived.Append(pageText);
      WriteLn(pageText);

      //  If this page ended with "--More--", send a SPACE to request the next page.
      moreComing := sbReceived.Contains('--More--',caseSensitive);
      if (moreComing) then
        begin
          success := ssh.ChannelSendString(channelNum,' ','utf-8');
          if (success = False) then
            begin
              WriteLn(ssh.LastErrorText);
              Exit;
            end;
        end;
    end;

  ssh.Disconnect();


  ssh.Free;
  saMatch.Free;
  sbReceived.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.