user2968409
user2968409

Reputation: 109

paramiko.SSHException: Channel closed

I try to use python 2.7.1 and paramiko 1.12.0:

connection = paramiko.SSHClient()
connection.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())

connection.connect(hostIP, 22, 'd', 'd')

command = 'ls'
(stdin, stdout, stderr) = connection.exec_command(command)
response = stdout.readlines()
errormsg = stderr.read()

But I get this error message:

Traceback (most recent call last):<br>
  File "./ssh.py", line 32, in <module><br>
    (stdin, stdout, stderr) = connection.exec_command(command)<br>
  File "/app/python/2.7.1/LMWP3/lib/python2.7/site-packages/paramiko/client.py", line 379, in exec_command<br>
    chan.exec_command(command)<br>
  File "/app/python/2.7.1/LMWP3/lib/python2.7/site-packages/paramiko/channel.py", line 218, in exec_command<br>
    self._wait_for_event()<br>
  File "/app/python/2.7.1/LMWP3/lib/python2.7/site-packages/paramiko/channel.py", line 1129, in _wait_for_event<br>
    raise e<br>
paramiko.SSHException: Channel closed.

Upvotes: 5

Views: 22454

Answers (1)

user966588
user966588

Reputation:

import paramiko
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
conn = s.connect(hostIP, username ='root', password='rootpassword', port=22)

command = 'pwd'
(stdin, stdout, stderr) = s.exec_command(command)

print stdout.read()
s.close()

This should work fine with root user on linux. If it's not, you are probably passing wrong values for hostIP (for ex: quotes in the value), username, password.

Upvotes: 1

Related Questions