Search code examples
pythonsshtimeoutparamiko

How to kill all command child processes and grandson in paramiko exec_command in case of timeout


client = paramiko.SSHClient()
client.connect(<host details...>)
try:
    stdin, stdout, stderr = client.exec_command(cmd, timeout=timeout)
    err = stderr.channel.recv_stderr(sys.maxsize)
    exit_code = stdout.channel.recv_exit_status()
 except socket.timeout:
    # Need to kill all descendants of the exec_command process

I tried to add the bash timeout before command but have some chained command with';'


Solution

  • Need to run exec_command with get_pty=True, then when do client.close() it kills all childs also