Skip to content

Paramiko returns None intermittently #4

@thmosqueiro

Description

@thmosqueiro

For some reason, after paramiko sends a command to the remote server, sometimes it responds with a None instead of a string containing the server's response. I could not find a way to reproduce this behavior deterministically. However, if you set up a small script that uses paramiko and then run it several times in a row, it eventually happens.

Paramiko is only used in the sshKernel module. We could:

  • Fix it, if we can guarantee this doesn't happen anymore
  • Wrap a try/error around it
  • Change how we connect via SSH

Here is a minimal example of a Python script to test this error. This should print the output of uname -a of the remote server. Just try and run it several times in a row.

import paramiko

username = 'USERNAME'
address = 'ADDRESS'

def query(command):
        # Check if connection is made previously
        if (sshClient):
            stdin, stdout, stderr = sshClient.exec_command(command)
            while not stdout.channel.exit_status_ready():
                # Print stdout data
                if stdout.channel.recv_ready():
                    stdin.close()
                    std_out = stdout.readlines()
                    return ''.join(std_out)

                    # the method below, although recommended in the docs,
                    # is returning intermitent Nones with HIGHER frequency
                    # alldata = stdout.channel.recv(1024)
                    # while stdout.channel.recv_ready():
                    #     alldata += stdout.channel.recv(1024)
                    #
                    # # Print as string with utf8 encoding
                    # string = str(alldata).encode("utf-8")
                    # return string

        else:
            return "No connection."


sshClient = paramiko.client.SSHClient()
sshClient.set_missing_host_key_policy( paramiko.client.AutoAddPolicy() )
sshClient.connect( address, username=username, look_for_keys=True )

print( query( 'uname -a' ) )

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions