You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cannot return channel or file-like object from a function, will close connection!! #440
In [37]: ssh, stdout = testp()
In [38]: print("From function:")
From function:
In [39]: print(repr(stdout))
<paramiko.ChannelFile from <paramiko.Channel 1 (open) window=2097152 -> <paramiko.Transport at 0xf4042d50L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>
In [40]: print(stdout.read())
works
In [41]: print("\n")
Reacted by Nikhil, GHANSHYAM GAURAV and Blazej Michalik
## Output:
From function:
<paramiko.ChannelFile from <paramiko.Channel 0 (open) window=2097152 -> <paramiko.Transport at 0x374b330L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>
works
<paramiko.ChannelFile from <paramiko.Channel 0 (open) window=2097152 -> <paramiko.Transport at 0x374b270L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>
works
I using paramiko-2.1.1 and this bug still in place. This bug makes paramiko library absolutely unusable because if I return from any method Channel, it returned closed. If I return SSHClient, and then use invoke_shell() to get the channel it establish a new connection.
Thanks a lot! You are right. I have installed v2.1.6 and it really works correctly. But in RedHat repository last official version is 2.1.1, and using pip is prohibited in our environment. The Internet is also unavailable. Is there any ways how to make new version installed from tar.gz archive without pip?
P.S.: we have python2 available only
I already tried it, but there were a lot of errors. But anyway thanks a lot. I will try to check some dependencies, it this could be the reason for the failure of the installation last time.
1 works fine, 2 stops early and gives me an empty stdout. The only difference is a single variable not being returned from a local function.
From what I see, if either of ChannelFile are not returned, it gets unreferenced, which automatically calls BufferedFile.del() and apparently closes the entire channel, despite me neither telling it to nor the client session being over yet (the same happens if SSHClient is run through context manager).
Since this related report has been here for over a decade, I suppose it's intentional, but I feel tossing an unwanted variable around until you're done with the session is not intuitive or logical. Neither the exec_command's docstring nor docs mention that this is a behavior to keep in mind, and I've spent a day wondering if I'm having a stroke. From what I understand, closing input in SSH is equivalent to asking to close the session, but I don't see why the input is closed preemptively.
Activity
pkittenis commentedon Dec 17, 2014
Need to return the
SSHClientobject or it goes out of scope:Output
ghanshyamgaurav commentedon Sep 5, 2017
You can do this by this way too.
## Example:
import paramiko
def testp():
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
ssh.connect("tesstserver")
return ssh
ssh = testp()
stdout=ssh.exec_command("sleep 5; echo works")[1]
print("From function:")
print(repr(stdout))
print(stdout.read())
print("\n")
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
ssh.connect("tesstserver")
stdin, stdout, stderr = ssh.exec_command("sleep 5; echo works")
print(repr(stdout))
print(stdout.read())
print("\n")
## Output:
From function:
<paramiko.ChannelFile from <paramiko.Channel 0 (open) window=2097152 -> <paramiko.Transport at 0x374b330L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>
works
<paramiko.ChannelFile from <paramiko.Channel 0 (open) window=2097152 -> <paramiko.Transport at 0x374b270L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>
works
ploxiln commentedon Sep 5, 2017
This issue can be closed now, because this was fixed in a not-so-great way in #891 and then fixed somewhat more properly in #952
smolrise commentedon Jun 11, 2021
I using paramiko-2.1.1 and this bug still in place. This bug makes paramiko library absolutely unusable because if I return from any method Channel, it returned closed. If I return SSHClient, and then use invoke_shell() to get the channel it establish a new connection.
ploxiln commentedon Jun 11, 2021
The fixes are present in paramiko-2.1.3, released in June 2017. Your version is even older.
smolrise commentedon Jun 17, 2021
Thanks a lot! You are right. I have installed v2.1.6 and it really works correctly. But in RedHat repository last official version is 2.1.1, and using pip is prohibited in our environment. The Internet is also unavailable. Is there any ways how to make new version installed from tar.gz archive without pip?
P.S.: we have python2 available only
ploxiln commentedon Jun 17, 2021
you can untar the archive, then run
python setup.py install(you'll have to figure out any additional details on your own, sorry)
smolrise commentedon Jun 17, 2021
I already tried it, but there were a lot of errors. But anyway thanks a lot. I will try to check some dependencies, it this could be the reason for the failure of the installation last time.
gajendraks commentedon Jul 27, 2021
If we return all the three channels(stdin, stdout, stderr) from the function, no connection will get closed.
Amonimus commentedon May 6, 2026
Tested on both 3.0.0 and 4.0.0
Compare:
1.
2.
1 works fine, 2 stops early and gives me an empty stdout. The only difference is a single variable not being returned from a local function.
From what I see, if either of ChannelFile are not returned, it gets unreferenced, which automatically calls BufferedFile.del() and apparently closes the entire channel, despite me neither telling it to nor the client session being over yet (the same happens if SSHClient is run through context manager).
Since this related report has been here for over a decade, I suppose it's intentional, but I feel tossing an unwanted variable around until you're done with the session is not intuitive or logical. Neither the exec_command's docstring nor docs mention that this is a behavior to keep in mind, and I've spent a day wondering if I'm having a stroke. From what I understand, closing input in SSH is equivalent to asking to close the session, but I don't see why the input is closed preemptively.