Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Ssh
integer li_SshPort
string ls_Password
integer li_ChannelNum

li_Success = 0

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

loo_Ssh = create oleobject
li_rc = loo_Ssh.ConnectToNewObject("Chilkat.Ssh")
if li_rc < 0 then
    destroy loo_Ssh
    MessageBox("Error","Connecting to COM object failed")
    return
end if

li_SshPort = 22
li_Success = loo_Ssh.Connect("ssh.example.com",li_SshPort)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  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.
ls_Password = "mySshPassword"

li_Success = loo_Ssh.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  Open a session channel.  A negative return value indicates failure.
li_ChannelNum = loo_Ssh.OpenSessionChannel()
if li_ChannelNum < 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  Start a long-running command.
li_Success = loo_Ssh.SendReqExec(li_ChannelNum,"sleep 60")
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  Send an interrupt signal to the remote process.  The name omits the "SIG" prefix.
li_Success = loo_Ssh.SendReqSignal(li_ChannelNum,"INT")
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

Write-Debug "Signal sent."

loo_Ssh.Disconnect()


destroy loo_Ssh