在执行并行ssh时获取“TypeError:'NoneType'object is not iterable”

2024-09-30 01:22:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在服务器上执行并行ssh。在这样做的时候,我得到了“TypeError:'NoneType”object is not iterable“这个错误。请帮忙。在

我的剧本在下面

from pssh import ParallelSSHClient
from pssh.exceptions import AuthenticationException, UnknownHostException, ConnectionErrorException

def parallelsshjob():
        client = ParallelSSHClient(['10.84.226.72','10.84.226.74'], user = 'root', password = 'XXX')
        try:
                output = client.run_command('racadm getsvctag', sudo=True)
                print output
        except (AuthenticationException, UnknownHostException, ConnectionErrorException):
                pass
        #print output

if __name__ == '__main__':
        parallelsshjob()

回溯如下

^{pr2}$

帮助我解决这个问题,并建议我在这个脚本中使用ssh代理。提前谢谢。在


Tags: fromimport服务器clientoutputsshprintpssh
2条回答

通过在我的笔记本电脑上阅读代码和调试,我相信问题是您没有一个名为~/.ssh/config的文件。似乎parallel-ssh依赖于OpenSSH配置,这是丢失该文件时出现的错误。在

read_openssh_config此处不返回:https://github.com/pkittenis/parallel-ssh/blob/master/pssh/utils.py#L79

反过来,SSHClient.__init__在试图解压缩它期望接收的值时爆炸:https://github.com/pkittenis/parallel-ssh/blob/master/pssh/ssh_client.py#L97。在

修复的方法大概是让某种OpenSSH配置文件就位,但很抱歉,我对此一无所知。在

编辑

在清理了parallel-ssh的一些异常处理之后,下面是一个更好的错误堆栈跟踪:

Traceback (most recent call last):
  File "test.py", line 11, in <module>
    parallelsshjob()
  File "test.py", line 7, in parallelsshjob
    output = client.run_command('racadm getsvctag', sudo=True)
  File "/Users/smarx/test/pssh/venv/lib/python2.7/site-packages/pssh/pssh_client.py", line 517, in run_command
    self.get_output(cmd, output)
  File "/Users/smarx/test/pssh/venv/lib/python2.7/site-packages/pssh/pssh_client.py", line 601, in get_output
    (channel, host, stdout, stderr, stdin) = cmd.get()
  File "/Users/smarx/test/pssh/venv/lib/python2.7/site-packages/gevent/greenlet.py", line 480, in get
    self._raise_exception()
  File "/Users/smarx/test/pssh/venv/lib/python2.7/site-packages/gevent/greenlet.py", line 171, in _raise_exception
    reraise(*self.exc_info)
  File "/Users/smarx/test/pssh/venv/lib/python2.7/site-packages/gevent/greenlet.py", line 534, in run
    result = self._run(*self.args, **self.kwargs)
  File "/Users/smarx/test/pssh/venv/lib/python2.7/site-packages/pssh/pssh_client.py", line 559, in _exec_command
    channel_timeout=self.channel_timeout)
  File "/Users/smarx/test/pssh/venv/lib/python2.7/site-packages/pssh/ssh_client.py", line 98, in __init__
    host, config_file=_openssh_config_file)
TypeError: 'NoneType' object is not iterable

这个was seemingly a regression in the ^{} version of the library现在在0.92.1中解析。以前的版本也可以。OpenSSH config不应该是依赖项。在

要回答您的SSH代理问题,如果有一个正在运行并在用户会话中启用,它将自动使用。如果希望以编程方式提供私钥,可以执行以下操作

from pssh import ParallelSSHClient
from pssh.utils import load_private_key
pkey = load_private_key('my_private_key')
client = ParallelSSHClient(hosts, pkey=pkey)

也可以通过编程方式为代理提供多个密钥,如下所示

^{pr2}$

See documentation获取更多示例。在

相关问题 更多 >

    热门问题