Sample code for 30+ languages & platforms
Lua Requires Chilkat v9.5.0.79+

Setting Environment Variables for SCP Transfers

See more SCP Examples

Demonstrates how to set remote environment variables for an SCP transfer.

Note: Setting environment variables for SCP is only supported by some SSH servers.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local ssh = chilkat.Ssh.new()

-- First connect to an SSH server.
local success = ssh:connect("example.com", 22)
if not success then
    print(ssh.last_error_text)
    return
end

-- Wait a max of 5 seconds when reading responses..
ssh.idle_timeout_ms = 5000

-- Authenticate..
success = ssh:authenticate_pw("myLogin", "myPassword")
if not success then
    print(ssh.last_error_text)
    return
end

-- After the SSH object is connected and authenticated, we use it
-- as the underlying transport in our SCP object.
local scp = chilkat.Scp.new()

success = scp:use_ssh(ssh)
if not success then
    print(scp.last_error_text)
    return
end

-- Specify the environment variables to be set in JSON as follows.
-- This example sets two environment variables.  One is named "LCS_PASSWORD" and the other "MY_TEST_NAME".
local jsonEnvVars = chilkat.JsonObject.new()
jsonEnvVars.emit_compact = false
jsonEnvVars:update_string("LCS_PASSWORD", "secret")
jsonEnvVars:update_string("MY_TEST_NAME", "abc")
local strEnvVars = jsonEnvVars:emit()

print(strEnvVars)

-- Setting the SendEnv property causes Chilkat to set each environment variable on the SSH server
-- prior to doing the upload or download.
scp.send_env = strEnvVars

-- Do the upload..
local remotePath = "starfish.jpg"
local localPath = "qa_data/jpg/starfish.jpg"
success = scp:upload_file(localPath, remotePath)
if not success then
    print(scp.last_error_text)
    return
end

print("SCP upload file success.")

-- Disconnect
ssh:disconnect()