Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Send a Signal to a Remote SSH Process
See more SSH Examples
Demonstrates the Chilkat Ssh.SendReqSignal method, which requests delivery of a signal to the remote process running on a channel. The first argument is the channel number and the second is the signal name. Send it after the process has started.
Background: This is the SSH equivalent of pressing Ctrl+C or running
kill — it lets a client interrupt or terminate a long-running remote command without tearing down the connection. Signal names are given without the SIG prefix (INT, TERM, KILL, and so on). Note that some servers, including common OpenSSH builds, do not implement signal delivery.Chilkat Pascal (Lazarus/Delphi) Downloads
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;
sshPort: Integer;
password: string;
channelNum: Integer;
begin
success := False;
// Demonstrates the Ssh.SendReqSignal method, which requests delivery of a signal to the remote
// process running on a channel. The 1st argument is the channel number and the 2nd is the
// signal name. Send it after the process has started.
ssh := TSsh.Create;
sshPort := 22;
success := ssh.Connect('ssh.example.com',sshPort);
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;
// Open a session channel. A negative return value indicates failure.
channelNum := ssh.OpenSessionChannel();
if (channelNum < 0) then
begin
WriteLn(ssh.LastErrorText);
Exit;
end;
// Start a long-running command.
success := ssh.SendReqExec(channelNum,'sleep 60');
if (success = False) then
begin
WriteLn(ssh.LastErrorText);
Exit;
end;
// Send an interrupt signal to the remote process. The name omits the "SIG" prefix.
success := ssh.SendReqSignal(channelNum,'INT');
if (success = False) then
begin
WriteLn(ssh.LastErrorText);
Exit;
end;
WriteLn('Signal sent.');
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.