【问题标题】:Getting "TypeError: 'NoneType' object is not iterable" while doing parallel ssh在执行并行 ssh 时获取“TypeError:'NoneType' 对象不可迭代”
【发布时间】:2017-01-05 02:28:11
【问题描述】:

我正在尝试在服务器上进行并行 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()

而且 Traceback 在下面

Traceback (most recent call last):
  File "parallelssh.py", line 17, in <module>
    parallelsshjob()
  File "parallelssh.py", line 10, in parallelsshjob
    output = client.run_command('racadm getsvctag', sudo=True)
  File "/Library/Python/2.7/site-packages/pssh/pssh_client.py", line 520, in run_command
    raise ex
TypeError: 'NoneType' object is not iterable

帮助我解决问题,并建议我在同一个脚本中使用 ssh-agent。提前致谢。

【问题讨论】:

    标签: python ssh ssh-agent pssh


    【解决方案1】:

    通过阅读代码并在我的笔记本电脑上进行一些调试,我认为问题在于您没有名为 ~/.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
    

    【讨论】:

      【解决方案2】:

      was seemingly a regression in the 0.92.0 version of the library 现在已在0.92.1 中解决。以前的版本也可以。 OpenSSH 配置不应是依赖项。

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

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

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

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

      See documentation 了解更多示例。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-25
        • 1970-01-01
        • 2017-08-29
        • 2016-08-30
        • 2014-11-21
        相关资源
        最近更新 更多