Sample code for 30+ languages & platforms
Lua

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

Lua
local chilkat = require("chilkat")

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

local ssh = chilkat.Ssh.new()

local sshPort = 22
local success = ssh:connect("ssh.example.com", sshPort)
if not success then
    print(ssh.last_error_text)
    return
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.
local password = "mySshPassword"

success = ssh:authenticate_pw("mySshLogin", password)
if not success then
    print(ssh.last_error_text)
    return
end

-- Open a session channel.  A negative return value indicates failure.
local channelNum = ssh:open_session_channel()
if channelNum < 0 then
    print(ssh.last_error_text)
    return
end

-- Request the "sftp" subsystem.  A true return means the server accepted the request;
-- subsequent protocol data is exchanged with the normal channel send and receive methods.
success = ssh:send_req_subsystem(channelNum, "sftp")
if not success then
    print(ssh.last_error_text)
    return
end

print("Subsystem request accepted.")

ssh:disconnect()