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

SSH Through a SOCKS Proxy

See more SSH Examples

Demonstrates connecting to an SSH server through a SOCKS4 or SOCKS5 proxy. Set the SOCKS properties before calling Connect, and set SocksVersion to match the proxy server.

Background: SOCKS is a general-purpose TCP relay, so unlike an HTTP proxy it is designed from the start to carry arbitrary protocols — usually the smoother option for SSH. The version matters: SOCKS4 supports only a username with no password, while SOCKS5 supports full login/password authentication as well as IPv6 and remote DNS resolution. Note this is the inverse of dynamic port forwarding: here a SOCKS proxy is used to reach the SSH server, rather than SSH providing a SOCKS proxy.

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.Ssh;

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

procedure RunDemo;
var
  success: Boolean;
  ssh: TSsh;
  hostname: string;
  port: Integer;
  password: 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 an SSH server through a SOCKS4 or SOCKS5 proxy.

  ssh := TSsh.Create;

  //  Set the SOCKS proxy properties before connecting.  The hostname may be a domain name or an
  //  IP address.
  ssh.SocksHostname := 'socks.example.com';
  ssh.SocksPort := 1080;
  ssh.SocksUsername := 'mySocksLogin';
  ssh.SocksPassword := 'mySocksPassword';

  //  Set the version to match the proxy server: 4 or 5.
  //  Note: SOCKS4 supports only a username, with no password.  SOCKS5 supports full
  //  login/password authentication.
  ssh.SocksVersion := 5;

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

  WriteLn('Connected to the SSH server through the SOCKS proxy.');

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

  //  ... use the SSH connection normally ...

  ssh.Disconnect();


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