gpt4 book ai didi

python paramiko错误 "AttributeError: ' NoneType'对象没有属性 'open_session'“

转载 作者:行者123 更新时间:2023-12-01 09:17:20 24 4
gpt4 key购买 nike

我正在尝试 python paramiko 模块,出现以下错误。我该如何解决这个问题? (我搜索了很多,但没有幸运) 任何答复将不胜感激。 (在 Mac 操作系统上)

python2.7 aaa.py

Traceback (most recent call last):
File "aaa.py", line 24, in <module>
ssh_command('172.16.129.130', 22, 'user', 'password', 'ls')
File "aaa.py", line 14, in ssh_command
stdin,stdout,stderr = client.exec_command(command)
File "/Users/usera/anaconda2/lib/python2.7/site- packages/paramiko/client.py", line 480, in exec_command
chan = self._transport.open_session(timeout=timeout)
AttributeError: 'NoneType' object has no attribute 'open_session'

Python版本

Python 2.7.14 |Anaconda, Inc.| (default, Dec  7 2017, 11:07:58) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

Python代码

cat aaa.py
#-*- coding:utf-8 -*-

import paramiko

#def ssh_command(ip, port, username, password, command='pwd; ls'):
def ssh_command(ip, port, username, password, command):

paramiko.util.log_to_file("filename.log")

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect = (ip, port, username, password)
#stdin, stdout, stderr = client.exec_command(command)
stdin,stdout,stderr = client.exec_command(command)
#input = stdin.read()
output = stdout.read()
#output_error = stderr.read()

print ouput.decode('utf-8')

# Close the connect
client.close()

ssh_command('172.16.129.130', 22, 'user', 'password', 'ls')

最佳答案

问题是我的打字错误。

client.connect = (ip, port, username, password)

应该是

client.connect(ip, port, username, password)

关于python paramiko错误 "AttributeError: ' NoneType'对象没有属性 'open_session'“,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51122229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com