Code Monkey home page Code Monkey logo

Comments (5)

qzlisb avatar qzlisb commented on May 22, 2024

Because the first read is finished and the second read is always blocked, it's better to check if it's done again, such as checking the prompt.

from ssh2-python.

qzlisb avatar qzlisb commented on May 22, 2024

Or you can set session to non-block, but can't be sure to receive all data

from ssh2-python.

pkittenis avatar pkittenis commented on May 22, 2024

Hi there,

Thanks for the interest. This is not an issue with the library.

channel.read on an interactive shell (channel.shell) will block indefinitely if there is no output to be read - this is SSH protocol behaviour. The shell is waiting for input. As it is interactive and does not send EOF, there is no indicator no more output will be sent.

The channel should be closed before trying to read again if no more commands will be sent to it. Alternatively use channel.execute and channel.wait_eof to determine that no more output will be sent.

If all the output needed has already been received, why is the client code trying to read more data without doing anything on the channel? There will obviously be no output from it unless something is executed first.

There is functionality in the libssh2 API to check if output is available - see session.block_directions.

See example for use of close, eof and wait_eof.

from ssh2-python.

SomeProgrammerGuy avatar SomeProgrammerGuy commented on May 22, 2024

Cheers. That clears things up. I did it this way following an online example that is obviously wrong.

I'm also trying to build in some simple error checking for an automated unsupervised script.

i.e.

  1. Get returned data
  2. Is any data returned and is it complete<< as I had it this will never be reached.
  3. Check returned data is correct before sending commands. (if not exit etc)

Do again for next lot of data etc

Otherwise no returned data (as some issue occurred or its wrong) the script is stuck in an infinite loop (unless there's a timeout)

An infinite loop is never a good thing without some sort of exit plan.

The example you have posted is also doing this:

# Print output
size, data = channel.read()
while size > 0:
    print(data)
    size, data = channel.read()

from ssh2-python.

pkittenis avatar pkittenis commented on May 22, 2024

The example is using wait_eof and close before reading as described above. read returns 0 after these when all output has been consumed.

The example as present in the repo is correct for what it does, as above.

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.