PowerBuilder
PowerBuilder
Send Text on an SSH Channel
See more SSH Examples
Demonstrates the Chilkat Ssh.ChannelSendString method, which encodes text using a charset and sends the resulting bytes on a channel. The arguments are the channel number, the text, and the charset. No line ending or terminator is added automatically.
Background: This is how you type into a remote shell programmatically. The most common mistake is forgetting the newline — the remote shell will not act on a command until it receives one, so the terminator must be part of the text you send. An empty string is valid and sends zero bytes. The charset governs how the text becomes bytes on the wire.
Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Ssh
integer li_SshPort
string ls_Password
integer li_ChannelNum
integer li_PollTimeoutMs
integer li_Result
string ls_Output
li_Success = 0
// Demonstrates the Ssh.ChannelSendString method, which encodes text with a charset and sends
// the bytes on a channel. The 1st argument is the channel number, the 2nd is the text, and the
// 3rd is the charset. No line ending is added automatically.
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
li_ChannelNum = loo_Ssh.OpenSessionChannel()
if li_ChannelNum < 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
// Start a shell WITHOUT requesting a PTY. Without a PTY the shell sees it is connected to a
// raw data pipe and enters non-interactive mode: it suppresses the command prompt, does not
// echo typed commands back, and skips interactive login scripts. The output is therefore
// clean and easily parseable -- but because no prompt is ever sent, code must not wait for
// one. Instead, have each command print a predictable marker, or wait for EOF / channel close.
li_Success = loo_Ssh.SendReqShell(li_ChannelNum)
if li_Success = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
// Send a command. The trailing newline must be included because no terminator is added.
li_Success = loo_Ssh.ChannelSendString(li_ChannelNum,"ls -l /tmp~n","utf-8")
if li_Success = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
// Read the shell's response.
li_PollTimeoutMs = 2000
li_Result = loo_Ssh.ChannelReadAndPoll(li_ChannelNum,li_PollTimeoutMs)
if li_Result = -1 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