Sample code for 30+ languages & platforms
PowerBuilder

Set an Environment Variable on an SSH Channel

See more SSH Examples

Demonstrates the Chilkat Ssh.SendReqSetEnv method, which asks the server to set an environment variable for a session channel. The arguments are the channel number, the variable name, and its value. Send these requests before the exec, shell, or subsystem request.

Background: This is the clean way to pass configuration to a remote command without embedding it in the command line. Be aware that many SSH servers restrict which variables clients may set (OpenSSH's AcceptEnv), so a request can be silently ignored or rejected — do not rely on it without verifying the server's policy.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Ssh
integer li_SshPort
string ls_Password
integer li_ChannelNum
string ls_Output

li_Success = 0

//  Demonstrates the Ssh.SendReqSetEnv method, which asks the server to set an environment
//  variable for a session channel.  The 1st argument is the channel number, the 2nd is the
//  variable name, and the 3rd is its value.  Send it before the exec, shell, or subsystem
//  request.

loo_Ssh = create oleobject
li_rc = loo_Ssh.ConnectToNewObject("Chilkat.Ssh")
if li_rc < 0 then
    destroy loo_Ssh
    MessageBox("Error","Connecting to COM object failed")
    return
end if

li_SshPort = 22
li_Success = loo_Ssh.Connect("ssh.example.com",li_SshPort)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  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.
ls_Password = "mySshPassword"

li_Success = loo_Ssh.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  Open a session channel.  A negative return value indicates failure.
li_ChannelNum = loo_Ssh.OpenSessionChannel()
if li_ChannelNum < 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  Set environment variables for the channel.  Multiple variables may be requested.
li_Success = loo_Ssh.SendReqSetEnv(li_ChannelNum,"LANG","en_US.UTF-8")
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

li_Success = loo_Ssh.SendReqSetEnv(li_ChannelNum,"MY_APP_MODE","production")
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  Now run the command, which sees the requested environment.
li_Success = loo_Ssh.SendReqExec(li_ChannelNum,"echo $MY_APP_MODE")
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  IMPORTANT: Set a read timeout.  ReadTimeoutMs defaults to 0, which means no limit -- without
//  it, this call waits forever if the server never sends channel close.
loo_Ssh.ReadTimeoutMs = 15000

li_Success = loo_Ssh.ChannelReceiveToClose(li_ChannelNum)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

ls_Output = loo_Ssh.GetReceivedText(li_ChannelNum,"utf-8")
if loo_Ssh.LastMethodSuccess = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

Write-Debug ls_Output

loo_Ssh.Disconnect()


destroy loo_Ssh