0

我正在尝试在服务器上执行并行 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。提前致谢。

4

2 回答 2

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's 异常处理之后,这里有一个更好的错误堆栈跟踪:

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
于 2016-08-29T06:50:23.940 回答
0

似乎是0.92.0库版本中的回归,现在已在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)

有关更多示例,请参阅文档

于 2016-08-31T10:41:47.780 回答