获取" TypeError:' NoneType'对象不可迭代"同时做并行ssh

时间:2016-08-29 06:27:48

标签: python ssh ssh-agent pssh

我正在尝试在服务器上执行并行ssh。在这样做时,我得到了" TypeError:' NoneType'对象不可迭代"这个错误。请帮助。

我的脚本在

下面
function Group(group) {
  this.id = group.id;
  this.title = group.title;
  this.users = [];
  Group.cache[this.id] = this;
  group.users.forEach(this.addUser, this);
}
Group.cache = {};
Group.prototype.addUser = function(user) {
  this.users.push(
    typeof user === 'number'
      ? User.cache[user]
      : new User(user)
  );
};

function User(user) {
  this.id = user.id;
  this.username = user.username;
  this.password = user.password;
  this.groups = [];
  User.cache[this.id] = this;
  user.groups.forEach(this.addGroup, this);
}
User.cache = {};
User.prototype.addGroup = function(group) {
  this.groups.push(
    typeof group === 'number'
      ? Group.cache[group]
      : new Group(group)
  );
};

// begins the recursion
JSON.parse(
    '[{"id":1,"title":"adminGroup","users":[{"id":1,"username":"admin","password":"...","groups":[1]},{"id":31,"username":"user78","password":"...","groups":[1]},{"id":3,"username":"ali","password":"...","groups":[{"id":2,"title":"newsWriterGroup","users":[{"id":14,"username":"staff1","password":"...","groups":[{"id":1005,"title":"FileManagerAccessGroup","users":[{"id":25,"username":"test1","password":"...","groups":[1005]},14]},2]},3]},1]}]}]'
).forEach(function(group) { new Group(group) });

function stopCircularWithId(key, value) {
  return key === 'users' || key === 'groups'
    ? value.map(function(u) { return u.id })
    : value;
}
console.log('groups:', JSON.stringify(Group.cache, stopCircularWithId, 4));
console.log('users:', JSON.stringify(User.cache, stopCircularWithId, 4));

Traceback在

之下
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()

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

2 个答案:

答案 0 :(得分:1)

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

read_openssh_config在此处返回None: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

答案 1 :(得分:0)

was seemingly a regression in the 0.92.0 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)

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

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了解更多示例。