python paramiko Issue while closing the connection

穿精又带淫゛_ 提交于 2020-01-01 19:31:55

问题


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


来源:https://stackoverflow.com/questions/5193886/python-paramiko-issue-while-closing-the-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!