python paramiko Issue while closing the connection

2020-08-01 05:34发布

问题:

I have just been trying ssh connection with paramiko. Everything looks fine, but in the final step, when calling the close() method to disconnect the client.

Here is my script:

#!/usr/bin/python

import paramiko
import os

ssh = paramiko.SSHClient()
private_key = os.path.expanduser('~/.ssh/id_dsa')
mkey = paramiko.DSSKey.from_private_key_file(private_key,password='JacquiKoala')
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('monitor', username='probert', pkey = mkey)
stdin, stdout, stderr = ssh.exec_command('whoami')
print stdout.readlines()
ssh.close

The shell just hangs, I can type stuff, without any result, Ctrl+C or Ctrl+D don't stop the script nor the connection. I have no other way except closing the shell window which is kind of dirty.

I am running Ubuntu 10.10 with python 2.6.6 and paramiko-1.7.4 compiled from sources.

I really don't know what happens; the close() method is correctly executed as a print "blah" after is executed as well, no error message, and still connected without an appropriate way to stop it.

Thanks for helping me :)

Cheers;

Philippe

回答1:

TL;DR: You are not calling the function. Do so with ssh.close() instead of ssh.close

ssh.close is a reference to that function. It tells you about the function, it does not call the function. Here is an example:

def a():
    return 6

a
> <function a at 0x108f71aa0>
a()
> 6