Giter Site home page Giter Site logo

Comments (4)

pkittenis avatar pkittenis commented on July 4, 2024

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.

gpb536 avatar gpb536 commented on July 4, 2024

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.

pkittenis avatar pkittenis commented on July 4, 2024

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.

pkittenis avatar pkittenis commented on July 4, 2024

Not seeing anything that needs changing in ssh2-python.

from ssh2-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.