0

Problem

Hello my problem is that I want to use the ssh2-python package to remotely read a a bunch of files, but I can't seem to send commands to the remote host machine.

Originally I started with the paramiko package and I did get that to work, but I am dealing with a lot of large memory files (which is why I can't bring them to the local machine) and it is a bit too slow. I am currently running Python 3.6.3 & ssh2-python 0.18.0.post1 and have tried changing versions of ssh2-python, but it didn't help.

Code

import socket
from ssh2.session import Session

host_ip=socket.gethostbyname('hostname')
sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host_ip,22))
session=Session()
session.handshake(sock)
print(session.userauth_list('username'))
session.userauth_password('username','password')
channel=session.open_session()
channel.execute('echo Hello')

Code Prints the Following

0

['publickey', 'gssapi-keyex', 'gssapi-with-mic', 'password']

0

0

Expectation/Thoughts

I expected the code to print Hello, but instead it just printed 0. It also printed 0 after the handshake and after the call to the authentication method and I have no idea why. It seems like I am in contact with the remote machine as it did print out which authentications it would take, but it doesn't appear to me that I am actually logged in and can do anything. I would really like to use this package as from what I read online it is significantly faster paramiko, (alternatives would be good to) but I can't seem to figure out what is going on here.

Please help and thanks in advance!

DMD
  • 1
  • 1

1 Answers1

0

You may in fact be connected and executing commands, but channel.execute('ls') returns '0' (it's exit/status code).

If you want to read your response from the server:

channel.execute('echo Hello')
size, data = channel.read()
while size:
    size, dt = channel.read()
    data += dt

print(data.decode())

The API documentation for ssh2-python is rather sparse, but the examples should get you through some of the basics: https://github.com/ParallelSSH/ssh2-python/tree/master/examples

A complete version of the above is in example_echo.py

Brendano257
  • 553
  • 5
  • 10