CkPython
CkPython
Read an SSH Channel Until Output Goes Quiet
See more SSH Examples
Demonstrates the Chilkat Ssh.ChannelReadAndPoll method, which reads a channel in two phases: it waits for the first channel message, then keeps processing until no further data arrives for the given number of milliseconds. The first argument is the channel number and the second is the second-phase poll timeout.
The integer return value is -1 on error, -2 if nothing arrived before the timeout, 0 if EOF or channel close was processed, or — when greater than 0 — the total bytes currently buffered for the channel (not the bytes newly received by this call).
Background: This solves the practical problem of "read everything the command produced" when the channel is not going to close — an interactive shell, for example, stays open after each command. Waiting for a quiet period is a pragmatic stand-in for an end-of-output marker. The initial wait is governed by
ReadTimeoutMs (or IdleTimeoutMs when that is zero), while the argument controls only the quiet-period phase.Chilkat CkPython Downloads
import sys
import chilkat
success = False
# Demonstrates the Ssh.ChannelReadAndPoll method, which reads a channel in two phases: it waits
# for the first channel message, then keeps processing until no more data arrives for the given
# number of milliseconds. The 1st argument is the channel number and the 2nd is the poll
# timeout for the second phase.
ssh = chilkat.CkSsh()
sshPort = 22
success = ssh.Connect("ssh.example.com",sshPort)
if (success == False):
print(ssh.lastErrorText())
sys.exit()
# 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.
password = "mySshPassword"
success = ssh.AuthenticatePw("mySshLogin",password)
if (success == False):
print(ssh.lastErrorText())
sys.exit()
channelNum = ssh.OpenSessionChannel()
if (channelNum < 0):
print(ssh.lastErrorText())
sys.exit()
# 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.
success = ssh.SendReqShell(channelNum)
if (success == False):
print(ssh.lastErrorText())
sys.exit()
success = ssh.ChannelSendString(channelNum,"ls -l /usr/bin\n","utf-8")
if (success == False):
print(ssh.lastErrorText())
sys.exit()
# Keep reading until the output goes quiet for 500 milliseconds.
pollTimeoutMs = 500
# The return value is: -1 on error, -2 if nothing arrived before the timeout, 0 if EOF or
# channel close was processed, or the total number of buffered bytes when greater than 0.
result = ssh.ChannelReadAndPoll(channelNum,pollTimeoutMs)
if (result == -1):
print(ssh.lastErrorText())
sys.exit()
if (result == -2):
print("No channel data arrived before the timeout.")
if (result == 0):
print("EOF or channel close was processed. Buffered data may still remain.")
if (result > 0):
print("Total bytes buffered: " + str(result))
output = ssh.getReceivedText(channelNum,"utf-8")
if (ssh.get_LastMethodSuccess() == False):
print(ssh.lastErrorText())
sys.exit()
print(output)
ssh.Disconnect()