[Python] paramiko's ssh channel closed after executing one cmd - Python

Discussão em 'Python' iniciado por Stack, Junho 27, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using Python's Paramiko to execute commands in a remove server. The code is much simple.

    Here's my definition of the SSHConn class:

    class SSHConn:

    def __init__(self, hostname, user, pwd, filename=None):
    self.client = paramiko.SSHClient()
    self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    self.client.connect(hostname, username=user, password=pwd,
    key_filename=filename)
    self.transport = self.client.get_transport()
    self.channel = self.transport.open_session()


    So I'm running the following code:

    local_ssh = ssh.SSHConn(host, user, passwd)

    cmds = ('foo', 'bar')
    for cmd in cmds:
    local_ssh.channel.exec_command(cmd)
    self.log.info(local_ssh.channel.recv(1024))


    However, when I execute the following code snipped I'm getting:

    INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_6.6.1p1)
    INFO:paramiko.transport:Authentication (publickey) failed.
    INFO:paramiko.transport:Authentication (password) successful!
    INFO:paramiko.transport:Secsh channel 1 opened.
    INFO:testsets.testcase:
    ERROR:testsets.testcase:Channel is not open
    Traceback (most recent call last):
    File "/root/fds-src/source/test/integration-framework/testsets/testcases/test_100gb_volume.py", line 87, in runTest
    local_ssh.channel.exec_command(cmd)
    File "/usr/lib/python2.7/dist-packages/paramiko/channel.py", line 209, in exec_command
    raise SSHException('Channel is not open')
    SSHException: Channel is not open
    ERROR:testsets.testcase:Test Case Test100GBVolume failed.
    Traceback (most recent call last):
    File "/root/fds-src/source/test/integration-framework/testsets/testcases/test_100gb_volume.py", line 87, in runTest
    local_ssh.channel.exec_command(cmd)
    File "/usr/lib/python2.7/dist-packages/paramiko/channel.py", line 209, in exec_command
    raise SSHException('Channel is not open')
    SSHException: Channel is not open


    How can I keep the channel open?

    Continue reading...

Compartilhe esta Página