Repository navigation
Open
Description
Problem
- Random raise Exception("paramiko.ssh_exception.SSHException: Unable to open channel.") when execute_command
Env
Client: SSH-2.0-paramiko_2.7.1
Server: SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
Python traceback
Traceback (most recent call last):
File "ssh_test.py", line 25, in <module>
ret = ssh.exec_command(cmd)
File "/testenv/local/lib/python2.7/site-packages/paramiko/client.py", line 508, in exec_command
chan = self._transport.open_session(timeout=timeout)
File "/testenv/local/lib/python2.7/site-packages/paramiko/transport.py", line 879, in open_session
timeout=timeout,
File "/testenv/local/lib/python2.7/site-packages/paramiko/transport.py", line 1006, in open_channel
raise e
paramiko.ssh_exception.SSHException: Unable to open channel.
Client site log
2020-06-09 14:11:09,682 - paramiko.transport - DEBUG - [chan 9562] Max packet in: 32768 bytes
2020-06-09 14:11:09,722 - paramiko.transport - DEBUG - [chan 9562] remote_chanid: 1
2020-06-09 14:11:09,722 - paramiko.transport - DEBUG - [chan 9562] Max packet out: 32768 bytes
2020-06-09 14:11:09,722 - paramiko.transport - DEBUG - Secsh channel 9562 opened.
2020-06-09 14:11:09,747 - paramiko.transport - INFO - [chan 9561] 9561: __del__ called
2020-06-09 14:11:09,748 - paramiko.transport - DEBUG - [chan 9562] Sesch channel 9562 request ok
2020-06-09 14:11:09,751 - paramiko.transport - DEBUG - [chan 9562] EOF received (9562)
2020-06-09 14:11:09,752 - root - DEBUG - ('haha\n', 'debug1: SELinux support disabled\r\ndebug1: PAM: reinitializing credentials\r\ndebug1: permanently_set_uid: 0/0\r\nEnvironment:\n USER=root\n LOGNAME=root\n HOME=/root\n PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games\n MAIL=/var/mail/root\n SHELL=/bin/bash\n TZ=Asia/Taipei\n SSH_CLIENT=127.0.0.1 42875 1234\n SSH_CONNECTION=127.0.0.1 42875 127.0.0.1 1234\n')
9563
2020-06-09 14:11:09,752 - paramiko.transport - INFO - [chan 9562] shutdown called, how: 1
2020-06-09 14:11:09,753 - paramiko.transport - DEBUG - [chan 9562] Init EOF sending (9562), remote_chanid: 1
2020-06-09 14:11:09,753 - paramiko.transport - DEBUG - [chan 9562] EOF sent (9562), remote_chanid: 1
2020-06-09 14:11:09,753 - paramiko.transport - DEBUG - [chan 9562] Skip EOF sending (9562), remote_chanid: 1
echo haha
2020-06-09 14:11:09,754 - paramiko.transport - DEBUG - [chan 9563] Max packet in: 32768 bytes
2020-06-09 14:11:09,794 - paramiko.transport - DEBUG - [chan 9563] remote_chanid: 0
2020-06-09 14:11:09,794 - paramiko.transport - DEBUG - [chan 9563] Max packet out: 32768 bytes
2020-06-09 14:11:09,794 - paramiko.transport - DEBUG - Secsh channel 9563 opened.
2020-06-09 14:11:09,820 - paramiko.transport - INFO - [chan 9562] 9562: __del__ called
2020-06-09 14:11:09,821 - paramiko.transport - DEBUG - [chan 9563] Sesch channel 9563 request ok
2020-06-09 14:11:09,824 - paramiko.transport - DEBUG - [chan 9563] EOF received (9563)
2020-06-09 14:11:09,824 - root - DEBUG - ('haha\n', 'debug1: SELinux support disabled\r\ndebug1: PAM: reinitializing credentials\r\ndebug1: permanently_set_uid: 0/0\r\nEnvironment:\n USER=root\n LOGNAME=root\n HOME=/root\n PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games\n MAIL=/var/mail/root\n SHELL=/bin/bash\n TZ=Asia/Taipei\n SSH_CLIENT=127.0.0.1 42875 1234\n SSH_CONNECTION=127.0.0.1 42875 127.0.0.1 1234\n')
9564
2020-06-09 14:11:09,825 - paramiko.transport - INFO - [chan 9563] shutdown called, how: 1
2020-06-09 14:11:09,825 - paramiko.transport - DEBUG - [chan 9563] Init EOF sending (9563), remote_chanid: 0
2020-06-09 14:11:09,825 - paramiko.transport - DEBUG - [chan 9563] EOF sent (9563), remote_chanid: 0
2020-06-09 14:11:09,826 - paramiko.transport - DEBUG - [chan 9563] Skip EOF sending (9563), remote_chanid: 0
echo haha
2020-06-09 14:11:09,827 - paramiko.transport - DEBUG - [chan 9564] Max packet in: 32768 bytes
2020-06-09 14:11:09,866 - paramiko.transport - DEBUG - [chan 9564] remote_chanid: 1
2020-06-09 14:11:09,866 - paramiko.transport - DEBUG - [chan 9564] Max packet out: 32768 bytes
2020-06-09 14:11:09,866 - paramiko.transport - DEBUG - Secsh channel 9564 opened.
2020-06-09 14:11:09,892 - paramiko.transport - INFO - [chan 9563] 9563: __del__ called
2020-06-09 14:11:09,893 - paramiko.transport - DEBUG - [chan 9564] Sesch channel 9564 request ok
2020-06-09 14:11:09,896 - paramiko.transport - DEBUG - [chan 9564] EOF received (9564)
2020-06-09 14:11:09,896 - root - DEBUG - ('haha\n', 'debug1: SELinux support disabled\r\ndebug1: PAM: reinitializing credentials\r\ndebug1: permanently_set_uid: 0/0\r\nEnvironment:\n USER=root\n LOGNAME=root\n HOME=/root\n PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games\n MAIL=/var/mail/root\n SHELL=/bin/bash\n TZ=Asia/Taipei\n SSH_CLIENT=127.0.0.1 42875 1234\n SSH_CONNECTION=127.0.0.1 42875 127.0.0.1 1234\n')
9565
2020-06-09 14:11:09,897 - paramiko.transport - INFO - [chan 9564] shutdown called, how: 1
2020-06-09 14:11:09,897 - paramiko.transport - DEBUG - [chan 9564] Init EOF sending (9564), remote_chanid: 1
2020-06-09 14:11:09,897 - paramiko.transport - DEBUG - [chan 9564] EOF sent (9564), remote_chanid: 1
2020-06-09 14:11:09,898 - paramiko.transport - DEBUG - [chan 9564] Skip EOF sending (9564), remote_chanid: 1
echo haha
2020-06-09 14:11:09,899 - paramiko.transport - DEBUG - [chan 9565] Max packet in: 32768 bytes
2020-06-09 14:11:09,939 - paramiko.transport - INFO - Disconnect (code 2): ieof packet referred to nonexistent channel 1
2020-06-09 14:11:09,939 - paramiko.transport - INFO - [chan 9564] 9564: __del__ called
2020-06-09 14:11:09,981 - paramiko.transport - INFO - [chan 9565] 9565: __del__ called
Server site error
debug1: channel 1: new [server-session]
debug1: session_new: session 1
debug1: session_open: channel 1
debug1: session_open: session 1: link with channel 1
debug1: server_input_channel_open: confirm session
debug1: session_by_channel: session 0 channel 0
debug1: session_close_by_channel: channel 0 child 0
Close session: user root from 127.0.0.1 port 53213 id 0
debug1: channel 0: free: server-session, nchannels 2
debug1: server_input_channel_req: channel 1 request exec reply 1
debug1: session_by_channel: session 1 channel 1
debug1: session_input_channel_req: session 1 req exec
Starting session: command for root from 127.0.0.1 port 53213 id 1
debug1: Received SIGCHLD.
debug1: session_by_pid: pid 13605
debug1: session_exit_message: session 1 channel 1 pid 13605
debug1: session_exit_message: release channel 1
debug1: session_by_channel: session 1 channel 1
debug1: session_close_by_channel: channel 1 child 0
Close session: user root from 127.0.0.1 port 53213 id 1
debug1: channel 1: free: server-session, nchannels 1
channel_by_id: 1: bad id: channel free
Disconnecting user root 127.0.0.1 port 53213: ieof packet referred to nonexistent channel 1
Script for reproduce
Usege
python ssh_test.py [IP] [port] [usr] [pwd]
import sys
import paramiko
print paramiko.__version__
print paramiko
if len(sys.argv) != 5:
print('{__name__} ip port usr passwd'.format(**locals()))
print(sys.argv)
sys.exit(1)
print sys.argv
ip = sys.argv[1]
port = sys.argv[2]
usr = sys.argv[3]
passwd = sys.argv[4]
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=ip, port=port,
username=usr, password=passwd)
while True:
cmd = 'echo haha'
print cmd
ret = ssh.exec_command(cmd)
_in, _out, _err = ret
o = _out.read()
e = _err.read()
# follow line make bug
del o, e, _in, _out, _err, ret
# or replase by _in.close()
ssh.close()
Reproduce frequency
- abount 1~5 min(s)
Possible reason
Line 1392 in 72a068c
| self.channel.shutdown_write() |
- comment out this code can workaround this problem
Related bug
Metadata
Metadata
Assignees
Labels
No labels
Activity
skip Channel stdin shutdown if channel already closed
skip Channel stdin shutdown if channel already closed
channel: check if already closed in _send_eof()
channel: check if already closed in _send_eof()
ploxiln commentedon Jul 29, 2020
I can't seem to reproduce this using your script, perhaps the timing of my systems is not quite the same ...
Does this alternate fix work for you?
EDIT: I tested running the script on my laptop with a wifi connection, against two servers: one in the same room running
OpenSSH_7.9p1 Debian-10+deb10u2, and one on the opposite US coast runningOpenSSH_8.2p1 Ubuntu-4ubuntu0.1. I also ran it under both python-2.7 and python-3.7, and modified the script in what really should be trivial ways, just toprint()fully compatibly with python-3 and slightly more readable: https://gist.github.com/ploxiln/86dbd713dc959d87130e2066a0124a5eploxiln commentedon Aug 7, 2020
another option that may be convenient to test this fix - try using the latest release of my fork, paramiko-ng, something like this:
(for more details: https://github.com/ploxiln/paramiko-ng/#installation https://github.com/ploxiln/paramiko-ng/releases)
markhuang3310 commentedon Aug 17, 2020
paramiko version=2.8.4
python version=2.7.17
The commit still not work
You can try to reproduce with the following command:
ploxiln commentedon Aug 18, 2020
OK, I can reproduce with localhost sshd, thanks.
rusek commentedon Sep 14, 2020
This looks like a duplicate of #1115.
channel: lock around transport send of eof and channel close
channel: lock around transport send of eof and channel close
ploxiln commentedon Nov 27, 2021
For what it's worth ... I've finally released paramiko-ng-2.8.6 with what I believe is a proper fix for this issue. Thanks for your script and instructions for reproducing this issue reliably (I also had to use python-2.7, rather than porting the script to python3, to reproduce it reliably). I also found that the #1115 was a great explanation of the cause and #1137 was a working model for how to fix it (though I took the liberty of modifying/minimizing it).
harixxxx commentedon May 12, 2022
Is this bug specific to paramiko 2.7.1 or does it exist in previous versions also (e.g: 2.4.1)?