尝试使用 paramiko 连接到远程 Unix 服务器时出现错误:
TypeError: 'NoneType' object is not iterable"
令人惊讶的是,当我尝试从另一台具有相同服务器、用户名和密码的笔记本电脑运行相同的脚本时,我能够连接到服务器并执行远程命令。
>>> import paramiko
>>> ssh = paramiko.SSHClient()
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh.connect(ftpipaddress,username ='akar',password ='change')
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: 'NoneType' object is not iterable
完全不知道安装了相同 Python 2.7 和 paramiko 包的笔记本电脑可能会出现什么问题。请帮忙。
请您参考如下方法:
根据documentation , connect 方法不返回任何东西。就 Python 而言,这意味着此方法返回 None。您可能正在寻找 exec_command 方法:
exec_command(command, bufsize=-1, timeout=None, get_pty=False)Execute a command on the SSH server. A new Channel is opened and the requested command is executed. The command’s input and output streams are returned as Python file-like objects representing stdin, stdout, and stderr.
Parameters:
command (str) – the command to execute
bufsize (int) – interpreted the same way as by the built-in file() function in Python
timeout (int) – set command’s channel timeout. See Channel.settimeout.settimeout
Returns: the stdin, stdout, and stderr of the executing command, as a 3-tuple
Raises SSHException: if the server fails to execute the command
