Tcl
Tcl
Request an SSH Subsystem
See more SSH Examples
Demonstrates the Chilkat Ssh.SendReqSubsystem method, which requests a predefined subsystem on a session channel. The first argument is the channel number and the second is the subsystem name. This example requests the sftp subsystem.
Background: A subsystem is a named service the server exposes over a channel instead of a shell command —
sftp is the familiar example, and netconf is common on network devices. After acceptance, the channel carries that protocol's data, exchanged with the ordinary channel send and receive methods. If the server rejects the subsystem, the channel normally stays open and can be reused for another request.Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# Demonstrates the Ssh.SendReqSubsystem method, which requests a predefined subsystem on a
# session channel. The 1st argument is the channel number and the 2nd is the subsystem name.
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
}
# Request the "sftp" subsystem. A 1 return means the server accepted the request;
# subsequent protocol data is exchanged with the normal channel send and receive methods.
set success [CkSsh_SendReqSubsystem $ssh $channelNum "sftp"]
if {$success == 0} then {
puts [CkSsh_lastErrorText $ssh]
delete_CkSsh $ssh
exit
}
puts "Subsystem request accepted."
CkSsh_Disconnect $ssh
delete_CkSsh $ssh