Skip to content

channels don't work unless a reference to SSHClient is explicitly kept #44

@w31rd0

Description

@w31rd0

Using an instance of Channel received from somewhere without a reference to SSHClient being explicitly kept (to prevent it from being garbage collected) sometimes gives unexpected results. For instance, a call to exec_command() raises an exception about the channel being closed, while recv_exit_status() just returns -1.

Here's a synthetic test case.

import paramiko

def exec_remote_command(command, host, username=None, password=None):
    channel = _exec_remote_command(command, host, username=username, password=password)
    return channel.recv_exit_status()

def _exec_remote_command(command, host, username=None, password=None):
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy())
    client.connect(host, username=username, password=password)
    channel = client.get_transport().open_session()
    channel.exec_command(command)
    return channel

if __name__ == '__main__':
    print exec_remote_command('ls', '127.0.0.1') # prints -1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions