Sample code for 30+ languages & platforms
CkPython

Check if the SSH Connection is Alive

See more SSH Examples

Demonstrates the Chilkat Ssh.CheckConnection method, which reports whether the underlying TCP socket still appears to be open. It takes no arguments and returns true if the socket does not appear closed, or false if the connection is known to be closed or invalid. This example connects, authenticates, checks the socket, and then uses SendIgnore for a definitive liveness test.

Background: Unlike the IsConnected property, which simply reports the state the Ssh object already knows, CheckConnection performs an additional non-consuming check of the socket — it calls the operating system's recv with MSG_PEEK to inspect up to one byte without removing any data from the receive buffer. It is still a local check: it sends no SSH message, does not authenticate, and does not prove the remote server is responsive. A silently broken network path can keep it returning true until the OS notices, and a receive timeout, aborted operation, auth failure, or rejected channel-open does not necessarily close the socket. When you need an actual SSH round trip, use SendIgnore. Also note a false result may simply mean no connection exists, and this method may not update LastErrorText, so an existing error string could describe an earlier operation.

Chilkat CkPython Downloads

CkPython
import sys
import chilkat

success = False

#  Demonstrates the Ssh.CheckConnection method, which reports whether the underlying TCP socket
#  still appears to be open.  It takes no arguments and returns True if the socket does not
#  appear closed, or False if the connection is known to be closed or invalid.
#  
#  Unlike the IsConnected property, which reports the state already known to the Ssh object,
#  CheckConnection performs an additional non-consuming peek at the socket.  It is still a local
#  check: it does not send an SSH message or prove the remote server is responsive.  Use
#  SendIgnore when an active SSH round trip is required.

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()

#  Check whether the socket still appears open.
if (ssh.CheckConnection()):
    print("The socket still appears open.")
else:
    print("The connection is closed or invalid.")

#  Note: a silently broken network path can still report open here until the OS detects it.
#  For a definitive test that the SSH connection is writable, send an IGNORE message.
success = ssh.SendIgnore()
if (success == False):
    print(ssh.lastErrorText())
    sys.exit()

print("SSH IGNORE sent -- the connection is writable.")

ssh.Disconnect()