Zig
Zig
Setting Environment Variables for SCP Transfers
See more SCP Examples
Demonstrates how to set remote environment variables for an SCP transfer.Note 1: This example requires Chilkat v9.5.0.79 or greater.
Note 2: Setting environment variables for SCP is only supported by some SSH servers.
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const ssh = try chilkat.Ssh.init();
defer ssh.deinit();
// First connect to an SSH server.
ssh.connect("example.com", 22) catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
// Wait a max of 5 seconds when reading responses..
ssh.setIdleTimeoutMs(5000);
// Authenticate..
ssh.authenticatePw("myLogin", "myPassword") catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
// After the SSH object is connected and authenticated, we use it
// as the underlying transport in our SCP object.
const scp = try chilkat.Scp.init();
defer scp.deinit();
scp.useSsh(ssh) catch {
std.debug.print("{s}\n", .{try scp.getLastErrorText(alloc)});
return;
};
// 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".
const json_env_vars = try chilkat.JsonObject.init();
defer json_env_vars.deinit();
json_env_vars.setEmitCompact(false);
json_env_vars.updateString("LCS_PASSWORD", "secret") catch {};
json_env_vars.updateString("MY_TEST_NAME", "abc") catch {};
const str_env_vars = try json_env_vars.emit(alloc);
std.debug.print("{s}\n", .{str_env_vars});
// Setting the SendEnv property causes Chilkat to set each environment variable on the SSH server
// prior to doing the upload or download.
scp.setSendEnv(str_env_vars);
// Do the upload..
const remote_path = "starfish.jpg";
const local_path = "qa_data/jpg/starfish.jpg";
scp.uploadFile(local_path, remote_path) catch {
std.debug.print("{s}\n", .{try scp.getLastErrorText(alloc)});
return;
};
std.debug.print("SCP upload file success.\n", .{});
// Disconnect
ssh.disconnect();
}