Nihdez
Nihdez

Reputation: 33

Paramiko Issues - Channel Closed when Executing a Command

I've been trying to run commands on an Overture Box using paramiko to establish the SSH connection.

My script does connect properly to the box, no issues are seen, the problem comes when the script runs the exec_command(cmd) method:

Traceback (most recent call last):
  File "test.py", line 39, in <module>
    stdin, stdout, stderr = ssh.exec_command("version")
  File "/opt/csw/lib/python2.7/site-packages/paramiko/client.py", line 345, in exec_command
    chan.exec_command(command)
  File "/opt/csw/lib/python2.7/site-packages/paramiko/channel.py", line 60, in _check
    return func(self, *args, **kwds)
  File "/opt/csw/lib/python2.7/site-packages/paramiko/channel.py", line 229, in exec_command
    self._wait_for_event()
  File "/opt/csw/lib/python2.7/site-packages/paramiko/channel.py", line 1086, in _wait_for_event
    raise e
paramiko.ssh_exception.SSHException: Channel closed.

Same script using a Linux Box as the target host yields the proper results

The code is borrowed, I just found the snippet Googling:

import sys
import time
import select
import paramiko

host = raw_input("Hostname?: ")
i = 1
password = raw_input("Password?: ")


#
# Try to connect to the host.
# Retry a few times if it fails.
#
while True:
    print "Trying to connect to %s (%i/30)" % (host, i)

    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(host, port=22, username="username", password=password)
        print "Connected to %s" % host
        break
    except paramiko.AuthenticationException:
        print "Authentication failed when connecting to %s" % host
        sys.exit(1)
    except:
        print "Could not SSH to %s, waiting for it to start" % host
        i += 1
        time.sleep(2)

    # If we could not connect within time limit
    if i == 30:
        print "Could not connect to %s. Giving up" % host
        sys.exit(1)

# Send the command (non-blocking)
stdin, stdout, stderr = ssh.exec_command("version")

# Wait for the command to terminate
while not stdout.channel.exit_status_ready():
    # Only print data if there is data to read in the channel
    if stdout.channel.recv_ready():
        rl, wl, xl = select.select([stdout.channel], [], [], 0.0)
        if len(rl) > 0:
           # Print data from stdout
            print stdout.channel.recv(1024),

#
# Disconnect from the host
#
print "Command done, closing SSH connection"
ssh.close()

I found other questions where it's said that the issue might be related to the SFTP settings in the host, but I'm able to do:

sftp username@hostname

I've Googled for a while but I can't find anything helpful so far.

Thanks in advance for your help.

EDIT: Almost forgot, I know the issue is exactly at:

stdin, stdout, stderr = ssh.exec_command("version")

I've run the whole script interactively and it breaks when I run that line.

Upvotes: 2

Views: 14920

Answers (2)

Ali Aqrabawi
Ali Aqrabawi

Reputation: 143

here is a workaround for this,

  1. get a transport for your connection.
  2. open a session.
  3. then exec_command(command) and thenrecv()`, no need for sleep.

    client.connect("host", port=22, username="user",password= "pass",look_for_keys=False, allow_agent=False)
    
    transport=client.get_transport()
    channel = t.open_session()
    
    channel.exec_command(command)
    out = channel.recv(-1)
    print(out.decode())
    

Upvotes: 0

PKapp
PKapp

Reputation: 196

Could be that the Overture does not support the SSH exec_command request.

I have needed to use transport.open_session(), and session.get_pty() and session.invoke_shell() to setup an interactive shell session, then use session.send() and session.recv() to write/read to the shell session for Cisco switches and other network appliances, when exec_command() is not available.

Upvotes: 7

Related Questions