Comments (4)
What version of ssh2-python
is being used? It must be out of date as channel failures now raise exceptions rather than returning None
.
Cannot replicate with latest release, 0.15.0
:
from __future__ import print_function
import socket
import os
import pwd
from ssh2.session import Session
USERNAME = pwd.getpwuid(os.geteuid()).pw_name
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 22))
s = Session()
s.handshake(sock)
s.agent_auth(USERNAME)
chan = s.open_session()
chan.execute('echo me')
size, data = chan.read()
while size > 0:
print(data)
size, data = chan.read()
chan.close()
try:
d_chan = s.direct_tcpip('fakey', 22)
except Exception:
pass
else:
raise Exception("Should have failed")
chan = s.open_session()
chan.execute('echo me')
size, data = chan.read()
while size > 0:
print(data)
size, data = chan.read()
chan.close()
s.disconnect()
sock.close()
Output:
me
me
from ssh2-python.
Thanks, I'm using 0.15 now. I have more information.
In your example, the underlying DNS lookup of "fakey" will fail, and so there will be no attempted socket connection to a server. Your example does not work when the server for the direct_tcpip connection is unreachable (e.g. it happens to be powered off, or perhaps behind a firewall). In that case, the code waits indefinitely when trying to open the direct channel.
I tried adding a timeout to the session (s.set_timeout(10000)). After the timeout occurs during the opening of the direct_tcpip channel, the session is unusable (the second s.open_session() fails).
I don't have a full understanding of what is happening under the hood. When opening a direct_tcpip channel, I presume that within the underlying libssh2 library a new socket connection between 'localhost' and 'fakey' is created.... how is the timeout for that new connection set? It seems like there isn't a timeout.
from ssh2-python.
The SSH protocol implementation, and this is in libssh2 code, is to open a new channel on the same socket for the direct tcp/ip connection. This is no different than any other channel, like open_session()
, so should work the same way.
As far as I can see, using socket timeout also requires session to have same timeout and therefore be in non-blocking mode. In the below test case after a timeout occurs, the session will raise Timeout
continuously and has to be restarted, ie re-authenticate:
from __future__ import print_function
import socket
import os
import pwd
from ssh2.session import Session
from ssh2.error_codes import LIBSSH2_ERROR_EAGAIN
from ssh2.utils import wait_socket
USERNAME = pwd.getpwuid(os.geteuid()).pw_name
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 22))
s = Session()
s.handshake(sock)
s.agent_auth(USERNAME)
sock.settimeout(1000)
s.set_timeout(1000)
chan = s.open_session()
while chan == LIBSSH2_ERROR_EAGAIN:
wait_socket(sock, s)
chan = s.open_session()
while chan.execute('echo me') == LIBSSH2_ERROR_EAGAIN:
wait_socket(sock, s)
while chan.wait_eof() == LIBSSH2_ERROR_EAGAIN:
wait_socket(sock, s)
size, data = chan.read()
while size == LIBSSH2_ERROR_EAGAIN:
wait_socket(sock, s)
size, data = chan.read()
while size > 0:
print(data)
size, data = chan.read()
tries = 0
try:
d_chan = s.direct_tcpip('google.com', 5555)
while d_chan == LIBSSH2_ERROR_EAGAIN:
wait_socket(sock, s, timeout=.1)
d_chan = s.direct_tcpip('google.com', 5555)
tries += 1
if tries > 2:
raise Exception
except Exception:
pass
else:
raise Exception("Should have failed")
# Raises timeout immediately
chan = s.open_session()
chan.close()
s.disconnect()
sock.close()
This looks like an issue in libssh2 and there is one similar issue about channel blocking indefinitely after a timeout that looks related. Would be worth making a test case in C when there is some time.
from ssh2-python.
Not seeing anything that needs changing in ssh2-python.
from ssh2-python.
Related Issues (20)
- Unknown error while starting the session HOT 1
- Fails to build on Python 3.11 - longintrepr.h: No such file or directory HOT 1
- ssh2-python is unable to establish connection with RHEL 9 SSHD HOT 2
- Remove Python 2 support badge HOT 1
- ed25519 key auth fails with FileError HOT 7
- `SocketRecvError` on resuming upload of partially uploaded files HOT 2
- Update libssh2 version to 1.11.0 HOT 8
- Help installing ssh2-python on alpine docker container HOT 1
- ssh2-python failed to install on windows 11 python 3.11, python 3.10 install succeded HOT 1
- Unable to install on python 3.10 macOS M1 HOT 2
- [Feature request] Musllinux wheel
- Python 3.12 support HOT 1
- Feature request: enable compression flag HOT 3
- Drop EOL Python versions (3.6, 3.7)
- Drop manylinux2010 wheels
- Migrate to OpenSSL 3
- Migrate to Cython 3
- Automate updating of vendored libraries
- scp does not write to file
- Core dump when calling session.methods()
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ssh2-python.