Sample code for 30+ languages & platforms
Tcl

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

Tcl

load ./chilkat.dll

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

set ssh [new_CkSsh]

set sshPort 22
set success [CkSsh_Connect $ssh "ssh.example.com" $sshPort]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    exit
}

#  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.
set password "mySshPassword"

set success [CkSsh_AuthenticatePw $ssh "mySshLogin" $password]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    exit
}

#  Open a session channel.  A negative return value indicates failure.
set channelNum [CkSsh_OpenSessionChannel $ssh]
if {$channelNum < 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    exit
}

#  Start a long-running command.
set success [CkSsh_SendReqExec $ssh $channelNum "sleep 60"]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    exit
}

#  Send an interrupt signal to the remote process.  The name omits the "SIG" prefix.
set success [CkSsh_SendReqSignal $ssh $channelNum "INT"]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    exit
}

puts "Signal sent."

CkSsh_Disconnect $ssh

delete_CkSsh $ssh